#!/bin/sh
#pragma ident	"@(#)postinstall	1.14	99/11/19 SMI"
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#

#
# Generate warn.conf entries. First argument is the number of fields
# 2nd argument is the operator (syslog|terminal|mail).
# The rest of the arguments are entries, which have to be split
# into lines.
#
wdump()
{
	fields=$1
	operator=$2
	shift
	shift

	fcnt=0
	for i in $*
	do
		fcnt=`expr $fcnt + 1`
		if [ $fcnt -gt $fields ]
		then
			fcnt=1
			echo
		fi

		echo "$i" '\c'
		if [ $fcnt -eq 1 ]
		then
			echo $operator '\c'

		fi
	done
	echo
}

tmp=/tmp/k$$
target=$PKG_INSTALL_ROOT/etc/krb5/krb5.conf

if [ $KRB5_CONF_REPLACE -eq 1 ]
then

	if [ "$KRB5_CONF_HELP_URL" = "" ]
	then
		echo `gettext 'ERROR: help URL was not specified.'`
		exit 1
	fi

	cat $target |
	sed "s/___default_realm___/$KRB5_CONF_REALM/g" |
	sed "s/___master_kdc___/$KRB5_CONF_MASTER/g" |
	sed "s/___slave_kdcs___/$KRB5_CONF_SLAVES/g" |
	sed "s/___domain_mapping___/$KRB5_CONF_DOM_MAP/g" |
	sed "s^___help_url___^$KRB5_CONF_HELP_URL^g" |
	awk '
	!/kdc =.*kdc =/	{ print $0 ; }
	/kdc =.*kdc =/	{
				for (i = 1 ; i <= NF ; i++) {
					if ($i == "kdc")
						printf "\t\t%s ", $i ;
					else if ($i == "=" )
						printf "%s ", $i ;
					else
						printf "%s\n", $i ;
				}
			}
	' > $tmp

	if [ $? = 0 ]
	then
		cp $tmp $target
		rm -f $tmp
	else
		echo
		printf "`gettext 'Configuration of %s failed.'`\n" $target
		echo
		rm -f $tmp
		exit 1
	fi

fi

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

if [ $WARN_CONF_REPLACE -eq 1 ]
then
	set -f
	if [ "$WARN_CONF_EMAILS" != "" ]
	then
		wdump 3 mail "$WARN_CONF_EMAILS" >> $target
	fi

	if [ "$WARN_CONF_SYSLOGS" != "" ]
	then
		wdump 2 syslog "$WARN_CONF_SYSLOGS" >> $target
	fi

	if [ "$WARN_CONF_TERMINALS" != "" ]
	then
		wdump 2 terminal "$WARN_CONF_TERMINALS" >> $target
	fi
	set +f
fi

target=$PKG_INSTALL_ROOT/etc/default/login
rm -f $tmp

if [ $LOGIN_PATH -eq 1 ]
then

	grep '^PATH=' $target > /dev/null 2>&1
	if [ $? = 0 ]
	then
		grep '^PATH=.*/usr/krb5/bin' $target > /dev/null 2>&1
		if [ $? = 1 ]
		then
			sed 's?^PATH=?PATH=/usr/krb5/bin:?' < $target > $tmp
		fi
	else
		cp $target $tmp
		echo PATH=/usr/krb5/bin:/usr/bin >> $tmp
	fi

	if [ -f $tmp ]
	then
		cp $tmp $target
		rm -f $tmp
	fi
fi

if [ $LOGIN_PATH -eq 1 ]
then

	grep '^SUPATH=' $target > /dev/null 2>&1
	if [ $? = 0 ]
	then
		grep '^SUPATH=.*/usr/krb5/bin' $target > /dev/null 2>&1
		if [ $? = 1 ]
		then
			sed 's?^SUPATH=?SUPATH=/usr/krb5/bin:?' < $target > $tmp
		fi
	else
		cp $target $tmp
		echo SUPATH=/usr/sbin:/usr/krb5/bin:/usr/bin >> $tmp
	fi

	if [ -f $tmp ]
	then
		cp $tmp $target
		rm -f $tmp
	fi
fi

if [ $LOGIN_SUPATH -eq 1 ]
then

	grep '^SUPATH=' $target > /dev/null 2>&1
	if [ $? = 0 ]
	then
		grep '^SUPATH=.*/usr/krb5/sbin' $target > /dev/null 2>&1
		if [ $? = 1 ]
		then
			sed 's?^SUPATH=.*$?&:/usr/krb5/sbin?' < $target > $tmp
		fi
	else
		cp $target $tmp
		echo SUPATH=/usr/sbin:/usr/bin:/usr/krb5/sbin >> $tmp
	fi

	if [ -f $tmp ]
	then
		cp $tmp $target
		rm -f $tmp
	fi
fi

target=$PKG_INSTALL_ROOT/etc/pam.conf
rm -f $tmp

if [ "$PAM_FIX_EDIT" -eq "1" ]
then
	pam_prefix="#SEAM "
	cat $target | sed "s/^#SEAM //" > $tmp
	cp $tmp $target
fi

target=$PKG_INSTALL_ROOT/etc/nfssec.conf
rm -f $tmp

cat $target > $tmp
grep '^krb5[ 	]' $tmp > /dev/null
if [ $? -ne 0 ]
then
	echo 'krb5 390003 kerberos_v5 default - #SUNWkr5cl' >> $tmp
fi

grep '^krb5i[ 	]' $tmp > /dev/null
if [ $? -ne 0 ]
then
	echo 'krb5i 390004 kerberos_v5 default integrity #SUNWkr5cl' >> $tmp
fi

grep '^krb5p[ 	]' $tmp > /dev/null
if [ $? -ne 0 ]
then
	echo 'krb5p 390005 kerberos_v5 default privacy #SUNWkr5cl' >> $tmp
fi

cp $tmp $target

rm -f $tmp

exit 0
