#!/bin/ksh
#
# "@(#)modify_install_server 1.00 99/04/30
#
#
# Copyright (c) 1992-1996 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.

#
# Used to copy a Solaris Installer miniroot to a pre-existing Solaris CD net
# image to allow booting and installing using the Solaris Installer from the
# network.
#

trap "cleanup_and_exit 1" 1 2 3 15

LOG=/tmp/setup_$$.log

PRESERVE="false"

#
# cleanup_and_exit
#
# Purpose : Call cleanup and exit with the passed parameter
#
# Arguments :
#       exit code
#
cleanup_and_exit()
{
  	if [ ! -z $DIALPID ];then
	  	kill -9 $DIALPID
	fi
	  exit $1
}

#
# usage
#
# Purpose : Print the usage message in the event the user
#           has input illegal command line parameters and
#           then exit
#
# Arguments :
#       none
#
usage()
{
  	echo ""
  	echo "Usage: $myname [-p]  <install_dir_path> <installer_miniroot_path>"
	echo "   or  $myname [-h]"
	echo ""
	echo "    -p preserves existing miniroot image"
	echo "    -h print this message"
}

#
# failed
# Purpose : Print failure message and point to logfile
#
# Arguments : none
#
failed()
{
	echo "$myname execution failed"
	echo "Logfile found at $LOG"
	return 1
}

### Begin Main
myname=$0
while getopts "ph" opt; do
	case $opt in
		p ) PRESERVE="true";;
	  	h ) usage
		    exit 0;;
    		* ) echo ""
		    echo "ERROR: invalid option"
		    usage
	   	    exit 1;;
  	esac
done
shift $(($OPTIND -1))


if [ -z $1 ];then
  	echo ""
  	echo "path to existing install image required"
	usage
	cleanup_and_exit 1
fi

if [ -z $2 ];then
	echo ""
  	echo "path to installer miniroot required"
	usage
	cleanup_and_exit 1
fi

if [ -d $1/Solaris_8/Tools/Boot ];then
	TOOLSDIR=$1/Solaris_8/Tools
	BOOTDIR=$TOOLSDIR/Boot
	ORIGBOOTDIR=$BOOTDIR.orig
else
	echo $1/Tools/Boot not found!
	usage
	cleanup_and_exit 1
fi 
if [ -d $2 ];then
	MINILOC=$2
else
	echo ""
	echo "$2 not found!"
	usage
	cleanup_and_exit 1
fi

if [ ! -d $MINILOC/webstart/boot ];then
  	echo ""
  	echo "$MINILOC does not appear to be a valid installer miniroot"
	usage
	cleanup_and_exit 1
fi

echo "Setting up installer net image"

# mv the Current miniroot out of the way
mv $BOOTDIR $ORIGBOOTDIR >> $LOG 2>&1 || return $(failed)
if [ $PRESERVE = "false" ];then
  	echo ""
  	echo "removing existing miniroot"
  	rm -rf $ORIGBOOTDIR >> $LOG 2>&1 & rmpid=$!
else 
	echo "preserving existing miniroot in $ORIGBOOTDIR"
fi
# cp the cdmini to the Tools directory

mkdir $BOOTDIR >> $LOG 2>&1

$TOOLSDIR/dial & DIALPID=$!
export DIALPID

cd $MINILOC
find . -print | cpio -pdum $BOOTDIR >> $LOG 2>&1 || return $(failed)

#
if [ ! -z $rmpid ];then
	wait $rmpid
fi
kill -9 $DIALPID
echo "Setup complete"
rm $LOG
cleanup_and_exit 0

