#! /bin/sh
#
#	Shell script for the installation of the PortMaster daemon
#	on the local Host.
#
#	@(#)pm_host_install	1.4 6/15/90  Copyright 1990 Livingston Enterprises Inc
#
PATH=/bin:/usr/bin:/usr/ucb:$PATH
#
#	Set up global variables.
#
HOSTNAME=`hostname`
HOSTSFILE="/etc/hosts"
SERVICES="/etc/services"
INETD_CONF="/etc/inetd.conf"
PMD="in.pmd"
SYSBIN="/usr/etc"
ALT_SYSBIN="/etc"
DEFAULT_INTERNET="192.9.200.1"
RUNNING_YP="NO"
UNAME=`whoami`
DIR=`expr \
  ${0-.}'/' : '\(/\)[^/]*/$' \
  \| ${0-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
  \| .`
#
#	Must be run as root
#
if [ "$UNAME" != "root" ]; then
	echo "Host Installation must be run as root."
	echo "Installation Aborted."
	exit 0
fi
#
#	Check to see if already install
#
if [ -f $SYSBIN/$PMD ]; then
	echo -n "This host appears to already be installed. Continue anyway? (y/n) "
	read ANSWER
	if [ "$ANSWER" != "y" ]; then
		echo "Installation Aborted."
		exit 0
	fi
elif [ -f $ALT_SYSBIN/$PMD ]; then
	echo -n "This host appears to already be installed. Continue anyway? (y/n) "
	read ANSWER
	if [ "$ANSWER" != "y" ]; then
		echo "Installation Aborted."
		exit 0
	fi
fi
#
echo 'Performing Host Installation.'
#
rm -f /tmp/testhost
grep -v localhost $HOSTSFILE | grep '^[0-9]' > /tmp/testhost
grep $HOSTNAME /tmp/testhost >/dev/null
if [ $? = 0 ]; then
#
#	We have what appears to be a valid host entry.
	echo "Valid host entry already entered in $HOSTSFILE."
	HOST_INTERNET=`grep $HOSTNAME /tmp/testhost | awk '{print $1}'`
else
	echo "Default host internet address = $DEFAULT_INTERNET"
	echo -n "Use default? (y/n) "
	read ANSWER
	if [ "$ANSWER" = "y" ]; then
		HOST_INTERNET=$DEFAULT_INTERNET
	else
		echo -n "Please enter a new host internet address: "
		read HOST_INTERNET
		echo -n "Use $HOST_INTERNET as internet address for $HOSTNAME? (y/n) "
		read ANSWER
		if [ "$ANSWER" = "n" ]; then
			echo "Installation Aborted."
			exit 0
		fi
	fi
	echo "Adding host entry in $HOSTSFILE for $HOSTNAME"
	echo "#" >> $HOSTSFILE
	echo -n "# These lines added by the" >> $HOSTSFILE
	echo " PortMaster Install Program" >> $HOSTSFILE
	echo "#" >> $HOSTSFILE
	echo "$HOST_INTERNET	$HOSTNAME loghost" >> $HOSTSFILE
	echo "#" >> $HOSTSFILE
	echo -n "# End of lines added by the" >> $HOSTSFILE
	echo " PortMaster Install Program" >> $HOSTSFILE
	echo "#" >> $HOSTSFILE
fi
rm -f /tmp/testhost
#
#	Start the network running
#
echo "Starting local network interface."
/etc/ifconfig ec0 $HOSTNAME -trailers up >/dev/null 2>&1
/etc/ifconfig ie0 $HOSTNAME -trailers up >/dev/null 2>&1
/etc/ifconfig le0 $HOSTNAME -trailers up >/dev/null 2>&1
#
#	Software Installation - Common for both install options
#
	echo 'Performing Software Installation.'
#
#	Check to see if we are running yellow pages.
#
	RUNNING_YP="NO"
	rm -f /tmp/testyp
	/usr/etc/rpcinfo -p > /tmp/testyp
	grep -w ypbind /tmp/testyp>/dev/null
	if [ $? = 0 ]; then
		RUNNING_YP="YES"
	fi
	rm -f /tmp/testyp
#
#	Add entry to SERVICES
	if [ "$RUNNING_YP" = "NO" ]; then
		echo "Updating $SERVICES"
		mv $SERVICES $SERVICES.old
		grep -v '1642/tcp' $SERVICES.old >> $SERVICES
		echo "pmd		1642/tcp" >> $SERVICES
		SERVICES_OK="YES"
	else
#		Check to see if the service has already been added to YP
		/bin/ypmatch pmd services >/dev/null 2>&1
		if [ $? = 1 ]; then
#
#			Check to see if this is the yellow pages master
#
			YP_MASTER=`/bin/ypwhich -m services >/dev/null 2>&1`
			if [ "$YP_MASTER" = "$HOSTNAME" ]; then
				echo "Updating $SERVICES"
				mv $SERVICES $SERVICES.old
				grep -v '1642/tcp' $SERVICES.old >> $SERVICES
				echo "pmd		1642/tcp" >> $SERVICES
#
#				Remake YP map
				(cd /usr/etc/yp;make services)
#
#				Check to see if pmd made it into services
				/bin/ypmatch pmd services > /dev/null
				if [ $? = 1 ]; then
					SERVICES_OK="NO"
				else
					SERVICES_OK="YES"
				fi
			else
				SERVICES_OK="NO"
			fi
		else
			SERVICES_OK="YES"
		fi
	fi
#
#	Copy in.pmd to executable directory
	echo "Installing in.pmd daemon"
	rm -f $SYSBIN/$PMD
	rm -f $ALT_SYSBIN/$PMD
	cp $DIR/$PMD $SYSBIN/$PMD >/dev/null
	if [ ! -f $SYSBIN/$PMD ]; then
		cp $DIR/$PMD $ALT_SYSBIN/$PMD >/dev/null
		if [ ! -f $ALT_SYSBIN/$PMD ]; then
			echo "Could not copy $PMD to either"
			echo "$SYSBIN or $ALT_SYSBIN"
			echo "Installation Aborted."
			exit 1
		fi
		SYSBIN=$ALT_SYSBIN
	fi
	
#	add entry to inetd.conf
	echo "Updating $INETD_CONF"
	mv $INETD_CONF $INETD_CONF.old
	grep -v 'in.pmd' $INETD_CONF.old >> $INETD_CONF
	echo "pmd	stream	tcp	nowait	root	$SYSBIN/in.pmd	in.pmd"\
		>> $INETD_CONF
#
#	Notify inetd
#
	if [ "$SERVICES_OK" = "YES" ]; then
		INETD_PID=`ps ax | grep inetd | grep -v grep | awk '{print $1}'`
		/bin/kill -HUP $INETD_PID
	else
echo ""
echo "Your system appears to be running the Sun Yellow Pages.  To complete"
echo "the installation, please add the following line to the Yellow Page"
echo "Master's /etc/services file and remake the Services Yellow Page Map:"
echo ""
echo "pmd       1642/tcp"
echo ""
echo "For further assistance please consult your network System"
echo "Administrator or the Sun Yellow Pages Service administration guide."
echo ""
echo "After the services yellow pages has been set up kill and restart"
echo "\"inetd\" on this system."
echo ""
	fi
#
	echo "Host Installation completed."
#
