#!/bin/sh
# @(#)activate.cpp	1.23 revision of 97/10/08  18:31:41
# This is an unpublished work copyright (c) 1989 HELIOS Software GmbH
# 30159 Hannover 1, Germany
set +vx

# @(#)HeliosVersion 2.5.0a2 Copyright 1989-1998 HELIOS Software Garbsen

# @(#)setup.cpp	1.28 revision of 98/02/09  19:27:44
PATH=$ESDIR:$ESDIR/etc:$PATH
export PATH
grop="macusers"
user="macuser"
hostupper=`hostname|sed 's/\..*//'|tr '[a-z]' '[A-Z]'` 2>/dev/null
if test -z "$hostupper"
then
	hostupper=`uname -n|sed 's/\..*//'|tr '[a-z]' '[A-Z]'`
fi
hostid=`machid`
# test how we can suppress the new line on echo
if test "X`echo -n`" = "X-n"
then
	en=''
else
	en='-n'
fi
if test "X`echo '\c'`" = "X\c"
then
	ec=''
else
	ec='\c'
fi










fstyp="-k -l"
Slevel=3000
Klevel=3000
rcdir=/etc










# @(#)line.cpp	1.9 revision of 93/04/20  22:29:16
# Berkeley has a stoneage bourne shell, so this mess is really necessary.
# this is also the reason not to use cat <<-eof to keep the code nicely
# indented.
status=0
line=""
gets=". /tmp/gets$$"
trap "rm -f /tmp/gets$$; exit \$status" 0 1 2 3 15
cat >/tmp/gets$$ <<\eof
line="!"
while test x"$line" = x"!"
do
	echo $en "* $p ? [$d] "$ec
	read line
	if test x"$line" = x"!"
	then
		$SHELL
	fi
done
if test -z "$line"
then
	line="$d"
fi
eof


LFILE="$ESDIR/conf/ethershare.license"
BASE="EtherShare"
UNITS="20"
EXP="EtherShare User Expansion"
serial=""
expires=""
checksum=""
units=""
bserial=""
done="n"
tmpfile="/tmp/lic$$"

if test ! -f $LFILE
then
cat <<eof

You now need to enter a license to serialize the EtherShare programs.
You get the key by mail or fax from HELIOS Software. The people at
HELIOS will want to know your machine ID and the software serial number
from the tape label. The machine ID of this computer is "$hostid", but
you will have to read the serial number on the tape or disk yourself.
Please get your license from HELIOS in one of the following ways:

	FAX:		+49 5131 709325

	Internet:	keys@helios.de
	
	Phone:		+49 5131 709320 (09:00 to 17:00 middle european time)

	Paper Mail:	HELIOS Software GmbH
			Steinriede 3
			D-30827 Garbsen
			Germany

Do not forget, the request must include your machine ID and the software
serial number, without these we cannot give you a license.

eof
	p="Press return to continue" d="" $gets
	default="1"
else
	default="2"
fi

while test "$done" != "y"
do
cat <<eof

	EtherShare License Menu
	-----------------------

	The machine ID (host ID) of this computer is "$hostid"

	1)	Enter a EtherShare base license
	2)	Enter an user expansion license
	3)	Display licenses
	4)	Delete a license
	5)	Return to main menu

eof
	p="Your choice" d=$default $gets
	case "$line" in
	1|b|B)
		license_ok="n"
		while test $license_ok = "n"
		do
			ok="n"
			while test $ok = "n"
			do
				p="Software serial number (8 digits, q to quit)" d="$serial" $gets
				if test "X$line" = Xq
				then
					break 2
				fi
				serial="$line"
				line=`echo $en "$serial"$ec|wc -c`
				if test "$line" -ne 8
				then
					echo "The serial number must be 8 digits!"
				else
					grep "^Serial:.*$serial" $LFILE >/dev/null 2>&1
					if test $? -ne 0
					then
						ok="y"
					else
						echo "License with serial $serial is already installed"
					fi
				fi
			done
			echo "If you have a full license, leave the next field blank."
			p="Expiration date (eg. 1-Oct-1991, q to quit)" d="" $gets
			if test "X$line" = Xq
			then
				break
			fi
			expires="$line"
			ok="n"
			while test $ok = "n"
			do
				p="Number of users (units, one or more decimal digits, q to quit)" d="$UNITS" $gets
				if test "X$line" = Xq
				then
					break 2
				fi
				units="$line"
				if test -z "$units" -o "$units" -eq 0
				then
					echo "The number of users must be at least one digit!"
				else
					ok="y"
				fi
			done
			ok="n"
			while test $ok = "n"
			do
				p="Checksum (eg. abcd-defg-hjkl-mnop, q to quit)" d="$checksum" $gets
				if test "X$line" = Xq
				then
					break 2
				fi
				checksum="$line"
				line=`echo $en "$checksum"$ec|wc -c`
				if test "$line" -ne 19
				then
					echo "The checksum must be 19 characters!"
				else
					ok="y"
				fi
			done
			echo "Product:	$BASE" >$tmpfile
			echo "Serial:		$serial" >>$tmpfile
			echo "Units:		$units" >>$tmpfile
			if test -n "$expires"
			then
				echo "Expires:	$expires" >>$tmpfile
				if test "$serial" = "12345679"
				then
					echo "MachID:		$hostid" >>$tmpfile
				fi
			else
				echo "MachID:		$hostid" >>$tmpfile
			fi
			echo "Checksum:	$checksum" >>$tmpfile
			$ESDIR/lcheck -f $tmpfile "$BASE"
			if test $? -eq 0
			then
				license_ok="y"
			fi
		done
		if test $license_ok = "y"
		then
			if test -f $LFILE
			then
				cp $LFILE $LFILE.old
				awk "/^Product:/{
					if (del == 0) {
						for (i=0; i<lines; i++)
							print line[i]
					}
					lines=0
					del=0
					if (NF == 2 && \$2 == \"$BASE\")
						del=1
				}
				{
					line[lines++] = \$0
				}
				END {
					if (del == 0) {
						for (i=0; i<lines; i++)
							print line[i]
					}
				}" $LFILE.old >$LFILE
			fi
			cat $tmpfile >>$LFILE
cat <<eof

License is valid and has been entered into the license data base.

eof
			default="5"
		fi
		;;
	2|e|E)
		license_ok="n"
		while test $license_ok = "n"
		do
			ok="n"
			while test $ok = "n"
			do
				p="Expansion serial number (8 digits, q to quit)" d="$serial" $gets
				if test "X$line" = Xq
				then
					break 2
				fi
				serial="$line"
				line=`echo $en "$serial"$ec|wc -c`
				if test "$line" -ne 8
				then
					echo "The serial number must be 8 digits!"
				else
					grep "^Serial:.*$serial" $LFILE >/dev/null 2>&1
					if test $? -ne 0
					then
						ok="y"
					else
						echo "License with serial $serial is already installed"
					fi
				fi
			done
			ok="n"
			while test $ok = "n"
			do
				p="Base serial number (8 digits, q to quit)" d="$bserial" $gets
				if test "X$line" = Xq
				then
					break 2
				fi
				bserial="$line"
				line=`echo $en "$bserial"$ec|wc -c`
				if test "$line" -ne 8
				then
					echo "The serial number must be 8 digits!"
				else
					grep "^Serial:.*$bserial" $LFILE >/dev/null 2>&1
					if test $? -ne 0
					then
						echo "Could not find base license with serial $bserial"
					else
						ok="y"
					fi
				fi
			done
			ok="n"
			while test $ok = "n"
			do
				p="Number of users (one or more decimal digits, q to quit)" d="$units" $gets
				if test "X$line" = Xq
				then
					break 2
				fi
				units="$line"
				if test -z "$units" -o "$units" -eq 0
				then
					echo "The number of users must be at least one digit!"
				else
					ok="y"
				fi
			done
			ok="n"
			while test $ok = "n"
			do
				p="Checksum (eg. abcd-defg-hjkl-mnop, q to quit)" d="$checksum" $gets
				if test "X$line" = Xq
				then
					break 2
				fi
				checksum="$line"
				line=`echo $en "$checksum"$ec|wc -c`
				if test "$line" -ne 19
				then
					echo "The checksum must be 19 characters!"
				else
					ok="y"
				fi
			done
			echo "Product:	$EXP" >$tmpfile
			echo "Serial:		$serial" >>$tmpfile
			echo "Base:		$bserial" >>$tmpfile
			echo "Units:		$units" >>$tmpfile
			echo "Checksum:	$checksum" >>$tmpfile
			$ESDIR/lcheck -f $LFILE -f $tmpfile -s $serial
			if test $? -eq 0
			then
				license_ok="y"
			fi
		done
		if test $license_ok = "y"
		then
			cat $tmpfile >>$LFILE
cat <<eof

License is valid and has been entered into the license data base.

eof
			default="5"
		fi
		;;
	3|d|D)
		more $LFILE
		;;
	4|r|R)
		ok="n"
		while test $ok = "n"
		do
			p="Software serial number to delete (8 digits, q to quit)" d="$serial" $gets
			if test "X$line" = Xq
			then
				break
			fi
			serial="$line"
			line=`echo $en "$serial"$ec|wc -c`
			if test "$line" -ne 8
			then
				echo "The serial number must be 8 digits!"
			else
				ok="y"
			fi
		done
		if test $ok = "y"
		then
			grep "^Serial:.*$serial" $LFILE >/dev/null 2>&1
			if test $? -ne 0
			then
				echo "Could not find license with serial $serial"
			else
				cp $LFILE $LFILE.old
				awk "/^Product:/{
					if (del == 0) {
						for (i=0; i<lines; i++)
							print line[i]
					}
					lines=0
					del=0
				}
				/^Serial:/{
					if (\$2 == \"$serial\")
						del=1
				}
				{
					line[lines++] = \$0
				}
				END {
					if (del == 0) {
						for (i=0; i<lines; i++)
							print line[i]
					}
				}" $LFILE.old >$LFILE
			fi
		fi
		;;
	5|q|Q)
		done="y"
		;;
	*)
		echo "Please enter a number between 1 and 5"
		;;
	esac
done

rm -f $tmpfile
