#ident	"@(#)pkg.nsu:postinstall	1.20.1.4"
#
#	postinstall script for nsu package
#
# error function - put out a slightly more descriptive error message
#	arg 1 is message #
#	arg 2 is optional argument for message
#
error() {
	errflag=1
	case $1 in
	1)
		echo ERROR: drvinstall on $2 failed
		;;
	2)
		echo ERROR: could not create $2
		;;
	3)
		echo ERROR: could not create pseudo-terminal slave devices
		;;
	4)
		echo ERROR: copy failed
		;;
	5)
		# message put out elsewhere
		;;
	6)
		echo ERROR: installation error
		;;
	7)
		echo ERROR:  mkboot on KERNEL failed
		;;
	*)
		echo ERROR: undefined error
		;;
	esac
	}
errflag=0
BOOT="$BASEDIR/boot"
MASTER="$BASEDIR/etc/master.d"
SYSTEM="$BASEDIR/etc/system"
#
#	configure drivers and note major numbers
#
CMAJOR=`/etc/drvinstall -m $MASTER/clone -s $SYSTEM -o $BOOT -v 1.0` \
	|| error 1 CLONE

#	configure kma accounting driver
KMAJOR=`/etc/drvinstall -m $MASTER/kmacct -s $SYSTEM -o $BOOT -v 1.0` \
	|| error 1 KMACCT
installf $PKGINST $BASEDIR/dev/kmacct c $KMAJOR 0 \
	0644 root other || error 2 $BASEDIR/dev/kmacct

#
/etc/drvinstall -m $MASTER/sockmod -s $SYSTEM -o $BOOT -v 1.0 -c 128 \
	>/dev/null || error 1 SOCKMOD
/etc/drvinstall -m $MASTER/timod  -s $SYSTEM -o $BOOT -v 1.0 -c 128 \
	>/dev/null || error 1 TIMOD
/etc/drvinstall -m $MASTER/tirdwr -s $SYSTEM -o $BOOT -v 1.0 -c 16 \
	>/dev/null || error 1 TIRDWR
/etc/drvinstall -m $MASTER/pckt -s $SYSTEM -o $BOOT -v 1.0 >/dev/null || error 1 PCKT
/etc/drvinstall -m $MASTER/connld -s $SYSTEM -o $BOOT -v 1.0 >/dev/null || error 1 CONNLD

#
#	The following is done only if PTNUM > 0
#
if [ "$PTNUM" -gt 0 ]
    then
	#
	#	(re-)configure pseudo-terminal master device
	#
	MMAJOR=`/etc/drvinstall -m $MASTER/ptm -s $SYSTEM -o $BOOT \
		-v 1.0 -c $PTNUM` || error 1 PTM
	SMAJOR=`/etc/drvinstall -m $MASTER/pts -s $SYSTEM -o $BOOT \
		-v 1.0`	|| error 1 PTS
	/etc/drvinstall -m $MASTER/ptem -s $SYSTEM -o $BOOT \
		-v 1.0 -c $PTNUM >/dev/null || error 1 PTEM
	installf $PKGINST $BASEDIR/dev/ptmx c $CMAJOR $MMAJOR \
		0666 root other || error 2 $BASEDIR/dev/ptmx
	#
	#	configure pseudo-terminal slave devices
	#
	installf $PKGINST /dev/pts d 775 root sys || error 2 /dev/pts
	TEMP=0
	until [ "$TEMP" -eq "$PTNUM" ]
	do
	    echo $BASEDIR/dev/pts/$TEMP c $SMAJOR $TEMP \
		0620 root tty
	    echo $BASEDIR/dev/pts$TEMP=$BASEDIR/dev/pts/$TEMP
	    TEMP=`expr $TEMP + 1`
	done | installf $PKGINST -
	if [ $? -ne 0 ]
	    then
		error 3
	    fi
	fi

#
#	configure loopback transport providers
#

NTICLTS=128
TICLTSMAJOR=`/etc/drvinstall -m $MASTER/ticlts -s $SYSTEM -o $BOOT \
	-v 1.0 -c $NTICLTS` || error 1 TICLTS
installf $PKGINST $BASEDIR/dev/ticlts c $CMAJOR $TICLTSMAJOR \
	0666 root other || error 2 $BASEDIR/dev/ticlts

NTICOTS=128
TICOTSMAJOR=`/etc/drvinstall -m $MASTER/ticots -s $SYSTEM -o $BOOT \
	-v 1.0 -c $NTICOTS` || error 1 TICOTS
installf $PKGINST $BASEDIR/dev/ticots c $CMAJOR $TICOTSMAJOR \
	0666 root other || error 2 $BASEDIR/dev/ticots

NTICOTSORD=128
TICOTSORDMAJOR=`/etc/drvinstall -m $MASTER/ticotsord -s $SYSTEM -o $BOOT \
	-v 1.0 -c $NTICOTSORD` || error 1 TICOTSORD
installf $PKGINST $BASEDIR/dev/ticotsord c $CMAJOR $TICOTSORDMAJOR \
	0666 root other || error 2 $BASEDIR/dev/ticotsord

if [ ! -s /etc/netconfig ]
then
	echo "#
#	The Network Configuration File.
#
# Each entry is of the form:
#
# network_id semantics flags protofamily protoname device nametoaddr_libs
#" >/etc/netconfig
fi

if grep '[ 	]loopback[ 	][ 	]*-[ 	][ 	]*/dev/ticlts[ 	]' /etc/netconfig >/dev/null 2>&1
then
	: Already there!
else
	echo "ticlts	   tpi_clts	  v	loopback	-	/dev/ticlts	/usr/lib/straddr.so" >>/etc/netconfig
fi

if grep '[ 	]loopback[ 	][ 	]*-[ 	][ 	]*/dev/ticots[ 	]' /etc/netconfig >/dev/null 2>&1
then
	: Already there!
else
	echo "ticots	   tpi_cots	  v	loopback	-	/dev/ticots	/usr/lib/straddr.so" >>/etc/netconfig
fi

if grep '[ 	]loopback[ 	][ 	]*-[ 	][ 	]*/dev/ticotsord[ 	]' /etc/netconfig >/dev/null 2>&1
then
	: Already there!
else
	echo "ticotsord  tpi_cots_ord	  v	loopback	-	/dev/ticotsord	/usr/lib/straddr.so" >>/etc/netconfig
fi

#
#	install listen id (if not there) and fix up files
#
/usr/bin/passmgmt -a -u 37 -g 4 -c "Network Admin" -h /usr/net/nls listen \
	>/dev/null 2>&1
STEM=$BASEDIR/etc/saf
sacadm -L -t listen | cut -f1 -d: >/tmp/listeners
#
#	convert the listener files that are already under SAF
#	control
#
for i in `cat /tmp/listeners`
do
	if [ -f $STEM/$i/_pmtab ]
	then
		cp $STEM/$i/_pmtab $STEM/$i/_opmtab || error 4
		/usr/lib/saf/dbfconv $STEM/$i/_opmtab $STEM/$i/_pmtab
		if [ $? -ne 0 ]
		then
			error 5
			echo "ERROR: listener database conversion failed"
		fi
	fi
done
#
#	now convert any old style listeners into new format
#
sacadm -L | cut -f1 -d: >/tmp/pms
LHOME=$BASEDIR/usr/net/nls
for i in $LHOME/*
do
	NETSPEC=`basename $i`
	if test -d $i
	then
		grep $NETSPEC /tmp/listeners >/dev/null
		if [ $? -eq 0 ]
		then
#
#	old listener has already been converted
#
			continue
		fi
#
#	does another port monitor exist with this name?
#
		grep $NETSPEC /tmp/pms >/dev/null
		if [ $? -eq 0 ]
		then
			error 5
			echo "ERROR: can not convert $NETSPEC listener to new format, tag already exists"
			continue
		fi
#
#	ok to create new port monitor
#
		COMMAND=/usr/lib/saf/listen
		if [ $NETSPEC = starlan ]
		then
			COMMAND="$COMMAND -m slan"
		fi
		sacadm -a -p $NETSPEC -t listen -c "$COMMAND $NETSPEC" -v `/usr/sbin/nlsadmin -V` -y "$NETSPEC listener"
		if [ $? -ne 0 ]
		then
			error 5
			echo "ERROR: can not create new port monitor entry for $NETSPEC"
			continue
		fi
#
#	convert the database file and put it in the right place
#
		if test -f $i/dbf
		then
			/usr/lib/saf/dbfconv $i/dbf $STEM/$NETSPEC/_pmtab
			if [ $? -ne 0 ]
			then
				error 5
				echo "ERROR: listener database conversion failed"
			else
				sacadm -x -p $NETSPEC
			fi
		fi
	fi
done
rm /tmp/listeners
rm /tmp/pms
#
# leave symbolic links around for compatibility
#
if [ -f $LHOME/listen ]
then
        rm $LHOME/listen
        ln -s $BASEDIR/usr/lib/saf/listen $LHOME/listen
fi
if [ -f $LHOME/dbfconv ]
then
        rm $LHOME/dbfconv
        ln -s $BASEDIR/usr/lib/saf/dbfconv $LHOME/dbfconv
fi
if [ -f $LHOME/nlps_server ]
then
        rm $LHOME/nlps_server
        ln -s $BASEDIR/usr/lib/saf/nlps_server $LHOME/nlps_server
fi
#
#	finished installing files: tell OAM
#
installf -f $PKGINST || error 6
#
#	make a new UNIX(R)
#
touch $SYSTEM
FREE=`expr "\`df / 2>/dev/null\`" : '.*: *\([0-9]*\)'`
NEED=`du -s $BOOT/KERNEL | cut -f1`
if [ "$NEED" -gt "$FREE" ]
    then
	echo "postinstall: ERROR: not enough space for mkboot"
	echo "	TOFIX: free space and then reinstall package."
	exit 1
fi
/etc/mkboot -m $MASTER -d $BOOT -k $BOOT/KERNEL || error 7
#
#	reboot required to get changes
#
errflag=`expr ${errflag:=0} + 10`
exit $errflag
