#! /bin/sh
#
# Copyright 1994 Sun Microsystems, Inc. All Rights Reserved.
#
# request: SUNWcccfg
#

#ident	"@(#)request 1.13	93/03/15	SMI"

FEATURE="SUNWcoop"
FEATVERS="1.100"
PRODNAME="Cooperative Consoles"
PRODVERS=1.100
PRODRTU=0
HOSTNAME=`uname -n`

UNAME=` uname -r 2>/dev/null` ||.;   
case $UNAME in
	"") TARARCH="sunos";;
	4.*) TARARCH="sunos";;
	5.*) TARARCH="svr4";;
	*) TARARCH="svr4";;
	esac

if [ "$TARARCH" = "svr4" ]
then
	HOSTID=`/usr/ucb/hostid`
else
	HOSTID=`/bin/hostid`
fi
      
#
# The request script for CC installation.
# We request the following input from the user:
#	CCLICENSE = Flex license password
#	CCLICDATE = expiration date
#
if [ "$TARARCH" = "svr4" ]
then
	ASKPASSWORD="\nPlease enter the License Password for this node"
	ASKCORRECT="\nIs this correct"
	ASKDEMO="\nIs this a demo license"
	ASKDATE="\nPlease enter the expiration date (dd-mmm-yyyy)"
	ASKInstall="\nDo you want to install the license now"
else
	ASKPASSWORD="Please enter the License Password for this node"
	ASKCORRECT="Is this correct"
	ASKDEMO="Is this a demo license"
	ASKDATE="Please enter the expiration date (dd-mmm-yyyy)"
	ASKInstall="Do you want to install the license now"
fi

#
# echo $1, return 1 for yes 0 for no
#
askyesno ()
{
	while true
	do
		if [ "$TARARCH" = "svr4" ]
		then
			echo "$1 (y/n): \c"
		else
			echo "$1 (y/n): "
		fi
		read ans
		if [ "$TARARCH" = "svr4" ]
		then
			case $ans in
				"Y"|"y") return 1;;
				"N"|"n") return 0;;
				*) echo "\n\tInvalid Input: $ans";;
			esac
		else
			case $ans in
				"Y"|"y") return 1;;
				"N"|"n") return 0;;
				*) echo "Invalid Input: $ans";;
			esac
		fi

	done
}

#
# echo $1, $2 contains default
#
askyesno_default ()
{
	while true
	do
		if [ "$TARARCH" = "svr4" ]
		then
			echo "$1 (y/n) [$2]: \c"
		else
			echo "$1 (y/n) [$2]: "
		fi
		read ans
		if [ "$ans" = "" ]
		then
			ans=$2
		fi

		if [ "$TARARCH" = "svr4" ]
		then
			case $ans in
				"Y"|"y") return 1;;
				"N"|"n") return 0;;
				*) echo "\n\tInvalid Input: $ans";;
			esac
		else
			case $ans in
				"Y"|"y") return 1;;
				"N"|"n") return 0;;
				*) echo "Invalid Input: $ans";;
			esac
		fi
	done
}


# Echo $1 and get non-null string ($2)
#
get_string ()
{
	while true
	do
		if [ "$TARARCH" = "svr4" ]
		then
			echo "$1: \c"
		else
			echo "$1: "
		fi
		read $2
		eval tmp_str="\$$2"
		if [ "$tmp_str" != "" ]
		then
			unset tmp_str
			return
		fi
	done
}

#
# return checksum of $1 in $2
#
checksum ()
{
	if [ "$TARARCH" = "svr4" ]
	then
		XXCHKSUM=`echo "$1\c" | sum -r | awk ' { printf "%4x", $1 } '`
	else
		XXCHKSUM=`echo "$1\c" | /usr/5bin/sum -r | awk ' { printf "%4x", $1 } '`
	fi
	XXCHKSUM=`echo "$XXCHKSUM" | cut -c3-`
	eval $2=$XXCHKSUM
	unset XXCHKSUM
}

instructions ()
{
if [ "$TARARCH" = "svr4" ]
then
	echo "\nTo obtain a license for this product, please call:\n"
	echo "\tBelgium        : 078 11 21 03"
	echo "\tCanada         : 1-800-872-4786"
	echo "\tFinland        : 9800 14406"
	echo "\tFrance         : 05 90 83 41"
	echo "\tGermany        : 0130 81 47 33"
	echo "\tItaly          : 1678 77252"
	echo "\tJapan          : 813-3263-3821"
	echo "\tNetherlands    : 06 0224198"
	echo "\tPuerto Rico    : 1-800-872-4786"
	echo "\tSpain          : 900 97 4448"
	echo "\tSweden         : 020 793154"
	echo "\tSwitzerland    : 155 8096"
	echo "\tUnited Kingdom : 0800 929 112"
	echo "\tUnited States  : 1-800-872-4786"
	echo "\tEuropean countries not listed: +44 937 541511"
	echo "\tOther countries: Local Distributor"

	echo "\nThe license distributor will ask you for the following information:"
	echo "\n\tProduct Name   : $PRODNAME"
	echo "\tProduct Version: $PRODVERS"
	echo "\tServer Name    : $HOSTNAME"
	echo "\tHost ID        : $HOSTID"
	checksum "$FEATURE$FEATVERS$HOSTNAME$HOSTID" CHKSUM
	echo "\tData Checksum  : $CHKSUM"
	unset CHKSUM

	echo "\n\tYou will also need to provide the Serial Number from"
	echo "\tthe license certificate."
else
	echo "To obtain a license for this product, please call:"
	echo "Belgium        : 078 11 21 03"
	echo "Canada         : 1-800-872-4786"
	echo "Finland        : 9800 14406"
	echo "France         : 05 90 83 41"
	echo "Germany        : 0130 81 47 33"
	echo "Italy          : 1678 77252"
	echo "Japan          : 813-3263-3821"
	echo "Netherlands    : 06 0224198"
	echo "Puerto Rico    : 1-800-872-4786"
	echo "Spain          : 900 97 4448"
	echo "Sweden         : 020 793154"
	echo "Switzerland    : 155 8096"
	echo "United Kingdom : 0800 929 112"
	echo "United States  : 1-800-872-4786"
	echo "European countries not listed: +44 937 541511"
	echo "Other countries: Local Distributor"

	echo "The license distributor will ask you for the following information:"
	echo "Product Name   : $PRODNAME"
	echo "Product Version: $PRODVERS"
	echo "Server Name    : $HOSTNAME"
	echo "Host ID        : $HOSTID"
	checksum "$FEATURE$FEATVERS$HOSTNAME$HOSTID" CHKSUM
	echo "Data Checksum  : $CHKSUM"
	unset CHKSUM

	echo "You will also need to provide the Serial Number from"
	echo "the license certificate."
fi

}

validate_date ()
{
	if [ "$1" = "" ]
	then
		return 0
	fi

	goodlines=`echo $1 | grep -c -v "[^0-9]"`
	
	if [ $goodlines -eq 1 -a $1 -ge 0 -a $1 -le 31 ]
	then
		return 1
	else
		return 0
	fi
}

validate_month ()
{
	case $1 in

		"jan"|"feb"|"mar"|"apr"|"may"|"jun"|"jul"|"aug"|"sep"|	\
		"oct"|"nov"|"dec"|"Jan"|"Feb"|"Mar"|"Apr"|"May"|"Jun"|	\
		"Jul"|"Aug"|"Sep"|"Oct"|"Nov"|"Dec") return 1;;

		*) return 0;;
	esac
}

validate_year ()
{
	if [ "$1" = "" ]
	then
		return 0
	fi

	goodlines=`echo $1 | grep -c -v "[^0-9]"`
	
	if [ $goodlines -eq 1 ]
	then
		return 1
	else
		return 0
	fi
}

#
# get date in $1, return 1 if OK, 0 if not ok
#
get_date ()
{
	get_string "$ASKDATE" $1

	eval tmp_str="\$$1"

	CCDAY=`echo $tmp_str | cut -d- -f1`
	CCMON=`echo $tmp_str | cut -d- -f2`
	CCYEAR=`echo $tmp_str | cut -d- -f3`

	validate_date $CCDAY
	if [ "$?" = "0" ]
	then
		if [ "$TARARCH" = "svr4" ]
		then
			echo "\n\tInvalid Date: $CCDAY"
		else
			echo "Invalid Date: $CCDAY"
		fi
		return 0
	fi

	validate_month $CCMON
	if [ "$?" = "0" ]
	then
		if [ "$TARARCH" = "svr4" ]
		then
			echo "\n\tInvalid Month: $CCMON"
		else
			echo "Invalid Month: $CCMON"
		fi
		return 0
	fi

	validate_year $CCYEAR
	if [ "$?" = "0" ]
	then
		if [ "$TARARCH" = "svr4" ]
		then
			echo "\n\tInvalid Year: $CCYEAR"
		else
			echo "Invalid Year: $CCYEAR"
		fi
		return 0
	fi

	return 1
}

#
# ensure license is a string in 0-9|A-F
#
get_validlicense ()
{
	PALLDONE=0
	while [ $PALLDONE -eq 0 ]
	do
		get_string "$ASKPASSWORD" $1
		eval tmp_str="\$$1"
		PALLDONE=`echo $tmp_str | grep -c "[0-9|A-F]"`
		if [ $PALLDONE -eq 0 ]
		then
			if [ "$TARARCH" = "svr4" ]
			then
				echo "\n\tInvalid Password: $tmp_str"
			else
				echo "Invalid Password: $tmp_str"
			fi
		fi
	done
	unset tmp_str PALLDONE
}

get_license ()
{
	ALLDONE=0
	while [ $ALLDONE -eq 0 ]
	do
		get_validlicense CCLICENSE

		unset CCLICDATE

		askyesno_default "$ASKDEMO" n
		if [ "$?" = "1" ]
		then
			DALLDONE=0
			while [ $DALLDONE -eq 0 ]
			do
				get_date CCLICDATE
				DALLDONE=$?
			done
		else
			CCLICDATE="01-jan-0"
		fi

		checksum "$CCLICDATE$PRODRTU$CCLICENSE" CHKSUM
		if [ "$TARARCH" = "svr4" ]
		then
			echo "\nThe password checksum is: $CHKSUM"
		else
			echo "The password checksum is: $CHKSUM"
		fi
		unset CHKSUM
		askyesno_default "$ASKCORRECT" y
		ALLDONE=$?
	done
}


# main ()

instructions

if [ "$TARARCH" = "svr4" ]
then
	echo "\nPlease Hit Return to continue .....\c"
else
	echo "Please Hit Return to continue ....."
fi
read ans; unset ans

#
# Get license from the user
#
askyesno_default "$ASKInstall" y
if [ "$?" = "1" ]
then
get_license

if [ "$CCLICDATE" = "" ]
then
	CCLICDATE="01-jan-0"
fi



SERVERNAME=lic.SUNW
PRODVERS=1.100
FEATURE=SUNWcoop

if [ "$TARARCH" = "svr4" ]
then
	ETCCC=/var/opt/SUNWconn/snm/cc_files
else
	ETCCC=/var/adm/snm/cc_files
fi

#
# First character of license is the vendor string
#
# VENDORSTR=`echo $CCLICENSE | cut -c-1`
# CCLICENSE=`echo $CCLICENSE | cut -c2-`

#
# Create the license file in /var/opt/SUNWconn/snm/cc_files or /var/adm/snm/cc_files
#
echo "INCREMENT $FEATURE $SERVERNAME $PRODVERS $CCLICDATE 0 $CCLICENSE \"0\" $HOSTID" >> $ETCCC/license.dat

fi
