#!/bin/ksh

#
# This script can be used to update the images when new software
# is installed on the win98 template machine.  The first argument
# is the name of the client machine.  THis program should only be
# run on the master image server. (ie. nermal, io or kepler)
#

if [ $# -ne 4 ] ; then
	echo Error: arg count
	echo Usage: $0 win98_master test_machine new_image_name lab
	echo win98_master -- the name of the computer where new image is made
	echo test_machine -- the name of the computer where new image is tested
	echo new_image_name -- name of new image
	echo lab -- either palab or cplab
	exit 1
fi

WIN98HOST=$1
TEST_MACHINE=$2
IMAGE=$3
LAB=$4

if [ "$LAB" != "palab" -a "$LAB" != "cplab" -a "$LAB" != "cs-win98" -a \
	"$LAB" != "math-win98" ] ; then
	echo Invalid lab name specified. Must be one of palab or cplab.
	exit 1
fi

# Paths used by programs
IMAGE_DIR="/diskless/rootfs/winboot"
SEMAPHORE_DIR="$IMAGE_DIR/semaphores/$WIN98HOST"
rm -r $SEMAPHORE_DIR

while true ; do

if !  mkdir $SEMAPHORE_DIR ; then 
	echo failed to make semaphore directory $SEMAPHORE_DIR
	exit 1
fi
touch  $SEMAPHORE_DIR/quickboot

echo "           PACKAGE INSTALL PROCEEDURE"
echo
echo "Reboot the Windows 98 install machine and install the package."
echo "Try to direct the install program to install to the M: drive"
echo "so that the C: drive space is kept to a minimum.  After you"
echo "have checked that the package works then fire up the defrag"
echo "program so that he disk image is as small as possible.  The"
echo "computer can be rebooted as many times as is required and you"
echo "will not have to go through the logon procedure. To continue"
echo "with making the image type 'y' at the prompt below and restart"
echo "the computer."

echo -n "continue?(y or n) "
read ready
if [ "$ready" = "n" ] ; then 
	rm -r $SEMAPHORE_DIR
	exit 1
fi

rm $SEMAPHORE_DIR/quickboot
touch  $SEMAPHORE_DIR/no_sync

echo "The computer should boot up to the login screen this time. Once"
echo "the login screen appears type 'y' to continue with the image upgrade."
echo
echo -n "continue?(y or n) "
read ready
if [ "$ready" = "n" ] ; then 
	rm -r $SEMAPHORE_DIR
	exit 1
fi

while true ; do
	echo "Testing ssh to $WIN98HOST (This may hang for a few minutes"
	echo "if the server is not reachable ; check nameserver )"
	tst=`ssh $WIN98HOST echo "hello world"`
	if [ "$tst" = "hello world" ] ; then
		break
	fi
	echo -n "cannot connect to server, try again: (y or n) "
	read again
	if [ "$again" = "n" ] ; then
		rm -r $SEMAPHORE_DIR
		exit 1
	fi
done

echo ssh working...

echo Determining the size of the windows partition...
size=`ssh $WIN98HOST /sbin/fatsize /dev/hda1`

echo size is: $size
echo Copying image over to server...
ssh $WIN98HOST dd if=/dev/hda1 bs=8192 count=$size | \
	dd of=$IMAGE_DIR/$IMAGE

echo Computing image checksum...
gen_check_sum $IMAGE_DIR/$IMAGE \
	$IMAGE_DIR/$IMAGE.sum

echo $IMAGE >$SEMAPHORE_DIR/test_image
echo $IMAGE >$IMAGE_DIR/semaphores/test_image
echo $IMAGE >$IMAGE_DIR/semaphores/$TEST_MACHINE/test_image

echo "The test compter is now set up to boot the new image.  Simply"
echo "reboot the compter to ensure that the new image is installed and"
echo "login to test the new image."
echo 
echo "When you are satisfied that the software is properly functioning"
echo "then type 'y' at the prompt below and the new image will be installed"
echo "as the default image for the lab in question."
echo "If more work needs to be done then type 'n' and reboot the devlopment"
echo "computer to make changes to the image."
echo "on the image then type 'n' below. "
echo
echo -n "Finalize new image?(y or n) "
read ready
if [ "$ready" = "y" ] ; then 
	rm  -r $SEMAPHORE_DIR
	case $LAB in
	palab)
		echo $IMAGE > $IMAGE_DIR/semaphores/default_palab_image
		;;
	cs-win98)
		echo $IMAGE > $IMAGE_DIR/semaphores/default_cs-win98_image
		;;
	math-win98)
		echo $IMAGE > $IMAGE_DIR/semaphores/default_math-win98_image
		;;
	cplab)
		echo $IMAGE > $IMAGE_DIR/semaphores/default_cplab_image
		;;
	esac
	echo "The new image has been set up as the default image on"
	echo "this server only.  You will need to resync this image"
	echo "with the other servers."
	exit 0
fi
rm  -r $SEMAPHORE_DIR
done