#!/bin/sh
# STMK 6th January 2011
# AIX Script to install License Management Software.

test `uname -s` = Linux && echocmd="echo -e" || echocmd="echo"

# ******** Ensure that the following variable always contains the version no
# ******** of the current License Manager.
THISVER=131
#*********

# Work out if the user has selected Japanese messages or not.  To do that, we
# first need to work out the location of cobgetmsg.  If anything fails, we
# continue using this (English messages) version.
BASE=`dirname $0`
if test ".$COBGETMSG" = "." && test -x $BASE/bin/cobgetmsg
then
    COBGETMSG=$BASE/bin/cobgetmsg
fi
if test ".$COBGETMSG" != "."
then
    extn=`$COBGETMSG -p$BASE mfinfo.lng 2 2> /dev/null`
    test ".$extn" != "." && test -x $0.$extn && exec $BASE/$0.$extn
fi

if test ! -w /etc/passwd
then
	$echocmd "You must be running as root to execute this script"
	exit 1
fi

set +u

unset COBPATH
unset COBDATA

COBSW=""
export COBSW

WARN=0
COBDIR=`pwd`
export COBDIR

LIBPATH=$COBDIR/lib:$LIBPATH:/lib
export LIBPATH

# Initialise ASLM if used
if test -x ./initaslm
then
	./initaslm
else
	if test -x ../aslmf/initaslm
	then
		../aslmf/initaslm
	fi
fi

OLDVERNO=`./lmfgetpv v`
if test "$OLDVERNO." != "."
then
	ADMINDIR=`./lmfgetpv p`

# update existing PRODFILE if located
	if test "$ADMINDIR." != "."
	then
		./lmfgetpv i $ADMINDIR
# now copy it to this directory for later use
		if test "$?" = "0"
		then
			cp $ADMINDIR/PRODFILE .
		fi
	fi

	if test "$ADMINDIR." = "."
	then
# license manager running, but its launch directory no longer exists
		if test "$OLDVERNO" -lt "$THISVER"
		then
			$echocmd "\n"
			$echocmd "An older version of License Manager than this one is "
			$echocmd "currently running on the system."
	        fi

		if test "$OLDVERNO" -eq "$THISVER"
		then
			$echocmd "\n"
			$echocmd "A possibly older version of License Manager than this one is "
			$echocmd "currently running on the system."
		fi

		if test "$OLDVERNO" -gt "$THISVER"
		then
			$echocmd "\n"
			$echocmd "A later version of License Manager than this one is "
			$echocmd "currently running on the system."
		fi

		$echocmd "However the directory it was loaded from no longer exists.\n"
        	$echocmd "DO YOU WANT THE CURRENTLY RUNNING LICENSE MANAGER TO BE STOPPED NOW? (y/n)"
        	read y
	        case "$y" in
               		y*|Y*)  ./lmfgetpv k
	                       $echocmd "\nLICENSE MANAGER REMOVED\n"
                       		;;
                        *)      ;;
        	esac

               $echocmd "\nDo you want to continue installation of this version of License Manager (y/n)?"
               read y
               case "$y" in
                   y*|Y*)  OLDVERNO=
	                   ADMINDIR=
                           ;;
                   *)      exit 1
                           ;;
               esac
	fi
fi

if test "$OLDVERNO." != "."
then
	if test "$OLDVERNO" -lt "$THISVER"
# license manager running, and its launch directory is located
	then
		$echocmd "\n"
		$echocmd "An older version of License Manager than this one is "
		$echocmd "currently running on the system. It is installed in"
		$echocmd "$ADMINDIR.\n"
                $echocmd "It is recommended that you upgrade by installing this version."
                $echocmd "Existing installed licenses will be retained and not affected."
		$echocmd "If you want to install this new version of License Manager"
        fi

	if test "$OLDVERNO" -eq "$THISVER"
	then
		$echocmd "\n"
		$echocmd "A possibly older version of License Manager than this one is "
		$echocmd "currently running on the system. It is installed in"
		$echocmd "$ADMINDIR.\n"
                $echocmd "It is recommended that you upgrade by installing this version."
                $echocmd "Existing installed licenses will be retained and not affected."
		$echocmd "If you want to install this new version of License Manager"
        fi

	if test "$OLDVERNO" -gt "$THISVER"
	then
		$echocmd "\n"
		$echocmd "A later version of License Manager than this one is "
		$echocmd "currently running on the system. It is installed in"
		$echocmd "$ADMINDIR.\n"
		$echocmd "YOU ARE ATTEMPTING TO INSTALL A VERSION OF LICENSE MANAGER"
		$echocmd "WHICH IS OLDER THAN THE VERSION ALREADY RUNNING\n"
                $echocmd "If you still want to install this older version of License Manager"
        fi

        $echocmd "into $ADMINDIR, then the currently running version must be stopped.\n"
        $echocmd "Do you want to proceed with installation into $ADMINDIR\n"
        $echocmd "(this will STOP the currently running License Manager) ? (y/n)"
        read y
        case "$y" in
               y*|Y*)  ./lmfgetpv k
		       while test -w "$ADMINDIR/mflm_manager"
		       do
			    rm $ADMINDIR/mflm_manager
		       done
                       $echocmd "\nLICENSE MANAGER STOPPED\n"
                       ;;
               *)      $echocmd "\nDo you want to install this new version of License Manager"
                       $echocmd "in a different directory to $ADMINDIR? (y/n)"
                       read y
                       case "$y" in
                           y*|Y*)  OLDADMINDIR=$ADMINDIR
                                   ADMINDIR=
                                   ;;
                           *)      exit 1
                                   ;;
                       esac
			;;
        esac
fi
if test "$ADMINDIR." = "."
then
	WARN=1

	DEFDIR="/opt/microfocus/mflmf"

	INVALID=TRUE
	while test "$INVALID" = "TRUE"
	do
		$echocmd "\nEnter the directory name where you wish to install License Manager"
		$echocmd "(Press Enter for default directory $DEFDIR)"
		INVALID=FALSE
		read ADMINDIR

		if test "$ADMINDIR" = "`pwd`"
		then
			INVALID=TRUE
			$echocmd "You cannot install in this directory"
		fi

		if test "$ADMINDIR" = "."
		then
			INVALID=TRUE
			$echocmd "You cannot install in this directory"
		fi

		if test "$ADMINDIR" = "./"
		then
			INVALID=TRUE
			$echocmd "You cannot install in this directory"
		fi
		if expr "$ADMINDIR" : '[^\/]' > /dev/null
		then
			INVALID=TRUE
			$echocmd "Absolute pathname required (ie must start with '/')"
		fi
	done


	if test "$ADMINDIR." = "."
	then
		ADMINDIR=$DEFDIR
	fi

        if test "$OLDADMINDIR" = "$ADMINDIR"
        then
           $echocmd "You cannot install here without stopping License Manager!"
           exit 1
        fi

	if test ! -d "$ADMINDIR"
	then
		$echocmd "$ADMINDIR does not exist"
		$echocmd "do you wish to create it ? (y/n) "
		read y
		case "$y" in
			y*|Y*)	mkdir -p $ADMINDIR
				chmod 777 $ADMINDIR
				;;
			*)	ADMINDIR=
				exit 1
				;;
		esac
	fi
fi

# Check this version no against version in install directory (if any)
if test "$WARN" = "1"
then
  if test -f $ADMINDIR/LMFVERNO
  then
	OLDVERNO=`cat $ADMINDIR/LMFVERNO`
	if test "$OLDVERNO" -gt "$THISVER"
	then
		$echocmd "\n"
		$echocmd "A later version of License Manager than this one is "
		$echocmd "already installed in"
		$echocmd "$ADMINDIR\n"
		$echocmd "YOU ARE ATTEMPTING TO INSTALL A VERSION OF LICENSE MANAGER"
		$echocmd "WHICH IS OLDER THAN THE VERSION ALREADY INSTALLED"
		INVALID=TRUE
		while test "$INVALID" = "TRUE"
		do
        		$echocmd "Do you want to proceed with installation into $ADMINDIR (y/n) "
		        read y
       			case "$y" in
       	        		y*|Y*)  INVALID=FALSE
                                   ;;
       	        		n*|N*)  exit 1
                                   ;;
	                        *)      ;;
                       esac
		done
        fi
  fi
fi

if test ! -d $ADMINDIR/lib
then	
	mkdir $ADMINDIR/lib
	chmod 777 $ADMINDIR/lib
fi
if test ! -d $ADMINDIR/terminfo
then
	mkdir $ADMINDIR/terminfo
	chmod 777 $ADMINDIR/terminfo
fi

if test -x "/usr/sbin/slibclean"
then
	/usr/sbin/slibclean
fi

# update/create PRODFILE
./lmfgetpv i $ADMINDIR

# now copy it to this directory so tar doesn't overwrite it
if test "$?" = "0"
then
	cp $ADMINDIR/PRODFILE .
fi

# delete any db that may have been previously created by user trying to
# use this directory as his lmf install directory. Failure to do this will
# cause this db to be installed as an invalid db to the lmf install directory.

OLDDB=0
if test -f ./mflmfdb
then
	OLDDB=1
fi
if test -f ./mflmfdb.idx
then
	OLDDB=1
fi
if test -f ./mflmfdbX
then
	OLDDB=1
fi
if test -f ./mflmfdbX.idx
then
	OLDDB=1
fi
if test "$OLDDB" = "1"
then
	$echocmd "\n"
	$echocmd "A license database has been unexpectedly found in the current directory."
	INVALID=TRUE
	while test "$INVALID" = "TRUE"
	do
		$echocmd "Continue the installation after deleting the database? (y/n) "
	        read y
		case "$y" in
               		y*|Y*)  INVALID=FALSE
                                rm ./mflmfdb*
                                ;;
        		n*|N*)  exit 1
                                ;;
               		*) 	;;
                esac
	done
fi


# cp -r * $ADMINDIR
# use tar instead of cp to maintain links

tar cf - . | (cd $ADMINDIR ; tar xf - )

# Overwrite the COBDIR/lmf/README.txt with the mflmf install area README.txt.
mv $ADMINDIR/.README_mflmf.txt README.txt

if test ! -f $ADMINDIR/mflmfdb
then
	cd $ADMINDIR
	./bin/rts32 lmfnewdb
fi	

# create empty log/backup files now so restricted access not a problem
touch $ADMINDIR/mflm-history
touch $ADMINDIR/MF-LMF.log
touch $ADMINDIR/mflmfdbX
touch $ADMINDIR/mflmfdbX.idx

# create an LMFVERNO file
$echocmd $THISVER > $ADMINDIR/LMFVERNO

rm $ADMINDIR/lmfinstall

$echocmd "Do you want only superuser to be able to access the License Admin System? (y/n)"
read y
case "$y" in
	y*|Y*)  chmod 700 $ADMINDIR
      		;;
	*)      ;;
esac

chmod 555 $ADMINDIR/*
chmod 544 $ADMINDIR/mflm_manager
chmod 777 $ADMINDIR/mflmfdb*

$echocmd "echo Starting MFLM License Manager" > $ADMINDIR/mflmrcscript
$echocmd "COBDIR=$ADMINDIR" >> $ADMINDIR/mflmrcscript
$echocmd "export COBDIR" >> $ADMINDIR/mflmrcscript

$echocmd "LIBPATH=\$COBDIR/lib:\$LIBPATH:/lib" >> $ADMINDIR/mflmrcscript
$echocmd "export LIBPATH" >> $ADMINDIR/mflmrcscript


$echocmd "cd $ADMINDIR" >> $ADMINDIR/mflmrcscript

$echocmd "./mflm_manager" >> $ADMINDIR/mflmrcscript
chmod 755 $ADMINDIR/mflmrcscript

$echocmd "It is recommended that you let license manager autostart at boot time"
$echocmd "Do you want license manager to be automatically started at boot time? (y/n)"
read y
case "$y" in
	y*|Y*)	
			cp $ADMINDIR/mflmrcscript /etc
			chmod 755 /etc/mflmrcscript
                        cp /etc/inittab /etc/inittab.bak
			check=`lsitab "mflmf" | grep mflmrcscript`
			if  [ ! -z "$check" ]
			then
			        rmitab "mflmf"
			fi
			check=`lsitab "mF" | grep mflmrcscript`
			if  [ ! -z "$check" ]
			then
			        rmitab "mF"
			fi
			mkitab "mF:2345:wait:sh /etc/mflmrcscript > /dev/null 2>&1"
       		;;

	*)	$echocmd "\nFor automatic startup of License Manager at boot time, you need to"
                $echocmd "copy $ADMINDIR/mflmrcscript to /etc"
		$echocmd "and add the following entry to your /etc/inittab file:"
		$echocmd "mF:2345:wait:sh /etc/mflmrcscript > /dev/null 2>&1"
		$echocmd "Refer to your Operating System Manual for details.\n"
		;;
esac
	
$echocmd $ADMINDIR > /tmp/lmfinstalldir
$echocmd "LMF installation complete\n"
