#!/bin/ksh
#
#       @(#)run_disk0.sh 1.110 98/04/18 SMI
#
# Copyright (c) 1992-1999 Sun Microsystems, Inc.  All Rights Reserved.
#


LOG_FILE=disk0_install.log

FIND_CD="/usr/sbin/install.d/find_device -c"
CD_ID=`$FIND_CD`
WIZDIR=/webstart/wizards/apps
WIZBINDIR=/webstart/wizards/bin
LAUNCHDIR=/a/var/sadm/webstart
PRODDIR=/a/var/sadm/prod
PLATFORM=`uname -p`
NOWIN_FILE=/tmp/.nowin
date >> /tmp/$LOG_FILE

# save find_device results for launcher
# intel OS cd's use p0 as the slice that contains the Solaris products.
# bugid 4239378
if [ $PLATFORM = "i386" ];then
  CD_ID=`echo $CD_ID | sed -e 's:s:p:g'`
fi

typeset cdid=

# If a .cdroot file exists, the Solaris Wizard will prompt for Solaris CD
#
if [ -f /.cdroot ];then          #This is a cdrom installation
  if [ ! -z $CD_ID ];then
    for i in $CD_ID;do
      if [ -b /dev/dsk/$i ];then
	if [ -L /dev/dsk/$i ];then
	  ls -l /dev/dsk/$i | grep `cat /.cdroot` \
	   >> /dev/null 2>&1
	  if [ $? -eq 0 ];then
	    cdid=$i
	    break
	  fi
	fi
      fi
    done
  fi
elif [ -f /.netmnt ];then
	Installfs=`cat /.netmnt`
	mount -F nfs -o ro ${Installfs} /cdrom
fi

if [ -z $cdid ];then
        for i in $CD_ID;do
                if [ -b /dev/dsk/$i ];then
                        cdid=$i
                        break
                fi
        done
fi

echo $cdid > /tmp/find_device.out


cd $WIZDIR
XYPOS=
FLAGS=
if [ ! -f $NOWIN_FILE ];then
  # wait for the kiosk to finish loading or for netscape to die
  first="true"
  while true;do
    if [ ! -f /tmp/.kioskdone ];then
      if [ $first = "true" ];then
	echo /tmp/.kioskdone notfound >> /tmp/$LOG_FILE
      fi
      $(pgrep netscape > /dev/null 2>&1)
      if [ $? -eq 0 ];then
	if [ $first = "true" ];then
	  echo netscape is running  >> /tmp/$LOG_FILE
	fi
      else
	echo netscape is not running >> /tmp/$LOG_FILE
	break
      fi
    else
      echo "kiosk is finished" >> /tmp/$LOG_FILE
      break
    fi
    first="false"
  done

  # If a Kiosk is in use, position wizards at lower left of screen
  if [ -d /webstart/kiosk ]; then 
    XYPOS="-DDISPLAY=$DISPLAY -Dwizard.xpos=-1 -Dwizard.ypos=-1"
  fi
  FLAGS=">> /tmp/$LOG_FILE"
else
  FLAGS="-nodisplay"
fi

java $XYPOS solaris -R /a $FLAGS 2>>/tmp/$LOG_FILE

if [ -f /tmp/.instsuccess ];then
  
  # If there is a dispatch_table, 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. 

  # 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.

  cd ${LAUNCHDIR}
  if [ "X$FLAGS" = "X-nodisplay" ]; then
    touch .nodisplay
  fi
  if [ ! -f dispatch_table ];then
    touch dispatch_table
  fi

  if [ -f dispatch_table ];then
    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/webstart
    cp launcher.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}/getVold ${LAUNCHDIR}/bin
    cp ${WIZBINDIR}/runwebstart ${LAUNCHDIR}/bin
    cp ${WIZBINDIR}/setupRoot ${LAUNCHDIR}/bin
    chmod a+x ${LAUNCHDIR}/bin/*
    cp ${WIZBINDIR}/s96webstart /a/etc/init.d/webstart
    ln /a/etc/init.d/webstart /a/etc/rc2.d/S96webstart
  
    # 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.
  
    DISPATCH_TABLE=${LAUNCHDIR}/dispatch_table
    MRINSTALL=${LAUNCHDIR}/.mrInstallsComplete
    /usr/bin/egrep -s "MiniRoot=YES" $DISPATCH_TABLE >> /tmp/$LOG_FILE 2>&1 
    if [ $? -eq 0 ];then
      MINIROOT=yes
      cd ${LAUNCHDIR}
      PKG_INSTALL_ROOT=/a java $XYPOS launcher $FLAGS 2>>/tmp/$LOG_FILE
    else
      MINIROOT=no
      cd ${LAUNCHDIR}
      PKG_INSTALL_ROOT=/a java $XYPOS launcher $FLAGS 2>>/tmp/$LOG_FILE
      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 >> /tmp/$LOG_FILE 2>&1 
    if [ $? -ne 0 ];then
      if [ "$MINIROOT" = "no" -o -d "$MRINSTALL" ];then
        rm /a/etc/rc2.d/S96webstart
        rm /a/etc/init.d/webstart
        rm -rf ${LAUNCHDIR}
        if [ -f /a/etc/default/init.save ]
        then
            mv -f /a/etc/default/init.save /a/etc/default/init >/dev/null 2>&1
        fi

      fi
    fi
  fi
  

  if [ -d /a/var/sadm/system/logs ];then
    if [ -f /a/var/sadm/system/logs/$LOG_FILE ];then
      mv /a/var/sadm/system/logs/$LOGFILE /a/var/sadm/system/logs/$LOG_FILE.$$
    fi
    cp /tmp/$LOG_FILE  /a/var/sadm/system/logs/$LOG_FILE > /dev/null 2>&1
    if [ -f  /a/var/sadm/system/logs/cd0_install.log ];then
      cp /a/var/sadm/system/logs/cd0_install.log  /a/var/sadm/system/logs/cd0_install.log.$$
    fi
    cp /webstart/boot/tmp/cd0_install.log /a/var/sadm/system/logs
  fi
  if [ -f /sbin/installKiosk ]; then
    /sbin/installKiosk >> /tmp/$LOG_FILE 2>&1 
  fi
  sync;sync;reboot -q > /dev/null
  exit 0
else
  while true;
  do
    sleep 100000
  done
fi
