#!/bin/sh

### Setup File for creating disk0image and archive
### Make sure that if there is to be no Kiosk,
### that a '-n' is passed BEFORE all other args...
USE_KIOSK=YES   ### Initialize default to yes

while getopts "nh" opt; do
	case $opt in
		n ) USE_KIOSK=NO
			echo "We will not be using a Kiosk"
			;;
		
		h ) 
			echo "Usage: disk0_setup [-n <optional>] [Solaris boot image absolute path] [net|cd] "
			exit 0
			;;
	esac
	
	### Shift to make sure other args come through
	shift
done


### Set up other args/vars
MINI_ROOT_PATH=$1
BOOTIMAGE=$MINI_ROOT_PATH/Tools/Boot
DISK0_IMAGE=$MINI_ROOT_PATH/Tools/disk0Boot
PACKAGE_DIR=$MINI_ROOT_PATH/Product
SRC=`pwd`
ADMINFILE=/tmp/admin.$$
ARCH=`uname -p`
FASTFSBIN=`pwd`/files/bin/fastfs.${ARCH}

if [ $ARCH = i386 ];then
	ZIP=/home/mattw/info-zip/zip22_x86/zip
else
	ZIP=/opt/info-zip/zip22/zip
fi


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: disk0_setup [-n] [Solaris boot image absolute path] [net|cd] <media_kit>"
        echo "Usage: disk0_setup [-n <optional>] [Solaris boot image absolute path] [net|cd] "
        exit 1
fi


if [ -f ${FASTFSBIN} ];then
	cp ${FASTFSBIN} /tmp/fastfs
	FASTFSBIN=/tmp/fastfs
	chmod +x ${FASTFSBIN}
	
	$FASTFSBIN -f $MINI_ROOT_PATH

	$FASTFSBIN $MINI_ROOT_PATH
	FASTFS="yes"
else
	FASTFS="no"
fi

if [ -f $BOOTIMAGE/a/swap2 ];then
	rm -r $BOOTIMAGE/a/*
fi


echo "	Creating Disk0 Image"
echo ""
#if [ -d $DISK0_IMAGE ];then
#	rm -rf $DISK0_IMAGE
#	echo ""
#fi
if [ -d $BOOTIMAGE/webstart ];then
	rm -rf $BOOTIMAGE/webstart
fi

### Make the disk0Boot directories (and other underneath)
mkdir -p $DISK0_IMAGE/webstart/boot

if [ $USE_KIOSK = "YES" ]; then
	echo "Making Kiosk directory.\n"
	mkdir -p $DISK0_IMAGE/webstart/kiosk
fi
mkdir -p $DISK0_IMAGE/webstart/utils
mkdir -p $DISK0_IMAGE/webstart/wizards/app
mkdir -p $DISK0_IMAGE/webstart/media_kits
mkdir -p $DISK0_IMAGE/webstart/os
mkdir -p $DISK0_IMAGE/webstart/os/common/patches
mkdir -p $DISK0_IMAGE/webstart/os/common/packages
mkdir -p $BOOTIMAGE/webstart/boot
mkdir -p $BOOTIMAGE/webstart/wizards
mkdir -p $BOOTIMAGE/webstart/utils

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


cd $PACKAGE_DIR
echo "Adding needed packages...."
/bin/ksh $SRC/neededpackages $ADMINFILE $BOOTIMAGE $PACKAGE_DIR
if [ $? -ne 0 ];then
	echo "Adding packages to $BOOTIMAGE failed, ... exiting"
	exit -1
else
	echo "Adding packages to $BOOTIMAGE successful....\n"
fi


echo "Starting to create DISK0_IMAGE: $DISK0_IMAGE\n"
cd $BOOTIMAGE
find . -print | cpio -dump $DISK0_IMAGE
if [ -f $DISK0_IMAGE/webstart/boot/*.cpio.zip ]; then
# 	echo "Removing $DISK0_IMAGE/webstart/boot/*.cpio.zip"
	rm $DISK0_IMAGE/webstart/boot/*.cpio.zip 2>&1 > /dev/null
fi


echo "Running miniroot_delete....\n"
sh $SRC/miniroot_delete $DISK0_IMAGE > /tmp/miniroot_delete.log 2>&1
cd $SRC
sh install_disk0 $DISK0_IMAGE
if [ $? -ne 0 ];then
  echo install_disk0 $DISK_IMAGE failed
  exit -1
else
	echo "miniroot_delete ran successfully"
fi

### Test to see if we wanted to use the Kiosk
### and install accordingly
if [ $USE_KIOSK = "YES" ]; then
	echo "Installing the Kiosk and Netscape.."
	ksh install_netscape $DISK0_IMAGE
	sh install_kiosk $DISK0_IMAGE $BOOTIMAGE
	echo "....done installing the Kiosk and Netscape.\n"
else
	echo "We are not using a Kiosk, so the Kiosk"
	echo "and Netscape are not being installed."
fi

### Install other software accordingly
sh install_wizards disk0 $DISK0_IMAGE
sh install_products $DISK0_IMAGE 

#setup cd0 image
sh cd0_setup $BOOTIMAGE

#setup openwin
cd $DISK0_IMAGE/usr

find openwin -print | cpio -odum -B -O $BOOTIMAGE/webstart/boot/openwin.cpio
$ZIP $BOOTIMAGE/webstart/boot/openwin.cpio.zip $BOOTIMAGE/webstart/boot/openwin.cpio
rm -rf openwin
rm $BOOTIMAGE/webstart/boot/openwin.cpio


#remove the devices, they are added prior to reboot"
if [ $DISK0_IMAGE != "" ];then
	echo "	Removing disk0 /dev & /devices"
	rm -rf $DISK0_IMAGE/dev/*
	rm -rf $DISK0_IMAGE/devices/*
fi

if [ $DISK0_IMAGE != "" ];then
	echo "  Removing disk0 platform files and dirs"
 	rm -rf $DISK0_IMAGE/platform/*
	rm -rf $DISK0_IMAGE/usr/platform/*
fi

if [ -f $DISK0_IMAGE/a/swap2 ];then
	rm -r $DISK0_IMAGE/a/*
fi

#ADDED until they fix the /usr/java/bin problem

if [ ! -h $DISK0_IMAGE/usr/bin/java ];then
	cd $DISK0_IMAGE/usr/bin
	if [ -d $DISK0_IMAGE/usr/java1.2 ] ;then
		ln -s ../java1.2/jre/bin/java java
	elif [ -d $DISK0_IMAGE/usr/java ];then
		ln -s ../java/bin/java java
	fi
fi

### Make the archive
echo "  Creating Archive"
cd $DISK0_IMAGE
if [ ! -d $BOOTIMAGE/webstart/boot ];then
	mkdir -p $BOOTIMAGE/webstart/boot
fi
if [ -f $BOOTIMAGE/webstart/boot/disk0.cpio.zip ];then
	echo "  Removing $BOOTIMAGE/webstart/boot/disk0.cpio.zip"
	rm $BOOTIMAGE/webstart/boot/disk0.cpio.zip
fi
find . -print | cpio -odum -B -O $BOOTIMAGE/webstart/boot/disk0.cpio
echo "Compressing $BOOTIMAGE/webstart/boot/disk0.cpio"
$ZIP $BOOTIMAGE/webstart/boot/disk0.cpio.zip $BOOTIMAGE/webstart/boot/disk0.cpio 
if [ $? -eq 0 ];then
	rm $BOOTIMAGE/webstart/boot/disk0.cpio
fi

if [ $FASTFS = "yes" ];then
	sync
	$FASTFSBIN -s $MINI_ROOT_PATH
	$FASTFSBIN $MINI_ROOT_PATH
	sync
	lockfs -fa
fi
echo ""
echo "Finished Creating Disk0 image"
