#
#       Copyright (c) 1999 Sun Microsystems, Inc.
#               All Rights Reserved
#
# @(#)postinstall	3.12 99/12/17 Sun Microsystems
#

# Add our TCP service descriptions to /etc/inet/services.
services=${PKG_INSTALL_ROOT}/etc/inet/services
if grep -w "385[235]/tcp" $services 1>/dev/null 2>&1 ; then
	echo "## SunScreen installation error: TCP services already in use:"
	grep -w "385[235]/tcp" $services
	echo "## aborting SunScreen installation"
	echo "## SunScreen installation error: TCP services already in use:" > /dev/console
	grep -w "385[235]/tcp" $services >/dev/console
	exit 1
else
	echo "ss_gui\t\t3852/tcp\t\t\t# SunScreen administration GUI HTTP; not configurable" >> $services
	echo "ss_ssadm\t3853/tcp\t\t\t# SunScreen remote administration commands; not configurable" >> $services
	echo "ss_securid\t3855/tcp\t\t\t# SunScreen SecurID PIN maintenance; not configurable" >>  $services
fi

# Add our file format descriptions to /etc/magic.
magic=${PKG_INSTALL_ROOT}/etc/magic
if grep SunScreenConfigurationBackup $magic >/dev/null 2>&1
then :
else
sed '
/USTAR/a\
>0	string	SunScreenConfigurationBackup.	 (SunScreen configuration backup)
$a\
# Begin SunScreen files\
257	string		ustar		USTAR tar archive\
>0	string	SunScreenConfigurationBackup.	 (SunScreen configuration backup)\
0	string	//SunScreenConfig	SunScreen firewall configuration\
0	string	T\\206\\225#		SunScreen packet log file version 0\
0	string	SunScreen\\040new\\040log\\n\\n	SunScreen packet log file\
>23	byte	>0				version %d\
>20	byte	>0				version %d byte-swapped\
0	string	SunScreen\\040log\\040file\\n	SunScreen packet log file\
>23	byte	>0				version %d\
>20	byte	>0				version %d byte-swapped\
# End SunScreen files
' $magic > $magic.new
cp $magic.new $magic
rm $magic.new
fi

# Work around SKIP not adding its driver properly to avoid reboot.
if [ "${PKG_INSTALL_ROOT-/}" = / ] &&
   [ ! -c /dev/skip_key ] &&
   grep skip_key /etc/name_to_major >/dev/null &&
   pkginfo SUNWbdc >/dev/null 2>&1
then
	echo "It looks like SKIP is installed but the skip_key driver isn't loaded."
	echo "I'll see if I can remedy this by running rem_drv and add_drv..."
	rem_drv skip_key
	add_drv skip_key
	if [ -c /dev/skip_key ]
	then 
		echo "Ah, things look much better now."
	else
		echo "Hmm, I don't know if this is going to work."
		echo "Configuring skip_key driver in /devices and /dev."
		drvconfig -i clone
		devlinks
	fi
fi

# Add our drivers.
if [ -f ${PKG_INSTALL_ROOT}/kernel/drv/screen ]
then	echo "## Adding screen driver ##"
	case ${PKG_INSTALL_ROOT} in
	""|/)	add_drv screen
		if [ ! -c /dev/screen ]
		then
			echo "Configuring screen driver in /devices and /dev."
			drvconfig -i clone
			devlinks
		fi
		;;
	*)	add_drv -b ${PKG_INSTALL_ROOT} screen;;
	esac
fi

if [ -f ${PKG_INSTALL_ROOT}/usr/kernel/drv/screen_skip ]
then	echo "## Adding screen_skip driver ##"
	case ${PKG_INSTALL_ROOT} in
	""|/)	add_drv screen_skip
		if [ ! -c /dev/screen_skip ]
		then
			echo "Configuring screen_skip driver in /devices and /dev."
			drvconfig -i clone
			devlinks
		fi
		;;
	*)	add_drv -b ${PKG_INSTALL_ROOT} screen_skip;;
	esac
fi

# Force reconfigure on next boot.
touch ${PKG_INSTALL_ROOT}/reconfigure

# Create screen name file.
namefile=${PKG_INSTALL_ROOT}/etc/opt/SUNWicg/SunScreen/name
[ -f $namefile ] || uname -n | sed 's/\..*//' >$namefile
chmod 644 $namefile

# Create software location file.
locationfile=${PKG_INSTALL_ROOT}/etc/opt/SUNWicg/SunScreen/location
echo "${CLIENT_BASEDIR}/SUNWicg/SunScreen" > $locationfile

# Remove any junk left by previous SunScreen software in system boot script.
rootusr=${PKG_INSTALL_ROOT}/etc/init.d/rootusr
if grep ss_plumb_interface $rootusr >/dev/null
then
	tmpfile=/tmp/rootusr.remove.$$
	echo "### Removing old SunScreen modifications to $rootusr ###"
	grep -v "SunScreen" $rootusr |
		grep -v "/sbin/ss_plumb_interface" |
		grep -v "sunscreen" > $tmpfile
	cp $tmpfile $rootusr
	rm -f $tmpfile
fi

echo "## Done ##"
