#! /bin/sh
# $Id: chk_lic_file.sh,v 1.4 2002/04/16 20:06:56 rlawrence Exp $ 
# SCCSID(@(#)chk_lic_file.sh	1.6) Modified 14:39:17 6/30/01
# Copyright 2001 NetTraverse, Inc.
# A shell script that makes sure the license is installed correctly.
# Also optionally checks for expired licenses and if another
# Windows session is allowed to start. 
# Exits with zero and nothing printed when all OK.
# When not ok, then prints a message to stdout and exits with
# a value between 1 and 24 which indicates the problem.

# The first section tests have these error returns:
#	11 = System is messed up
#	12 = Missing license file
#	13 = License file not readable.
#	14 = License file does not have LICENSE_CODE
#	99 = Problem with temp file.
# The second test uses check_license and returns its error codes:
#	1 thru 9 indicate an invalid base license.
# The third (optional) test use win_probe(.sh) to see if ok to run now.
# The error codes from it are returned (11,12,15-24).  11 and 12
# overlap with the first section tests:
#	11 = System messed up
#	12 = missing license file
# 15 thru 24 are unique to win_probe.

P1="$1"
P2="$2"

# Get the STATICMERGE setting.
. /etc/mrgssv.sh
if [ "x$STATICMERGE" = "x" ]
then
	# If STATICMERGE is not set then things are really
	# screwed up, so it is pointless to try to search
	# for a localized error message.  So there is no
	# need to NLSize the following message.
	echo "Missing or invalid /etc/mrgssv.sh or /etc/default/merge"
	echo "Exiting..."
	exit 11
fi
if [ ! -d "$VARMERGE" ]
then
	echo Directory $VARMERGE is not exist.
	exit 11
fi
if [ ! -d "$VARMERGE/install" ]
then
	echo Directory $VARMERGE/install is not exist.
	exit 11
fi

L_FILE="$VARMERGE"/install/license.lic
if [ ! -r "$L_FILE" ]
then
	if [ ! -f "$L_FILE" ]
	then
		echo "Missing license file $L_FILE"
		exit 12
	fi
	echo "License file $L_FILE is not readable."
	exit 13
fi

if [ -w "$VARMERGE"/tmp ]
then
	T_FILE="$VARMERGE"/tmp/tmp$$.tmp
	[ -f "$T_FILE" ] && exit 99
else
	T_FILE="$HOME"/tmp$$.tmp
	if [ -f "$T_FILE" ]
	then
		T_FILE=
	else
		echo x > "$T_FILE"
		if [ ! -f "$T_FILE" ]
		then
			T_FILE=
		fi
	fi
	if [ "_$T_FILE" = "_" ]
	then
		T_FILE=/tmp/tmp$$.tmp
		[ -f "$T_FILE" ] && exit 99
	fi
fi
echo "# temporary file" > "$T_FILE"
grep "^# start base license" "$L_FILE" > /dev/null
if [ $? -eq 0 ]
then
	# Scan just in the start to stop area.
	STATE=1
else
	# No special start line, so scan from the beginning.
	STATE=2
fi
cat "$L_FILE" | while read LINE
do
	case "$LINE" in
	\#\ start\ base\ license*)
		STATE=2
		;;
	\#\ end\ base\ license*)
		break
		;;
	LICENSE_CODE=*)
		[ $STATE -ne 2 ] && continue
		echo "$LINE" | "$STATICMERGE"/charconv -d -x >> "$T_FILE"
		break
		;;
	esac
done 
eval `grep "^LICENSE_CODE=" "$T_FILE"`
rm -f "$T_FILE"

if [ "_$LICENSE_CODE" = "_" ]
then
	echo "License file $L_FILE is missing LICENSE_CODE"
	exit 14
fi

$STATICMERGE/bin/check_license $LICENSE_CODE > /dev/null 2>&1
ERR_CODE=$?
if  [ $ERR_CODE -ne 0 ]
then
	echo "Invalid license"
	exit $ERR_CODE
fi

if [ "x$P2" = "x" ]
then
	CONFIG="win"
else
	CONFIG="$P2"
fi
case "_$P1" in
_-e)
	# "-e" -- Means to check for an expired license.
	OPT=-e
	;;
_-s)
	# "-s" -- Same as "-e" plus verify that session limits
	#	  will allow a Windows session to be started now.
	OPT=-s
	;;
_-x)
	# "-x" -- Means to check to see if ok to start a session
	#	  using the specified configuration.
	OPT=-x
	;;
*)
	# OK!
	exit 0
	;;
esac

# win_probe returns 0 or an error code from 11 to 24.
MSG=`$STATICMERGE/bin/win_probe $OPT $CONFIG 2> /dev/null`
ERR_CODE=$?
if  [ $ERR_CODE -eq 0 ]
then
	exit 0
fi
echo "$MSG"
exit $ERR_CODE
