#
# Copyright (c) 1992, by Sun Microsystems, Inc.
#
# request: SUNWdniKR
#

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

FEATURE="SUNWdni"
FEATVERS="8.000"
PRODNAME=DNI
PRODVERS=8.0
PRODRTU=0
HOSTNAME=`uname -n`
HOSTID=`/usr/ucb/hostid`
#
# The request script for DNI installation.
# We request the following input from the user:
#	DNIINTERFACE = le0..
# 	DNIHOSTNAME = host name
#	DNIAREA = area number
#	DNINODE = node number
#	DNILICENSE = Flex license password
#	DNILICDATE = expiration date
#

ASKAREA="\nPlease enter the DECnet area number for this node"
ASKNODE="\nPlease enter the DECnet node number for this node"
ASKCONTROLLER="\nPlease enter the Ethernet Controller unit number for DECnet"
ASKINTERFACE="\nPlease enter the interface you want to use (le0/ie0)"
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)"

#
# echo $1, return 1 for yes 0 for no
#
askyesno ()
{
	while true
	do
		echo "$1 (y/n): \c"
		read ans
		case $ans in

			"Y"|"y") return 1;;
		
			"N"|"n") return 0;;

			*) echo "\n\tInvalid Input: $ans";;
		esac
	done
}

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

			"Y"|"y") return 1;;
		
			"N"|"n") return 0;;

			*) echo "\n\tInvalid Input: $ans";;
		esac
	done
}


#
# Echo $1 and get number ($4) in the range $2 to $3
#
get_number_range ()
{
	temp=1
	while true
	do
		echo "$1 ($2 - $3): \c"
		read $4
		eval tmp_str="\$$4"
		tmp_lines=`echo $tmp_str | grep -c -v "[^0-9]"`
		tmp_words=`echo $tmp_str | wc -w`
		if [ $tmp_lines -eq 1 -a $tmp_words -eq 1 -a	\
				$tmp_str -ge $2 -a $tmp_str -le $3 ]
		then
			unset tmp_str tmp_lines tmp_words
			return
		else
			echo "\n\tInvalid Input: $tmp_str"
		fi
	done
}

#
# Echo $1 and get non-null string ($4) of len in range $2 to $3
#
get_word ()
{
	while true
	do
		echo "$1: \c"
		read $4
		eval tmp_str="\$$4"
		if [ "$tmp_str" = "" ]
		then
			return
		fi

		tmp_lines=`echo $tmp_str | grep -c -v "[^a-z|^A-Z|^0-9]"`
		tmp_words=`echo $tmp_str | wc -w`
		tmp_len=`echo $tmp_str | wc -c`
		if [ $tmp_lines -eq 1 -a  $tmp_words -eq 1 -a	\
				$tmp_len -gt $2 -a $tmp_len -le $3 ]
		then
			unset tmp_str tmp_words tmp_len tmp_lines
			return
		else
			echo "\n\tInvalid Input: $tmp_str"
		fi
	done
}

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

#
# return checksum of $1 in $2
#
checksum ()
{
	XXCHKSUM=`echo "$1\c" | sum -r | awk ' { printf "%4x", $1 } '`
	XXCHKSUM=`echo "$XXCHKSUM" | cut -c3-`
	eval $2=$XXCHKSUM
	unset XXCHKSUM
}

instructions ()
{
	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."

	#echo "\nThe operator will respond with an encoded password."
	#echo "\nYou will need to enter the License Password and configuration"
	#echo "information necessary to bring up the machine as a DECnet node."
}

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"

	DNIDAY=`echo $tmp_str | cut -d- -f1`
	DNIMON=`echo $tmp_str | cut -d- -f2`
	DNIYEAR=`echo $tmp_str | cut -d- -f3`

	validate_date $DNIDAY
	if [ "$?" = "0" ]
	then
		echo "\n\tInvalid Date: $DNIDAY"
		return 0
	fi

	validate_month $DNIMON
	if [ "$?" = "0" ]
	then
		echo "\n\tInvalid Month: $DNIMON"
		return 0
	fi

	validate_year $DNIYEAR
	if [ "$?" = "0" ]
	then
		echo "\n\tInvalid Year: $DNIYEAR"
		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
			echo "\n\tInvalid Password: $tmp_str"
		fi
	done
	unset tmp_str PALLDONE
}

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

		unset DNILICDATE

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

		checksum "$DNILICDATE$PRODRTU$DNILICENSE" CHKSUM
		echo "\nThe password checksum is: $CHKSUM"
		unset CHKSUM

		askyesno_default "$ASKCORRECT" y
		ALLDONE=$?
	done
}

askall ()
{
	unset DNIHOSTNAME DNIAREA DNINODE

	HNAME=`uname -n|cut -c-6`
	ASKHOST="\nPlease enter the DECnet node name for this node (2 to 6 chars) [$HNAME]"
	get_word "$ASKHOST" 2 7 DNIHOSTNAME

	if [ "$DNIHOSTNAME" = "" ]
	then
		DNIHOSTNAME=$HNAME
	fi

	get_number_range "$ASKAREA" 1 63 DNIAREA

	get_number_range "$ASKNODE" 1 1023 DNINODE

	echo "\nThe following ethernet interface(s) are configured on your system:\n"
	INTERFACE=`netstat -i | egrep '(le|ie)' | awk '{print $1}'`

	echo "$INTERFACE"

	ASKIF="\nDo you want to use this interface ($INTERFACE) for DECnet"

	NUMINT=`echo $INTERFACE | wc -w`

	if [ $NUMINT -eq 1 ] 
	then
		askyesno_default "$ASKIF" y
		if [ $? -eq 0 ]
		then
			unset INTERFACE
		fi
	else
		unset INTERFACE
	fi

	if [ "$INTERFACE" = "" ]
	then
		get_string "$ASKINTERFACE" INTERFACE
	fi
}

# main ()

instructions

echo "\nPlease Hit Return to continue .....\c"
read ans; unset ans

#
# Get license from the user
#
get_license

if [ "$DNILICDATE" = "" ]
then
	DNILICDATE="1-jan-0"
fi

#
# Get rest of the stuff
#
ALLDONE=0
while [ $ALLDONE -eq 0 ]
do
	askall

	echo "\nYou have entered the following values:\n"
	echo "\tDECnet host name  : $DNIHOSTNAME"
	echo "\tDECnet area number: $DNIAREA"
	echo "\tDECnet node number: $DNINODE"
	echo "\tEthernet Interface: $INTERFACE"

	askyesno_default "$ASKCORRECT" y
	ALLDONE=$?
done

#
# Export Parameters
#
cat >$1 <<!
DNIINTERFACE='$INTERFACE'
DNIHOSTNAME='$DNIHOSTNAME'
DNIAREA='$DNIAREA'
DNINODE='$DNINODE'
DNILICENSE='$DNILICENSE'
DNILICDATE='$DNILICDATE'
!

