#!/bin/ksh
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)setup-launcher.sh	1.12	06/06/13 SMI"
#

#
# This script prepares for post-install installation of extra
# products found in the Solaris distribution.
# This script should only be run if the interactive installation
# was successful
#
# arguments: $1 - 0 if launcher should auto-reboot, non-zero otherwise
#
. /sbin/install-common
if install_debug_isset scripts ; then
    set -x
fi

install_debug scripts "$0 begins `date`"

DATADIR=/usr/lib/install/data
LAUNCHDIR=/a/var/sadm/launcher
DISPATCH_TABLE=${LAUNCHDIR}/dispatch_table
WIZDIR=${DATADIR}/wizards/apps
PRODDIR=/a/var/sadm/prod
WIZBINDIR=${DATADIR}/wizards/bin
SVCCFG=/usr/sbin/svccfg
SVCCFG_REPOSITORY=/a/etc/svc/repository.db
METHODDIR=/a/lib/svc/method
INSTALLLAUNCHER=install-launcher
TEXT_INSTALL_FILE=/tmp/.text_install
REBOOT=$1
WIZARDFLAGS=$2
INSTALLTYPE=$3

export SVCCFG_REPOSITORY

# The logic to set up the postinstall packages to be added and
# dispatch table is normally executed during interactive install as
# part of the interactive installers.  The jumpstart installer has no
# such logic, so we must invoke this logic using the below scripts,
# when a jumpstart has taken place.
if [ "${INSTALLTYPE}" = "jumpstart" -a \
    -x ${WIZBINDIR}/parsePackagesToBeAdded -a \
    -x ${WIZBINDIR}/create_dispatch_table ] ; then
	${WIZBINDIR}/parsePackagesToBeAdded
	${WIZBINDIR}/create_dispatch_table
fi

# If jumpstart user deleted java, can't run launcher after reboot

if [ "${INSTALLTYPE}" = "jumpstart" -a -f ${DISPATCH_TABLE} -a \
    ! -f /a/usr/java/bin/java ] ; then
        echo `gettext "Unable to run Launcher without Java."`
        echo `gettext "The following CDs will not be installed:"`
        /usr/bin/grep CDName ${DISPATCH_TABLE} | /usr/bin/cut -d= -f2
        exit $REBOOT
fi

mkdir -p ${LAUNCHDIR}
cd ${LAUNCHDIR}

# If no dispatch_table was created by solaris wizard, create
# an empty one now. This will force launcher to run so we
# can see the reboot panel.

if [ -z "$DISPLAY" ]; then
  touch .nodisplay
fi

if [ ! -f dispatch_table ];then
  touch dispatch_table
fi

# We need to set up to run the launcher after the system reboot.
# Copy over the necessary files and set up rc script to run. Do 
# this now in case the system reboots unexpectedly. 

cd $WIZDIR
mkdir -p ${LAUNCHDIR}/bin
mkdir -p ${PRODDIR}
mkdir -p /var/sadm/system
ln -s /a/var/sadm/system/data /var/sadm/system/data
ln -s ${LAUNCHDIR} /var/sadm/launcher
cp launcher.class ${LAUNCHDIR}
cp postinstall.class ${LAUNCHDIR}
mkdir -p ${LAUNCHDIR}/com/sun/launcher
if [ -d com/sun/launcher ]; then
    cp -r com/sun/launcher ${LAUNCHDIR}/com/sun
fi
cp /tmp/find_device.out ${LAUNCHDIR} 
cp ${WIZBINDIR}/postinstaller ${LAUNCHDIR}/bin
cp ${WIZBINDIR}/getVold ${LAUNCHDIR}/bin
cp ${WIZBINDIR}/runinstall ${LAUNCHDIR}/bin
cp ${WIZBINDIR}/ttyFallback ${LAUNCHDIR}/bin
cp ${WIZBINDIR}/setupRoot ${LAUNCHDIR}/bin
chmod a+x ${LAUNCHDIR}/bin/*

if [ -d ${METHODDIR} -a -f ${SVCCFG_REPOSITORY} ]; then
    cp ${WIZBINDIR}/${INSTALLLAUNCHER} ${METHODDIR} >> ${INSTALL_LOG} 2>&1
    ${SVCCFG} import /var/svc/manifest/system/${INSTALLLAUNCHER}.xml \
						>> ${INSTALL_LOG} 2>&1
fi

# *** After vold gets converted into an service, we will make sure
# *** that it is a dependent of our system/install service, defined in
# *** install-launcher.xml.  This means that it will not start
# *** up until system/install is finished.

# If we need to reboot automatically, add -autonext flag
# to launcher reboot panel.
  
if [ -d ${LAUNCHDIR}/.autoreboot -o ${REBOOT} -eq 0 ]; then
  WIZARDFLAGS="-autonext Reboot:90 $WIZARDFLAGS"
  mkdir -p ${LAUNCHDIR}/.autoreboot
fi

# Copy the postinstall directories (packages/patches/scripts)
# to ${LAUNCHDIR} so they are available before or after the reboot.

if [ "${INSTALLTYPE}" != "jumpstart" -a -f ${LAUNCHDIR}/.itags \
					-a -f ${LAUNCHDIR}/.osDir ] ; then
    OSDIR=`head -1 ${LAUNCHDIR}/.osDir | awk '{print $1}'`
    cd ${OSDIR}
    find packages -print | cpio -pdum ${LAUNCHDIR} >/dev/null 2>&1
    find patches -print | cpio -pdum ${LAUNCHDIR} >/dev/null 2>&1
    find scripts -print | cpio -pdum ${LAUNCHDIR} >/dev/null 2>&1
fi

# After the miniroot based launcher is done, we check to see 
# if there are non-miniroot products. If not, we delete the 
# appropriate files so that the launcher won't come up after 
# the system reboot.

MRINSTALL=${LAUNCHDIR}/.mrInstallsComplete
if [ "${INSTALLTYPE}" != "jumpstart" -a -x /usr/java/bin/java \
				-a ! -f "${TEXT_INSTALL_FILE}" ] ; then
    /usr/bin/egrep -s "MiniRoot=YES" $DISPATCH_TABLE >> ${INSTALL_LOG} 2>&1 
    if [ $? -eq 0 ];then
	MINIROOT=yes
	cd ${LAUNCHDIR}
	PKG_INSTALL_ROOT=/a java $WIZARDPROPS launcher $WIZARDFLAGS 2>>${INSTALL_LOG}
    else
        /usr/bin/egrep -s "W2" $DISPATCH_TABLE > /dev/null 2>&1
        W2=$?
        /usr/bin/egrep -s "postinstaller" $DISPATCH_TABLE > /dev/null 2>&1
        if [ ${W2} -ne 0 -a  $? -eq 0 ]; then

                # The only product in the dispatch_table is the postinstall
	        # wizard and it was supposed to run after the reboot, 
	        # probably because the install is from CD. Since java is
	        # available, run the postinstall wizard in the miniroot instead.

		sed -e 's/MiniRoot=NO/MiniRoot=YES/' $DISPATCH_TABLE \
							> ${DISPATCH_TABLE}.$$
                cp ${DISPATCH_TABLE}.$$ ${DISPATCH_TABLE}
  	        MINIROOT=yes
	        cd ${LAUNCHDIR}
	        PKG_INSTALL_ROOT=/a java $WIZARDPROPS launcher $WIZARDFLAGS 2>>${INSTALL_LOG}
	else
	    MINIROOT=no
	    cd ${LAUNCHDIR}
	    PKG_INSTALL_ROOT=/a java $WIZARDPROPS launcher $WIZARDFLAGS 2>>${INSTALL_LOG}
	    mkdir -p $MRINSTALL
        fi
    fi

    # launcher tells us whether reboot is needed.  Even if
    # the user selected auto-reboot during install, the launcher
    # presents an opportunity to escape it.
    if [ -d ${LAUNCHDIR}/.needShell ] ; then
	# note that setting REBOOT to 1 means NO reboot,
	# to maintain consistency with the exit values
	# from ttinstall/jumpstart
	REBOOT=1
    else
	REBOOT=0
    fi
else 
    mkdir -p $MRINSTALL
fi

cd $WIZDIR
 
# Determine whether to run launcher after reboot. If no
# need to run launcher, remove rc scripts and launcher files.
# If backup /etc/default/init file exists, copy it back here

/usr/bin/egrep -s "MiniRoot=NO" $DISPATCH_TABLE > /dev/null 2>&1
REMAINING=$?
if [ ${REMAINING} -eq 0 ]; then

    # There is at least one product designated to install after the reboot.
    # See if there is only a single product in the dispatch_table (the first
    # product is designated as W1, the second would be W2).

    /usr/bin/egrep -s "W2" $DISPATCH_TABLE > /dev/null 2>&1
    if [ $? -ne 0 ]; then

        # There are no W2 entries, so we have only one product left to install 
	# after the reboot. See if it's the postinstall wizard.

        /usr/bin/egrep -s "postinstaller" $DISPATCH_TABLE > /dev/null 2>&1
        if [ $? -eq 0 ]; then

            # The only product in the dispatch_table is the postinstall
            # wizard and it is supposed to run after the reboot. 
            # Run a script version of the postinstall wizard now.
            # This avoids having to do an entire reboot just to run
            # the postinstall wizard.

	    if [ -x /sbin/install-additional-prods ] ; then
		/sbin/install-additional-prods
	    fi
	    REMAINING=-1
    	    mkdir -p $MRINSTALL
	fi
    fi

    if [ ${REMAINING} -eq 0 -a ! -x /a/usr/java/bin/java ]; then
	# In case we have a product whose REQUIRED metacluster
	# didn't include java, tell the user that it can't be installed.

	/usr/bin/egrep -s "CDName" ${DISPATCH_TABLE} > /dev/null 2>&1
	if [ $? -eq 0 ];then
	    echo `gettext "Unable to run Launcher without Java."`
	    echo `gettext "The following CDs will not be installed:"`
	    /usr/bin/grep CDName ${DISPATCH_TABLE} | /usr/bin/cut -d= -f2
	fi
	REMAINING=-1
        mkdir -p $MRINSTALL
    fi
fi

# No products remaining to be handled by launcher

if [ ${REMAINING} -ne 0 ]; then
  if [ "$MINIROOT" = "no" -o -d "$MRINSTALL" ]; then
    if [ -f /a/etc/default/init.save ]; then
	    mv -f /a/etc/default/init.save /a/etc/default/init >/dev/null 2>&1
    fi

    # The .needEject directory is normally created by the launcher. But now
    # that the launcher doesn't always run in the miniroot, we need to
    # do a redundant check to see if we need to eject the disc.

    if [ "${INSTALLTYPE}" != "jumpstart" \
         -a -d ${LAUNCHDIR}/.bootDisc -a ! -d ${LAUNCHDIR}/.noeject ]; then
        mkdir ${LAUNCHDIR}/.needEject > /dev/null 2>&1
    fi

    # Keep the rc script around if we need to eject a DVD. 
    # After reboot, script will eject disc and delete itself.

    if [ ! -d ${LAUNCHDIR}/.needEject ] ; then

	if [ -d ${METHODDIR} -a -f ${SVCCFG_REPOSITORY} ]; then

		# check if system/install service is in the repository
		# before attempting to delete it.
		if ${SVCCFG} select system/install:default >/dev/null 2>&1;then
			${SVCCFG} delete -f system/install >> \
						${INSTALL_LOG} 2>&1
			if [ $? -eq 0 ] ; then
				rm -f ${METHODDIR}/${INSTALLLAUNCHER} >> \
							${INSTALL_LOG} 2>&1
			else
				# In case the install service couldn't be
				# deleted from the repository, we leave the
				# script in place and make it clean itself up
				# after reboot.  This will avoid having the
				# system/install service run after reboot and
				# try to run the launcher.
				echo "#!/bin/sh" > \
						${METHODDIR}/${INSTALLLAUNCHER}
				echo "${SVCCFG} delete -f system/install" >> \
					${METHODDIR}/${INSTALLLAUNCHER}
				echo "rm -f /lib/svc/method/${INSTALLLAUNCHER}" \
					>> ${METHODDIR}/${INSTALLLAUNCHER}
				echo "exit 0" >> ${METHODDIR}/${INSTALLLAUNCHER}
			fi
		else
			rm -f ${METHODDIR}/${INSTALLLAUNCHER} >> \
							${INSTALL_LOG} 2>&1
		fi
	fi
	rm -rf ${LAUNCHDIR}
    fi
  fi
fi
	  
install_debug scripts "$0 completes `date`"
exit $REBOOT
