#!/bin/sh
#pragma ident	"@(#)request	1.11	99/05/29 SMI"
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
trap 'exit 3' 15

wdump()
{
	if [ $# -gt 2 ] && [ "$3" != "" ]
	then
		printf '\n%s\n-----------------------------------------\n' "$1"
		fields=$2
		shift
		shift

		fcnt=0
		for i in $*
		do
			fcnt=`expr $fcnt + 1`
			if [ $fcnt -gt $fields ]
			then
				fcnt=1
				printf "\n"
			fi
			printf "\t%s"  "$i"
		done
		printf "\n\n"
	fi
}

getwtime()
{
	getans2 $1 "`gettext 'Enter warning time %s: '`" "$2"
	while true ;
	do
		case $ans in
		*[hms]	)
				break
				;;

		*	)
				printf \
 "`gettext 'Warning time must indicate seconds, minutes, or hours with a suffix of s, m, or h.'`\n"
			getans2 $1 "`gettext 'Enter warning time %s: '`" "$2"
				;;
		esac
	done
}

getans2()
{
	def=$1
	# need to get the format string because it must be
	# quoted
	fmt=$2
	shift
	shift
	p1=""
	p2=""
	if [ $# -gt 0 ]
	then
		p1="$1"
		shift
		if [ $# -gt 0 ]
		then
			p2="$1"
			shift
			printf "$fmt" "$p1" "$p2" $*
		else
			printf "$fmt" "$p1" $*
			
		fi
	else
		printf "$fmt" $*
	fi

	read ans

	if [ "$ans" = "" ]
	then
		ans=$def
	fi
}

getans()
{
	# need to get the format string because it must be
	# quoted
	fmt=$1
	shift

	printf "$fmt" $*

	while true ;
	do
		read ans

		if [ "$ans" != "" ]
		then
			break
		else
			printf "$fmt" $*
		fi

	done
}

getYN()
{
	# need to get the format string because it must be
	# quoted
	fmt=$1
	yval=$2
	nval=$3
	q2=$4
	shift 4

	printf "$fmt" $*

	while true ;
	do
		read ans

		case $ans in

		$yval	)
			YN=1
			break
			;;

		$nval	)
			YN=0
			break
			;;

		*	)
			echo $q2 '\c'

		esac
	done
}

cf=$PKG_INSTALL_ROOT/etc/krb5/krb5.conf

KRB5_CONF_REPLACE=1
if [ -f $cf ]
then
	echo
	echo
	getYN "`gettext 'Note: %s already exists.\nDo you wish to generate a new %s interactively? (y)es/(n)o: `" y n \
		"`gettext 'Answer y for yes or n for no:'`" $cf $cf
	if [ $YN -eq 0 ]
	then
		KRB5_CONF_REPLACE=0
	fi
fi

cf=$PKG_INSTALL_ROOT/etc/krb5/warn.conf

WARN_CONF_REPLACE=1
if [ -f $cf ]
then
	echo
	echo
	getYN "`gettext 'Note: %s already exists.\nDo you wish to generate a new %s interactively? (y)es/(n)o: `" y n \
		"`gettext 'Answer y for yes or n for no:'`" $cf $cf
	if [ $YN -eq 0 ]
	then
		WARN_CONF_REPLACE=0
	fi
fi

while true ;
do
	echo
	echo 

	if [ $KRB5_CONF_REPLACE -eq 1 ]
	then
		getans "`gettext 'Please specify the default Kerberos V5 realm: '`"
		realm=`echo $ans | tr "[:lower:]" "[:upper:]"`

		getYN "`gettext 'Is realm %s also a DNS domain? (y)es/(n)o: '`" y n \
			"`gettext 'Answer y for yes or n for no:'`" $realm

		if [ $YN -eq 0 ]
		then
			getans "`gettext 'Please specify the DNS domain that maps to realm %s: '`" $realm
			case $ans in
			.*	)
				;;
			*	)
				ans=".$ans"
				;;
			esac
			

			KRB5_CONF_DOM_MAP="$ans = $realm"
			
		else
			KRB5_CONF_DOM_MAP=""
		fi

		getans "`gettext 'Please specify the master KDC for realm %s: '`" $realm
		master=$ans

		getans "`gettext 'Please specify a slave KDC for realm %s: '`" $realm
		slave="`echo $ans | sed 's/, 	//g' `"
		slaves=""
		slaves="$slaves $slave"

		while true ;
		do
			getYN "`gettext 'Are there more slave KDCs for realm %s? (y)es/(n)o: '`" y n \
				"`gettext 'Answer y for yes or n for no:'`" $realm

			if [ $YN -eq 1 ]
			then
				getans "`gettext 'Please specify another slave KDC for realm %s: '`" $realm
				slave="`echo $ans | sed 's/, 	//g' `"
				slaves="$slaves $slave"
			else
				break
			fi
		done

		help_url="`gettext 'http://not_known_yet'`"
		getans2 $help_url "`gettext 'Please specify the URL for online help [%s]: '`" $help_url
		help_url=$ans

	fi

	WARN_CONF_EMAILS=""
	WARN_CONF_TERMINALS=""
	WARN_CONF_SYSLOGS=""

	if [ $WARN_CONF_REPLACE -eq 1 ]
	then
		printf "`gettext 'The following questions about warnings apply to ticket expiration warnings\nin the warn.conf file. '`\n"
		getYN "`gettext 'Do you wish to add e-mail warnings? (y)es/(n)o: '`" y n \
			"`gettext 'Answer y for yes or n for no:'`"
		while [ $YN -eq 1 ]
		do
			getans "`gettext 'Enter principal name: '`"
			princ=$ans
			getans2 $princ \
"`gettext 'Enter e-mail address for principal %s [%s]: '`" $princ $princ
			email=$ans

			getwtime 1h "`gettext '[1h == One hour]'`"
			wtime=$ans

			WARN_CONF_EMAILS="$WARN_CONF_EMAILS $princ $wtime $email"
			getYN "`gettext 'Do you wish to add more e-mail warnings? (y)es/(n)o: '`" y n \
			"`gettext 'Answer y for yes or n for no:'`"
		done
		getYN "`gettext 'Do you wish to add syslog warnings? (y)es/(n)o: '`" y n \
			"`gettext 'Answer y for yes or n for no:'`"
		while [ $YN -eq 1 ]
		do
			getans2 '*' "`gettext 'Enter principal name [%s == %s]: '`" '*' "`gettext 'Wild card'`"
			princ=$ans

			getwtime 1h "`gettext '[1h == One hour]'`"
			wtime=$ans

			WARN_CONF_SYSLOGS="$WARN_CONF_SYSLOGS $princ $wtime"
			getYN "`gettext 'Do you wish to add more syslog warnings? (y)es/(n)o: '`" y n \
				"`gettext 'Answer y for yes or n for no:'`"
		done
		getYN "`gettext 'Do you wish to add terminal warnings? (y)es/(n)o: '`" y n \
			"`gettext 'Answer y for yes or n for no:'`"
		while [ $YN -eq 1 ]
		do
			getans2 '*' "`gettext 'Enter principal name [%s == %s]: '`" '*' "`gettext 'Wild card'`"
			princ=$ans

			getwtime 15m "`gettext '[15m == 15 minutes]'`"
			wtime=$ans

			WARN_CONF_TERMINALS="$WARN_CONF_TERMINALS $princ $wtime"
			getYN "`gettext 'Do you wish to add more terminal warnings? (y)es/(n)o: '`" y n \
				"`gettext 'Answer y for yes or n for no:'`"
		done
		
	fi

	getYN \
"`gettext 'Do you wish to prepend %s to the default PATH variable? (y)es/(n)o: '`" y n \
		"`gettext 'Answer y for yes or n for no:'`" /usr/krb5/bin

	LOGIN_PATH=$YN
	getYN \
"`gettext 'Do you wish to append %s to the default PATH variable for root? (y)es/(n)o: '`" y n \
		"`gettext 'Answer y for yes or n for no:'`" /usr/krb5/sbin

	LOGIN_SUPATH=$YN

	echo
	echo
	echo `gettext 'You have selected:'`
	echo
	if [ $KRB5_CONF_REPLACE -eq 1 ]
	then
		printf "`gettext 'A Kerberos V5 realm called %s'`\n" $realm 
		if [ "$KRB5_CONF_DOM_MAP" = "" ]
		then
			printf "`gettext 'Realm %s is a DNS domain.'`\n" \
				"$realm"
		else
			printf "`gettext 'A domain to realm mapping of %s'`\n" \
				"$KRB5_CONF_DOM_MAP"
		fi
		printf "`gettext 'A master KDC called %s'`\n" $master 
		printf "`gettext 'One or more slave KDCs: %s'`\n" "$slaves"
		printf "`gettext 'A help URL of %s'`\n" $help_url
	fi

	if [ $WARN_CONF_REPLACE -eq 1 ]
	then
		set -f
		wdump "`gettext 'These E-mail warnings:'`" 3 \
				"$WARN_CONF_EMAILS"

		wdump "`gettext 'These Syslog warnings:'`" 2 \
				"$WARN_CONF_SYSLOGS"

		wdump "`gettext 'These Terminal warnings:'`" 2 \
				"$WARN_CONF_TERMINALS"
		set +f

	fi

	if [ $LOGIN_PATH -eq 1 ]
	then
		printf \
"`gettext 'Prepending %s to the default PATH variable.'`\n" /usr/krb5/bin 
	else
		printf \
"`gettext 'Not prepending %s to the default PATH variable.'`\n" /usr/krb5/bin 

	fi

	if [ $LOGIN_SUPATH -eq 1 ]
	then
		printf \
"`gettext 'Appending %s to the default root PATH variable.'`\n" /usr/krb5/sbin 
	else
		printf \
"`gettext 'Not appending %s to the default root PATH variable.'`\n" \
	/usr/krb5/sbin 

	fi

	echo
	getYN "`gettext 'Are the above selections correct? (y)es/(n)o: '`" \
		y n "`gettext 'Answer y for yes or n for no.'`"

	if [ $YN -eq 1 ]
	then
		break
	fi
done


KRB5_CONF_SLAVES=""
for i in $slaves
do
        KRB5_CONF_SLAVES="${KRB5_CONF_SLAVES} kdc = ${i}"
done

echo
echo

cat > $1<<!
SUNWKR5CL_REQUEST_VERS=3
KRB5_CONF_REPLACE=$KRB5_CONF_REPLACE
KRB5_CONF_REALM=$realm
KRB5_CONF_MASTER=$master
KRB5_CONF_SLAVES="$KRB5_CONF_SLAVES"
KRB5_CONF_DOM_MAP="$KRB5_CONF_DOM_MAP"
KRB5_CONF_HELP_URL=$help_url
WARN_CONF_REPLACE=$WARN_CONF_REPLACE
WARN_CONF_EMAILS="$WARN_CONF_EMAILS"
WARN_CONF_SYSLOGS="$WARN_CONF_SYSLOGS"
WARN_CONF_TERMINALS="$WARN_CONF_TERMINALS"
LOGIN_PATH=$LOGIN_PATH
LOGIN_SUPATH=$LOGIN_SUPATH
PAM_EDIT=1
!
exit 0
