#!/sbin/sh
# Usage:	install_disk0 <boot image absolute path>
# parameters:	$1 = boot image directory (Ex: /export2/s26_sparc)

BOOTIMAGE=$1
SRC=`pwd`
FILES=$SRC/disk0
ARCH=`uname -p`
PARENT_WS_LOC=/net/installzone.eng/export/install/cd0_1.0


BASEDIR=$BOOTIMAGE/../../..

CDTOC=$BASEDIR/.cdtoc

if [ `/usr/ucb/whoami` != "root" ]; then 
	echo "You Must be root."
	exit 1
fi
if [ ! "$BOOTIMAGE" -o "$1" = '-h' -o "$BOOTIMAGE" = '-help' ]; then
	echo "Usage: install_cd0 [Solaris boot image absolute path]."
	exit 1
fi

if [ ! -f $CDTOC ];then
  echo "Can not find $CDTOC"
  echo exiting
  exec sh
fi

PRODNAME=`grep PRODNAME $CDTOC | sed  -e 's:PRODNAME=::'`
PRODVER=`grep PRODVERS $CDTOC | sed -e 's:PRODVERS=::'`
NAMEVER=${PRODNAME}_${PRODVER}
MINI_PKG_LOC=${PARENT_WS_LOC}/pkgs/${ARCH}/${NAMEVER}

echo "Installing disk0 files ..."

ADMINFILE=/tmp/disk0admin.$$

cat << EOF > $ADMINFILE
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
EOF

cd $MINI_PKG_LOC
LOG=/tmp/minipkg.log
if [ -f $LOG ];then
  rm $LOG
fi
while read pkg
do
  if [ -d $pkg ];then
    pkgadd -d . -a $ADMINFILE -R $BOOTIMAGE $pkg >> $LOG 2>&1
    if [ $? -ne 0 ];then
      echo pkgadd of $pkg failed, see $LOG for details
      echo Exiting...
      exit -x
    fi
  else
    echo $pkg is required and is not in $MINI_PKG_LOC, Exiting...
    exit -l
  fi
done << EOF
SUNWadmap
SUNWadmc
SUNWadmgn
SUNWinst
EOF

#mkdir -p $MINI_CD0/bin

#Copy the new cd0 mini-root files to the curret mini-root
cp  $FILES/rcS.sh  		$BOOTIMAGE/sbin/rcS
chmod 744 $BOOTIMAGE/sbin/rcS
cp  $FILES/startup		$BOOTIMAGE/sbin/startup
chmod 555 $BOOTIMAGE/sbin/startup
cp $FILES/PFinstall.sh		$BOOTIMAGE/sbin/PFinstall
chmod 755 $BOOTIMAGE/sbin/PFinstall
cp $FILES/rmswap.sh $BOOTIMAGE/sbin/rmswap
chmod 755 $BOOTIMAGE/sbin/rmswap
cp  $FILES/setupRootDisk0.sh		$BOOTIMAGE/sbin/setupRootDisk0
chmod 755 $BOOTIMAGE/sbin/setupRootDisk0
cp $FILES/setup_sysidroot.sh    $BOOTIMAGE/sbin/setup_sysidroot
chmod 755  $BOOTIMAGE/sbin/setup_sysidroot
cp $FILES/cleanup_hosts.sh    $BOOTIMAGE/sbin/cleanup_hosts
chmod 755  $BOOTIMAGE/sbin/cleanup_hosts

if [ ! -f $BOOTIMAGE/nsswitch.dns ];then
  cp $FILES/nsswitch.dns $BOOTIMAGE/etc
fi

if [ ! -h $BOOTIMAGE/etc/resolv.conf ];then
  cd $BOOTIMAGE/etc
  ln -s ../tmp/root/etc/resolv.conf resolv.conf
  cd $SRC
fi

cp $FILES/preinstall.sh			$BOOTIMAGE/sbin/preinstall
chmod 755 $BOOTIMAGE/sbin/preinstall
cp $FILES/disk0_install.sh 		$BOOTIMAGE/sbin/disk0_install
chmod 755 $BOOTIMAGE/sbin/disk0_install
cp $FILES/run_install.sh		$BOOTIMAGE/sbin/run_install
chmod 755 $BOOTIMAGE/sbin/run_install
cp $FILES/getmemory			$BOOTIMAGE/sbin/getmemory
chmod 755 $BOOTIMAGE/sbin/getmemory
cp $FILES/getdisk			$BOOTIMAGE/sbin/getdisk
chmod 755 $BOOTIMAGE/sbin/getdisk
cp $FILES/getInstallLangs.sh		$BOOTIMAGE/sbin/getInstallLangs
chmod 755 $BOOTIMAGE/sbin/getInstallLangs
cp $FILES/transfer_list			$BOOTIMAGE/.tmp_proto/root/etc/transfer_list
chmod 644 $BOOTIMAGE/.tmp_proto/root/etc/transfer_list
cp $FILES/sysIDtool.state 		$BOOTIMAGE/.tmp_proto/root/etc/.sysIDtool.state
chmod 644 $BOOTIMAGE/.tmp_proto/root/etc/.sysIDtool.state
# add the appropriate termtype for architecture
if [ $ARCH -eq "sparc" ];then
	echo sun-cmd >> $BOOTIMAGE/.tmp_proto/root/etc/.sysIDtool.state

else
	echo AT386 >> $BOOTIMAGE/.tmp_proto/root/etc/.sysIDtool.state
fi
cp $FILES/bin/find_device.$ARCH $BOOTIMAGE/usr/sbin/install.d/find_device
chmod 555 $BOOTIMAGE/usr/sbin/install.d/find_device


if [ -f $BOOTIMAGE/etc/bootrc ];then
	echo "		copying bootrc"
	cp $FILES/bootrc.disk0 $BOOTIMAGE/etc/bootrc
	chmod 755 $BOOTIMAGE/etc/bootrc
fi

