:
# WPCorp Script to handle various FLEXlm license manager tasks

# Get the execution directory
excdir=`echo $0 | sed 's|/[^/]*.$||g'` # dirname
if [ $excdir = "." -o $excdir = "$0" ]
then
	excdir=`pwd`
fi
export excdir

# For 7 the sdatadir is ../shlib10
sdatadir=$excdir/../shlib10

# No argument. The user must want to see status of the License Manager daemons
if [ $# -lt 1 ]
then
	$excdir/lmutil lmstat -a -c $sdatadir/license.dat
	exit 0
fi

if [ "$1" = "-h" -o "$1" = "-help" ]
then
	echo "Usage: wplicense [-s][-k][-e][-u][-r][-merge]"
fi

# Does the user want to see users of a specific feature?
if [ "$1" = "-u" ]
then
	# Not enough arguments.
	if [ $# -lt 2 ]
	then
		echo "Usage: wplicense -u [wp7]"
		exit 1
	fi

	$excdir/lmutil lmstat -c $sdatadir/license.dat -f $2
	exit 0
fi

# Now, make sure the user is a FLEX administrator
# i.e. super user or member of group "lmadmin"
if [ -f $excdir/lm_isadmin ]
then
	$excdir/lm_isadmin 2> /dev/null
	if [ $? = 0 ]
	then
		echo You must be a FLEX administrator to run $0 $1
		echo You must be SuperUser or a member of group "lmadmin"
		exit 1
	fi
fi

# Does the user want to start the license manager daemon?
if [ "$1" = "-s" ]
then
	if [ `$excdir/servers_up` = "UP" ]
	then
		echo "WP License daemon already UP"
		exit 1
	fi

	if [ -f $excdir/lmgrd ]
	then
		if [ -f $excdir/License.Log ]
		then
			COUNT=5
			while [ $COUNT -gt 1 ]
			do
				BAKFILE=$excdir/License.Logbk$COUNT
				COUNT=`expr $COUNT - 1`
				BAKFILE2=$excdir/License.Logbk$COUNT
				if [ -f $BAKFILE2 ]
				then
					mv $BAKFILE2 $BAKFILE
				fi
			done
			mv $excdir/License.Log $BAKFILE2
		fi

		nohup $excdir/lmgrd -c $sdatadir/license.dat -p -2 \
			> $excdir/License.Log &

		sleep 5;
		while [ ! -f $excdir/License.Log ]; do sleep 2; done;
		if [ `$excdir/servers_up` = "UP" ]
		then
			echo "WP License daemon started"
			exit 0
		else
			echo "WP License daemon NOT started"
			echo "Check License.Log file for possible errors"
			exit 1
		fi
	else
		echo "ERROR: File not found: " $excdir/lmgrd
		exit 1
	fi
fi

# Does the user want to kill the license manager daemon?
if [ "$1" = "-k" ]
then
	if [ -f $excdir/lmutil ]
	then
		$excdir/lmutil lmdown -q -c $sdatadir/license.dat 
		if [ $? = 0 -o $? = 255 ]
		then
			echo "WP License daemon killed"
			exit 0
		else
			echo "wplicense -k  -- FAILED"
			exit 1
		fi
	else
		echo "ERROR: File not found: " $excdir/lmutil
		exit 1
	fi
fi

# Does the user want to edit/create the license.dat file?
if [ "$1" = "-e" -o "$1" = "-merge" ]
then
	MYHOSTNM=""
	MACH1=""
	MACH2=""
	if [ -f /bin/hostname ]
	then
		MYHOSTNM=/bin/hostname
	elif [ -f /usr/bin/hostname ]
	then
		MYHOSTNM=/usr/bin/hostname
	fi
	if [ "$MYHOSTNM" ]
	then
		MACH1=`$MYHOSTNM 2> /dev/null`
		MACH2=`grep SERVER $sdatadir/license.dat | awk '{print $2}'`
	fi
	if [ "$MACH1" = "$MACH2" ]			#On the license server?
	then								#yes
		$excdir/wplicense -k			#Kill the old daemons
		sleep 2
	fi
	if [ -f $excdir/wpcreatlic ]
	then
		if [ "$1" = "-merge" ]
		then
			$excdir/wpcreatlic -merge
		else
			$excdir/wpcreatlic
		fi
		if [ "$MACH1" = "$MACH2" ]		#On the license server?
		then							#yes
			$excdir/wplicense -s		#Restart the new daemons
		fi
		exit 0
	else
		echo "ERROR: File not found: " $excdir/wpcreatlic
		exit 1
	fi
fi

# Does the user want to remove a license of a specific user?
if [ "$1" = "-r" ]
then
	if [ -f $excdir/lmutil ]
	then

		# Not enough arguments.
		if [ $# -lt 5 ]
		then
			echo "Usage: wplicense -r wp7 user host [display]"
			exit 1
		fi

		$excdir/lmutil lmremove -c $sdatadir/license.dat $2 $3 $4 $5
		exit 0
	else
		echo "ERROR: File not found: " $excdir/lmutil
		exit 1
	fi
fi

if [ "$1" = "-df" -o "$1" = "-ef" ]
then
	if [ -f $excdir/wpcreatlic ]
	then
		$excdir/wpcreatlic $1
		exit 0
	else
		echo "ERROR: File not found: " $execdir/wpcreatlic
		exit 1
	fi
fi

echo "Invalid option."
echo "Usage: wplicense [-s][-k][-e][-u][-r][-merge]"

exit 1

