#!/bin/ksh
#
#       @(#)cd0_install.sh 1.110 98/04/18 SMI
#
# Copyright (c) 1992-1998 Sun Microsystems, Inc.  All Rights Reserved. Sun
# considers its source code as an unpublished, proprietary trade secret, and
# it is available only under strict license provisions.  This copyright
# notice is placed here only to protect Sun in the event the source is
# deemed a published work.  Dissassembly, decompilation, or other means of
# reducing the object code to human readable form is prohibited by the
# license agreement under which this code is provided to the user or company
# in possession of this copy.
#
# RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the Government
# is subject to restrictions as set forth in subparagraph (c)(1)(ii) of the
# Rights in Technical Data and Computer Software clause at DFARS 52.227-7013
# and in similar clauses in the FAR and NASA FAR Supplement.
#


trap "" 1 2 3 15
LOG_FILE=/tmp/cd0_install.log
echo "cd0-> cd0_install.sh - `date`"  2>&1 >> $LOG_FILE 
ARCH=`uname -m`
DTDSPMSG=/usr/dt/bin/dtdspmsg
OWCONFIG_FILE=/etc/openwin/server/etc/OWconfig

RUNFLAG=y	# y = write mini-root to disk (20min), n = simulate it (1min)
### echo "Begining Setup, Approximate time is 20 minutes or less"
if [ -x /sbin/dial ]; then
  dial &
  dial_pid=$!
fi
# Local Variables
PATH=$PATH:./:/sbin:/usr/sbin; export PATH

Message() {
  ### Message() - display message from install.cat
  ### $1 - message id
  ### $2 - fallback message text

  if [ -x $DTDSPMSG ]; then
    $DTDSPMSG -s 3 findswap.cat $1 "$2" "$3" "$4" "$5"
  else
    echo "$2"
  fi
}

#Local Functions

_getexit() {
  typeset -r exitcode=$1

  case "$(exitcode)" in
    
    100) echo "Not Enough swap" >> $LOG_FILE  2>&1
	 Message 4 "Exiting to sh, see %s\n" $LOG_FILE
	 exec sh;;
    200) echo "Failure setting up filesystem" >> $LOG_FILE  2>&1
	  Message 4 "Exiting to sh, see %s\n" $LOG_FILE
	 exec sh;;
    300) echo "Failure creating rootdisk" >> $LOG_FILE  2>&1
	 Message 4 "Exiting to sh, see %s\n" $LOG_FILE
	 exec sh;;

  esac
}

# Locates swap on disk if > 200mb , if not then it 
# finds other disk partions that are suiteable and executes 

cd /webstart/wizards

for i in `ls W*`
do
  echo ${dial_pid} >> /tmp/.dial
  echo "Executing $i" >> $LOG_FILE  2>&1
  ./$i
  EXITCODE=$?
  if [ $? -gt 0 ];then
    _getexit $EXITCODE
  fi
done

## Test to make sure we were successful in the copyover
if [ ! -f /tmp/.cpiosuccess ];then
  exec sh
fi

## If this file exists, we booted of cdrom and want to let the wizards know
if [ -f /tmp/.cdroot ];then
  cp /tmp/.cdroot /a/.cdroot >> $LOG_FILE  2>&1
fi
## The root location on the cd
if [ -f /tmp/.cd0root ];then
  cp /tmp/.cd0root /a/.cd0root  >> $LOG_FILE  2>&1
fi

## If this file exists, we are doing a netinstall and want to let the wizards
## know.
if [ -f /tmp/.netmnt ];then
  cp /tmp/.netmnt /a/.netmnt  >> $LOG_FILE  2>&1
fi

## Create the boot/tmp dir to store the logs.
## 
if [ ! -d /a/webstart/boot/tmp ];then
  mkdir -p /a/webstart/boot/tmp >> $LOG_FILE  2>&1
fi

## copy files needed by the wizard to the expected dir.
if [ -f /tmp/.swappart ];then
  cp /tmp/.swappart /a/.swappart >> $LOG_FILE  2>&1
  cp /tmp/.swapinfo /a/.swapinfo >> $LOG_FILE  2>&1
  cp /tmp/.rootdisk /a/.rootdisk >> $LOG_FILE  2>&1
  cp /tmp/.dsize.out /a/webstart/boot/diskinfo.out >> $LOG_FILE 2>&1
  cp /tmp/.swapinfo.out /a/webstart/boot/partinfo.out >> $LOG_FILE 2>&1
fi

# Kill the dial.
dial_pid=`cat /tmp/.dial`
if [ ! -z "${dial_pid}" ]; then
  kill $dial_pid >> $LOG_FILE  2>&1
fi

## if upgrade was chosen, let the wizard know.
if [ -f /tmp/.install_upgrade ];then
  cp /tmp/.install_upgrade /a >> $LOG_FILE  2>&1
fi

### Get the information from the locale choice off of cd0
### and append it to the init file for execution
### upon reboot - if the file is not found,
### default 'C' will be used.
if [ -f /tmp/root/etc/default/init ]; then
  cp /tmp/root/etc/default/init /a/.tmp_proto/root/etc/default/init.orig >> $LOG_FILE  2>&1
  /usr/bin/env | /usr/bin/grep LC >> /tmp/root/etc/default/init
  /usr/bin/env | /usr/bin/grep LANG >> /tmp/root/etc/default/init
  cp /tmp/root/etc/default/init /a/tmp/root/etc/default/init >> $LOG_FILE  2>&1
  cp /tmp/root/etc/default/init /a/.tmp_proto/root/etc/default/init >> $LOG_FILE  2>&1
fi

if [ ! -d /a/.tmp_proto/root/var/sadm ];then
  mkdir -p /a/.tmp_proto/root/var/sadm > $LOG_FILE  2>&1
fi

ln -s ../../../../a/var/sadm/prod /a/.tmp_proto/root/var/sadm


## if not running Xserver, do not after reboot either.
if [ -f /tmp/.nowin ];then
  touch /a/.tmp_proto/.nowin
fi

BOOT_DEVICE=`cat /tmp/.bootdev` >> $LOG_FILE 2>&1
cp /tmp/.bootdev /a >> $LOG_FILE 2>&1

# Reboot the DISK0 locallly disk based mini-root
if [ ${ARCH} = "i86pc" ];then

  #cp the OWconfig file if it exists
  if [ -f /tmp/root/${OWCONFIG_FILE} ];then
    echo copying /tmp/root/$OWCONFIG_FILE to /a/.tmp_proto/root/$OWCONFIG_FILE >> $LOG_FILE 2>&1
    cp /tmp/root/$OWCONFIG_FILE /a/.tmp_proto/root/$OWCONFIG_FILE >> $LOG_FILE 2>&1
  fi
  # mount the pcfs fs on /tmp/x86_boot
  X86_BOOT=`cat /tmp/.x86_boot` >> $LOG_FILE  2>&1
  cp /tmp/.bootpart /a/.bootpart >> $LOGFILE 2>&1
  mkdir /tmp/x86_boot >> $LOG_FILE  2>&1
  mount -F pcfs /dev/dsk/${X86_BOOT} /tmp/x86_boot  >> $LOG_FILE  2>&1
  if [ $? -ne 0 ];then
    Message 3 "Unable to mount /dev/dsk/%s\n" ${X86_BOOT}
    exit -1
  fi
  # setup the x86 boot part
  cd /boot
  rm -rf /tmp/x86_boot/*
  find . -print | cpio -dump /tmp/x86_boot >> $LOG_FILE  2>&1
  if [ -f /tmp/bootenv.new ];then
    rm /tmp/bootenv.new
  fi
  ## remove the confflags from the bootenv.rc file
  grep -v confflags /tmp/root/boot/solaris/bootenv.rc > /tmp/bootenv.new
  ## set up the bootenv.rc to boot to the slice specified in $BOOT_DEVICE
  cp /tmp/root/boot/solaris/bootenv.rc /tmp/root/boot/solaris/bootenv.old >> $LOG_FILE  2>&1
  cp /tmp/bootenv.new /tmp/root/boot/solaris/bootenv.rc >> $LOG_FILE  2>&1
  rm /tmp/bootenv.new >> $LOG_FILE  2>&1
  eeprom auto-boot?=true
  eeprom bootpath=${BOOT_DEVICE}

  ### Added for bugid 4280892 - cd0: should not autoboot from hard disk 
  ### when ITU is used during first boot
  if [ -f /tmp/diskette_rc.d/icdinst9.sh ]; then
    sh /tmp/diskette_rc.d/icdinst9.sh -i
  fi

  cp /tmp/root/boot/solaris/bootenv.rc /tmp/x86_boot/solaris/bootenv.rc >> $LOG_FILE  2>&1

  if [ -f /tmp/x86_boot/escd.rf ];then
    rm /tmp/x86_boot/escd.rf
  fi

  if [ -f /tmp/root/boot/escd.rf ];then
    cp /tmp/root/boot/escd.rf /tmp/x86_boot
    cp /tmp/root/boot/escd.rf /a/.tmp_proto/root/boot
  fi

  ## make the solaris map file know where to find what it needs
  echo "/boot/		/ p" > /tmp/x86_boot/solaris.map
  ## sync the miniroot /boot with what is in the x86 boot partition
  rm -r /a/boot/* >> $LOG_FILE  2>&1
  cp -r /tmp/x86_boot/* /a/boot  >> $LOG_FILE  2>&1
  rm /a/boot/solaris.map >> $LOG_FILE  2>&1
  rm /a/boot/solaris/bootenv.rc  >> $LOG_FILE  2>&1
  cd /a/boot/solaris  >> $LOG_FILE  2>&1
  ln -s ../../tmp/root/boot/solaris/bootenv.rc bootenv.rc  >> $LOG_FILE  2>&1
  rm /a/.tmp_proto/root/boot/solaris/bootenv.rc
  ## put the bootenv.rc in the tmp_proto area so the right one is copied 
  ## after the reboot
  cp /tmp/x86_boot/solaris/bootenv.rc /a/.tmp_proto/root/boot/solaris

  ## create the escd.rf
  if [ -f /a/boot/escd.rf ];then
    rm /a/boot/escd.rf
  fi

  cd /a/boot
  ln -s ../tmp/root/boot/escd.rf escd.rf >> $LOG_FILE 2>&1
 
  cd /tmp/root  >> $LOG_FILE  2>&1
  cp -rfp /tmp/diskette_rc.d /a/.tmp_proto  >> $LOG_FILE  2>&1
  umount /tmp/x86_boot > /dev/null 2>&1 >> $LOG_FILE  2>&1
  
  if [ -f /tmp/sysidcfg ];then
    rm /a/etc/sysidcfg
    cp /tmp/sysidcfg /a/etc/sysidcfg 
  fi
  Message 1 "Need to reboot to continue the installation.\nPlease remove the boot media (floppy or cdrom) and press Enter.\nNote: If the boot media is cdrom, you must wait for the system\nto reset in order to eject."
  read stdin
  cp -rf $LOG_FILE /a/webstart/boot/$LOG_FILE
  sync;sync;reboot 2>&1 >> $LOG_FILE
else
  #### FIX for bugid 4233697
  cd /a/usr/java1.2/jre/lib/sparc > /dev/null 2>&1
  if [ $? -eq 0 ];then
    if [ ! -h libmon_sun4m.so ];then
      ln -s libmonitor_v8.so ./libmon_sun4m.so
    fi
  fi
  Message 2 "Rebooting to continue the installation.\n"
  cp -rf $LOG_FILE /a/webstart/boot/$LOG_FILE
  sync;sync;reboot -l -q -- $BOOT_DEVICE 2>&1 >> $LOG_FILE  2>&1
fi

exec sh
