#ident	"@(#)pkg.nis:nis/request	1.28"
#ident  "$Header: $"
SCRIPTS=/usr/sbin/pkginst
UPGRADE_STORE=/var/sadm/upgrade
PKGMSG=/${PKGINST}.pkg export PKGMSG
LOCALE=${LC_ALL:-${LC_MESSAGES:-${LANG:-"C"}}}
PATH=/bin:/usr/bin:/usr/etc:/usr/sbin:$PATH
export PATH 
yproot_dir=/var/yp
hf=/tmp/ypservers #used by postinstall
clientp=F export clientp
masterp=F
slavep=F export slavep
host="" export host
def_dom="" export def_dom
host_type="" export host_type
master="" export master
ACCEPT="No" export ACCEPT
not_configured=1 
SERV1="" export SERV1
SERV2="" export SERV2
SERV3="" export SERV3
CONFIG_NOW="No" export CONFIG_NOW
NISOVERLAY=/tmp/nis.overlay export NISOVERLAY
PKGADD_FILE=$1
NIS_RESP=$1 export NIS_RESP
nis_setup_exists="-d $yproot_dir/binding -a -d $yproot_dir/binding/* -o -f $yproot_dir/binding"
INITSCRIPT=/etc/init.d/nis
USE_CURRENT="No" export USE_CURRENT
TYPE="" export TYPE
badhosts=/tmp/.nishosts export badhosts

ckhost()
{
	#
	# Now check to see if the host is in /etc/hosts
	#
	if [ -f /etc/inet/hosts ]
	then
		found=`cat /etc/inet/hosts | sed '/^#/d' | nawk '
			BEGIN {found=0}
				host == $2 {found=1; exit 0}
			END {print found}' host=$1`
	fi
	return $found
}

cleanup()
{
rm -f $MENU_RESP
rm -f $NISOVERLAY
rm -f $hf
rm -f $badhosts

}

savemv()
{
	trap "" 1 2 3 15
	mv -f $1 $2 > /tmp/nis.config 2>&1
	if [ $? -ne 0 ]; then
		error 4 "Unable to create %s\n" $2
	fi
	trap 1 2 3 15
}

error()
{
	
	export error_string 
	export mgs_num 
	msg_num="$1" 
	error_string="`/usr/bin/pfmt -s error -g ${PKGMSG}:$1 "$2" 2>&1`" $3
	. /etc/inst/locale/C/menus/menu_colors.sh error > /dev/null 2>&1
	menu_colors error
	menu -f ${MENU_DIR}/menu.err -o /dev/null 2>/dev/null
	menu_colors regular
}
setdomain()
{
	def_dom=`domainname` 2>/tmp/nis.config
	if [ $? -ne 0 ]
	then 
		error 3 "Can not get local host's domain name. \n"
		cleanup
		exit 1
	fi
	menu -f ${MENU_DIR}/menu.5 -o ${MENU_RESP} 2>/dev/null
	cat ${MENU_RESP}  >> ${NIS_RESP}
	. ${MENU_RESP}
}

initnis()
{

host=`uname -n` 2>/tmp/nis.config
notdone=1
while [ $notdone = 1 ]
do
menu -f ${MENU_DIR}/menu.2 -o ${MENU_RESP} 2>/dev/null
. ${MENU_RESP}
	case "$TYPE" 
	in
		1 )
			masterp=T
			ismaster=1
			isserver=1
			notdone=0
			host_type="master server"
			;;
		2 )
			menu -f ${MENU_DIR}/menu.2.1 -o ${MENU_RESP} 
			cat ${MENU_RESP}  >> ${NIS_RESP}
			. ${MENU_RESP}
			if [ ! -z "$master" ]; then
				if [ "$host" =  "$master" ]; then
					. /etc/inst/locale/C/menus/menu_colors.sh warn 1>/dev/null 2>&1
					menu_colors warn
					menu -f ${MENU_DIR}/menu.2.1.1 -o ${MENU_RESP} 
					menu_colors regular
					continue
				fi
				slavep=T
				isserver=1
				notdone=0
			fi
			host_type="slave server"
			;;
		3 )
			clientp=T
			notdone=0
			host_type="client"
			;;
		* )     continue
			;;
	esac
done
cat ${MENU_RESP}  >> ${NIS_RESP}
}
#
# get current NIS configuration 
#
getnisconfig()
{
/sbin/rm -rf $NISOVERLAY
def_dom=`/usr/bin/domainname`
echo "def_dom=$def_dom" >> $NIS_RESP
if [ $nis_setup_exists ]
then
	if [ -f $yproot_dir/binding/$def_dom/ypservers ]; then
		
		#
		# Just print out the first 3 servers onto the menu
		#
		sed  -n '1,3p' $yproot_dir/binding/$def_dom/ypservers >> $NISOVERLAY 
		echo "\n \n" >> $NISOVERLAY
	else
		#
		# if /var/yp/binding/$def_dom/ypservers is not present 
		# then can not use current configuarion
		#
		return 1
	fi
	if [ -f $INITSCRIPT ]
	then
		/sbin/grep "^domain" $INITSCRIPT >> $NIS_RESP
		/sbin/grep "^isserver" $INITSCRIPT >> $NIS_RESP
		/sbin/grep "^ismaster" $INITSCRIPT >> $NIS_RESP
		/sbin/grep "^startypbind" $INITSCRIPT >> $NIS_RESP
		/sbin/grep "^startypserv" $INITSCRIPT >> $NIS_RESP
		/sbin/grep "^startyppass" $INITSCRIPT >> $NIS_RESP
	else
		#
		# if /etc/init.d/nis is not present then can not use
		# current configuarion
		#
		return 1
	fi
	
	. $NIS_RESP
	if [ $isserver = "1" ]
	then
		if [ $ismaster = "1" ]
		then
			TYPE=1
		else
			TYPE=2
		fi
	else
		TYPE=3
	fi
	echo "TYPE=$TYPE" >> $NIS_RESP
	/usr/sbin/menu -f ${MENU_DIR}/menu.ol -o ${MENU_RESP} 
	cat ${MENU_RESP}  >> ${NIS_RESP}
	. ${MENU_RESP} 
	rm -rf $NISOVERLAY

	if [ "$USE_CURRENT" = "No" ]
	then
		return 1 # do not use current configuration
	else
		# save some volatile files
		mv /var/yp/YPMAPS /var/yp/YPMAPS.old 2>/dev/null
		mv /var/yp/aliases /var/yp/aliases.old 2>/dev/null
		mv /var/yp/Makefile /var/yp/Makefile.old 2>/dev/null
		return 0 # use current configuration
	fi
else
	return 1 # no valid configuration found
fi
}

. ${SCRIPTS}/updebug
[ "$UPDEBUG" = YES ] && set -x

#
# Check to see if we are being called from boot floppy
#
if [ -f /etc/inst/scripts/postreboot.sh ]
then
	mkdir /tmp/nis
	cp ${REQDIR}/request /etc/inst/scripts/nisrequest
	cp ${REQDIR}/postinstall /etc/inst/scripts/nispostinst
	chmod +x /etc/inst/scripts/nisrequest
	chmod +x /etc/inst/scripts/nispostinst
	exit 0
fi
# We are rebooting after fnd set installation if the script
# /etc/rc2.d/S02POSTINST exists
if [ ! -f /etc/rc2.d/S02POSTINST ] 
then
	# if we make it this far, postinstall must be called from pkagadd
	NIS_RESP=/tmp/nis.resp
fi

#
# menu stuff
#
MENU_DIR=/etc/inst/locale/${LOCALE}/menus/nis
PKG_MENU=${REQDIR}/nis
REQ_MENU=${PKG_MENU}
MENU_RESP=/tmp/nisout
[ -d ${MENU_DIR} ] || mkdir -p ${MENU_DIR}
MENUS="menu.1 menu.2 menu.2.1 menu.2.1.1 menu.4 menu.5 menu.err menu.wk menu.ck menu.ol menu.hosts"
for i in $MENUS
do
	if [ ! -f ${MENU_DIR}/$i ]
	then
		if [ ! -f ${PKG_MENU}/$i ]
		then
			cp ${PKG_MENU}/$i ${MENU_DIR}
			chgrp sys ${MENU_DIR}/$i
		else
			cp ${REQ_MENU}/$i ${MENU_DIR}
			chgrp sys ${MENU_DIR}/$i
		fi
	fi
done
#
#  Set up to use menu_colors; default to C-locale if ${LANG}'s dir has
#  no menu_colors.sh
#
if [ -f /etc/inst/locale/${LOCALE}/menus/menu_colors.sh ]
then
	. /etc/inst/locale/${LOCALE}/menus/menu_colors.sh 1> /dev/null 2>&1
else
	. /etc/inst/locale/C/menus/menu_colors.sh 1> /dev/null 2>&1
fi

menu_colors regular
#
# Create in message catalogs
#
if [ ! -f /usr/lib/locale/${LOCALE}/LC_MESSAGES/${PKGMSG} ]
then
   if [ -f ${REQDIR}/inst/locale/${LOCALE}/${PKGMSG} -a \
	-d /usr/lib/locale/${LOCALE}/LC_MESSAGES ]
   then
	cp ${REQDIR}/inst/locale/${LOCALE}/${PKGMSG} \
	   /usr/lib/locale/${LOCALE}/LC_MESSAGES
   fi
fi

#
# This is the main loop of the script
#
while [ $not_configured = 1 ]
do

############# Begin UPGRADE AND OVERLAY #######################
${SCRIPTS}/chkpkgrel $PKGINST
PKGVERSION=$?

case $PKGVERSION in
	4)	PKGINSTALL_TYPE=UPGRADE	;;
	2)	PKGINSTALL_TYPE=OVERLAY	;;
	*)	unset PKGINSTALL_TYPE	;;
esac
#
# Check to see if NIS is installed and initialized.
# If so, get the current NIS configuration
# otherwise, initialize NIS. getnisconfig() returns 0 if a
# valid configuration was found.
#
# the NIS package did not exist before update 6 of uw1.1.
# Upgrade will then be anything from update 6 to uw2.0 or later
# Overlay will be update  6 to update 7 etc., uw2.0 to uw2.1.
# In both cases the actions to be take are the same for now.
#
if [ $PKGINSTALL_TYPE=UPGRADE ] || [ $PKGINSTALL_TYPE=OVERLAY ]
then
	getnisconfig
	if [ "$?" = "0" ]
	then
		if [ $USE_CURRENT = "Yes" ]
		then
			echo "CONFIG_NOW=Yes" >> $NIS_RESP
			cp $NIS_RESP $PKGADD_FILE 2>/dev/null
			exit 0
		fi
	fi
fi
############# End UPGRADE AND OVERLAY #######################

#
# Find out if user wants to configure NIS now
#
menu -f ${MENU_DIR}/menu.1 -o ${MENU_RESP} 
. ${MENU_RESP}
if [ $CONFIG_NOW = "No" ]
then
	echo "CONFIG_NOW=No" >> $NIS_RESP
	cp $NIS_RESP $PKGADD_FILE 2>/dev/null
	not_configured=0
	continue
fi

initnis
host=`uname -n` 2>/tmp/nis.config

if [ $? -ne 0 ]
then 
	error 1 "Can not determine local host's name. \n"
	cleanup
	exit 1
fi

if [ -z "$host" ]
then
	error 2 "The local host's name has not been set. Please set it. \n"
	cleanup
	exit 1
fi

setdomain
binding_dir="$yproot_dir""/binding/""$def_dom"
binding_file="$yproot_dir""/binding/""$def_dom""/ypservers"


if [ ! -d "$yproot_dir" ]
then
	mkdir -p $yproot_dir 2>/dev/null
fi

if [ ! -d "$binding_dir" ]
then
	mkdir -p $binding_dir 2>/dev/null
fi

if [ ! -d  $binding_dir ]
then	
    error 12 " The directory %s does not exist. Restore it from the distribution." $binding_dir
	cleanup
	exit 1
fi

if [ $slavep = F ]
then
	rm -f $badhosts
	rm -f $hf
	more_servers="Yes"
	first_loop="yes" export first_loop
	if [ $TYPE = "1" ] 
	then
		SERVER1=`uname -n`
		export SERVER1
	fi
	if [ $TYPE = "3" ] 
	then
		SERVER1=""
		export SERVER1
	fi
	while [ $more_servers = "Yes" ]; do
	menu -f ${MENU_DIR}/menu.4 -o ${MENU_RESP}
	. ${MENU_RESP}
		for i in SERVER1 SERVER2 SERVER3 SERVER4 SERVER5
		do
			eval arg=$"$i"
			if [  "$arg" != "" ]
			then 
				echo $arg >> $hf
				ckhost $arg
				if [ "$?" = "0" ]
				then
					echo $arg >> $badhosts
				fi
			fi
			if [ $first_loop = "yes" ]
			then
				if [ "$i" = "SERVER1" ]
				then
					SERV1=$arg
				fi
				if [ "$i" = "SERVER2" ]
				then
					SERV2=$arg
				fi
				if [ "$i" = "SERVER3" ]
				then
					SERV3=$arg
				fi
			fi
		done
		first_loop="no"
		SERVER1=""
	done
else # this is a slave host
	rm -f $badhosts
	rm -f $hf
	echo $master >> $hf
	echo $host  >> $hf
	ckhost $master
	if [ "$?" = "0" ]
	then
		echo $master >> $badhosts
	fi
fi
	
#
# check to see if hosts were entered that were not
# in /etc/hosts
#
if [ -f $badhosts ]
then
menu_colors warn
menu -f ${MENU_DIR}/menu.hosts -o /dev/null 2>/tmp/error
menu_colors regular
fi
	
menu -f ${MENU_DIR}/menu.ck -o ${MENU_RESP}
. ${MENU_RESP}
if [ $ACCEPT != "Yes" ]
then
	master=""
	slavep=F
	client=F
	SERV1=""
	SERV2=""
	SERV3=""
	not_configured=1;
else
	domainname $def_dom
	cp  $hf $binding_file
	rm -f $badhosts
	if [ -x /sbin/chlvl ]
	then
		chlvl SYS_PUBLIC $binding_file
	fi
	echo "CONFIG_NOW=Yes" >> $NIS_RESP
	not_configured=0;
fi
cp $NIS_RESP $PKGADD_FILE 2>/dev/null
done

