#!/sbin/sh
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#pragma ident	"@(#)sysid-finish.sh	1.8	06/01/13 SMI"
#

. /sbin/install-common
if install_debug_isset scripts ; then
    set -x
fi

install_debug scripts "$0 begins `date`"

# sysid-finish completes system identification by running a host
# of system identification utilities.
CMDFILE=/tmp/sysid-finish.$$
SYSIDSTATE=/etc/.sysIDtool.state
SVCADM=/usr/sbin/svcadm

install_debug scripts "command file is $CMDFILE"

echo `gettext "Completing system identification..."`

MEM=`/sbin/mem`; MEM=`expr $MEM / 1024`
if [ "${RUN_TEXT_INSTALL}" = "yes" -o -z "$DISPLAY" -o \
	$MEM -lt $MIN_GUI_SYSID_VIRTMEM ]; then
    cat  > ${CMDFILE} <<-EOF
	. /sbin/install-common

	# run the CLI system identification
	SYSIDNET="/usr/sbin/sysidnet -y"
	if install_debug_isset man_sysid ; then
	    echo "Would have run \$SYSIDNET"
	    /sbin/sh
	else
	    \$SYSIDNET
	fi

	SYSIDKRB5="/usr/sbin/sysidkrb5 -m"
	if install_debug_isset man_sysid ; then
	    echo "Would have run \$SYSIDKRB5"
	    /sbin/sh
	else
	    \$SYSIDKRB5
	fi

	echo "Starting remote procedure call (RPC) services:\c"
	SYSIDNS="/usr/sbin/sysidns -y"
	if install_debug_isset man_sysid ; then
	    echo "Would have run \$SYSIDNS"
	    /sbin/sh
	else
	    \$SYSIDNS
	fi

	# NOTE:	/var/nis/$hostname will never exist at this point
	#
	if [ -f /var/nis/NIS_COLD_START ]; then
	    echo " keyserv\c"
	${SVCADM} enable -t network/rpc/keyserv 2>&1 | install_debug_pipe scripts
	fi

	echo " done."

	SYSIDSYS="/usr/sbin/sysidsys"
	if install_debug_isset man_sysid ; then
	    echo "Would have run \$SYSIDSYS"
	    /sbin/sh
	else
	    \$SYSIDSYS
	fi

	SYSIDROOT="/usr/sbin/sysidroot -m"
	if install_debug_isset man_sysid ; then
	    echo "Would have run \$SYSIDROOT"
	    /sbin/sh
	else
	    \$SYSIDROOT
	fi
	EOF

	SYS_IDENT_TITLE=`gettext "System Identification"`
	if [ -n "${DISPLAY}" ]; then
	    if [ -x /usr/dt/bin/dtterm ] ; then
		/usr/dt/bin/dtterm -title "${SYS_IDENT_TITLE}" -geometry 80x36+0+0 -e /sbin/sh $CMDFILE
	    else
		if [ -n "$XTF" ] ; then
		    /usr/openwin/bin/xterm -title "${SYS_IDENT_TITLE}" -fn "$XTF" -geometry 80x24+0+0 -cu -wf -e /sbin/sh $CMDFILE
		else
		    /usr/openwin/bin/xterm -title "${SYS_IDENT_TITLE}"  -geometry 80x24+0+0 -cu -wf -e /sbin/sh $CMDFILE
		fi
	    fi
	else
	    /sbin/sh $CMDFILE
	fi

	if install_debug_isset scripts ; then
	    echo "Not deleting $CMDFILE for debug"
	else
	    rm -f $CMDFILE
	fi

else
    if install_debug_isset wizard ; then
	FLAGS="-debug $FLAGS"
    fi

    FLAGS="$FLAGS >> $INSTALL_LOG"

    ### Beef up the ulimit
    ### Helps with the limitation of files (processes) open
    ### from within the browser
    /usr/bin/ulimit -n 256
    JAVCMD="java sysid ${FLAGS}"
    install_debug scripts "invoking $JAVCMD"

    if install_debug_isset man_sysid ; then
	echo "Would have run $JAVCMD"
	/sbin/sh
    else
	$JAVCMD
    fi
fi

#
# Method to check the /etc/.sysIDtool.state file to see if this
# system is networked.
system_networked() {

    networked=0
    line=

    if [ -f ${SYSIDSTATE} ] ; then
	line=`/usr/bin/grep "System is on a network" ${SYSIDSTATE}`

	if [ $? -eq 0 ] ; then
	    networked=`echo $line | /usr/bin/cut -c1`
	fi
    fi

    [ $networked -eq 1 ] && return 1

    return 0
}

#
# If the system is networked we setup additional networking for the kiosk
#
system_networked
if [ $? -eq 1 ] ; then
    ${SVCADM} disable -t network/initial 2>&1 | install_debug_pipe scripts
    ${SVCADM} enable -t network/initial 2>&1 | install_debug_pipe scripts

    ${SVCADM} disable -t network/rpc/nisplus 2>&1 | install_debug_pipe scripts
    ${SVCADM} disable -t network/rpc/keyserv 2>&1 | install_debug_pipe scripts

    ${SVCADM} disable -t network/nis/client 2>&1 | install_debug_pipe scripts

    # Disabling the internet/nis/client service does not kill ypbind because
    # the current ypbind running was not started by that service.
    # We need to call ypstop manually to kill ypbind.
    if [ -x /usr/lib/netsvc/yp/ypstop ]; then
	/usr/lib/netsvc/yp/ypstop 2>&1 | install_debug_pipe scripts
    fi

    # Sleep 10 to give ypstop time to kill ypbind
    sleep 10

    /usr/bin/pgrep ypbind > /dev/null 2>&1

    if [ $? -eq 0 ] ; then
	# It's still running, lets really make sure
	sleep 15
	/usr/bin/pgrep ypbind > /dev/null 2>&1

	if [ $? -eq 0 ] ; then
	    # ypbind is still running, kill it again, show no mercy
	    # workaround for bugid
	    # 4262125 - x86 system hangs at sysid due to
	    # ypbind never time out with cd0.
	    /usr/bin/pkill -9 ypbind 2>&1 | install_debug_pipe scripts
	fi
    fi

    ${SVCADM} disable -t network/rpc/bind 2>&1 | install_debug_pipe scripts

    # Disabling the network/rpc/bind service does not kill rpcbind because
    # the current rpcbind running was not started by that service.
    # We need to kill the rpcbind process manually.
    /usr/bin/pkill -9 -x -u 0,1 rpcbind  2>&1 | install_debug_pipe scripts
    /usr/bin/rm -rf /var/run/rpc_door 2>&1 | install_debug_pipe scripts

    ${SVCADM} disable -t system/name-service-cache 2>&1 | install_debug_pipe scripts

    # Gather domainname
    DOMAINNAME=`/usr/bin/domainname`

    # If we're configured with DNS, enable it.
    if [ -f /etc/resolv.conf ]; then
	${SVCADM} enable -t -r network/dns/client 2>&1 | install_debug_pipe scripts
    fi

    # If we're configured with nisplus, enable it.
    if [ -f /var/nis/NIS_COLD_START ]; then
	${SVCADM} enable -t -r network/rpc/nisplus 2>&1 | install_debug_pipe scripts
    # Else if we're configured with nis, enable nis.
    elif [ -n "$DOMAINNAME" -a -d /var/yp/binding/$DOMAINNAME ]; then
	${SVCADM} enable -t -r network/nis/client 2>&1 | install_debug_pipe scripts
    # Else just enable rpcbind.
    else
	${SVCADM} enable -t -r network/rpc/bind 2>&1 | install_debug_pipe scripts
    fi

    ${SVCADM} disable -t network/service 2>&1 | install_debug_pipe scripts
    ${SVCADM} enable -t network/service 2>&1 | install_debug_pipe scripts

    ${SVCADM} enable -t system/name-service-cache 2>&1 | install_debug_pipe scripts

    ${SVCADM} disable -t network/inetd 2>&1 | install_debug_pipe scripts
    ${SVCADM} enable -t network/inetd 2>&1 | install_debug_pipe scripts

fi

#
# If this system is booted into the disk0 miniroot of a CD0 install,
# we must run /sbin/cleanup_hosts because the /etc/inet/hosts and
# /etc/inet/netmasks files are configured and written to "/" since we
# have a writable "/" in this miniroot.  We symlink them back to /tmp/root/
# by calling /sbin/cleanup_hosts
#
if [ -f /.swappart ] ; then
    /sbin/cleanup_hosts 2>&1 | install_debug_pipe scripts
fi

# clean up clobbered links when mini-root is read-write
#
if [ ! -d /tmp/root/etc/inet ] ; then
	mkdir -p /tmp/root/etc/inet
	cp /etc/inet/hosts /tmp/root/etc/inet/hosts
	cp /etc/inet/netmasks /tmp/root/etc/inet/netmasks
	cd /etc/inet
	rm hosts netmasks
	ln -s ../../tmp/root/etc/inet/hosts hosts
	ln -s ../../tmp/root/etc/inet/netmasks netmasks
fi

install_debug scripts "$0 completes `date`"
