#!/bin/bash

# The parameters for this script are as follows:

# imagedrive image_name customization_filename 
# 	image_name = filename of image to use
#	customization_filename is the name of the file to use to adapt the installed image
#		to the machine and environment it is now in.  This file contains things like
#		the files that control the network configuration, the modules to load, the
#		fstab to use, etc.
#
# The kernel and system map from the new scratch partition will be copied
# to your /boot partition under the names vmlinuz.scratch and System.map.scratch.
#
# After running this program to image a shared partition, your next step in
# your autobench.dat file should be to call 'boot' to reboot to the partition
# you just imaged.

. /etc/autobench.conf || . functions

# check if all required variables are defined, otherwise exit autobench
if [ "$DATS_MAINT_PARTITION" == "" -o "$DATS_MAINT_LABEL" == "" -o "$DATS_SCRATCH_PARTITION" == "" -o "$DATS_SCRATCH_LABEL" == "" ]; then
	log "Imagedrive called when system not configured properly, please fix autobench.conf"
	exit 86
fi

test_string=$(mount | grep ' / ' | grep "$DATS_MAINT_PARTITION" )

if [ -z "$test_string" ] ;then
	log "not booted to maint partition, rebooting to label $DATS_MAINT_LABEL"
	#re-add the imagedrive command to the top of the autobench.dat, since we didn't really do it yet.
	echo $0 "$@" > $TMPDIR/tmpfile
	cat $AUTODIR/scripts/autobench.dat >> $TMPDIR/tmpfile
	cp $TMPDIR/tmpfile $AUTODIR/scripts/autobench.dat
	rm  $TMPDIR/tmpfile
	# boot to the maint partition.  If this fails we should abort, thus avoing possible infinite loop.
	boot $DATS_MAINT_LABEL
	#exit with no retcode, so that boot's retcode gets passed up
	exit 
fi


# $CUSTOMIZATIONFILENAME is the name of a tar file to be extracted over the $DATS_SCRATCH_PARTITION
CUSTOMIZATIONFILENAME=$2

# $TEMPFILESLOCATION is a directory that is large enough to hold your disk image file.
TEMPFILESLOCATION="$TMPDIR/images"
mkdir -p $TEMPFILESLOCATION

# $SCRATCHMOUNT is the mount point upon which to mount $DATS_SCRATCH_PARTITION after it has been imaged
SCRATCHMOUNT='$TMPDIR/mnt'
mkdir -p $SCRATCHMOUNT

# UNTESTED, but this is probably a better way to bring the image over
# ftp ftp://hostname.ibm.com/path/to/image.gz - | bunzip2 | dd of=/dev/scratchdisk

# FTP the disk image file and machine customization file from $DATS_IMAGEHOME
wget ${DATS_IMAGEHOME}/${1} -O ${TEMPFILESLOCATION}/${1}
check_status "Downloading new image file"
wget ${DATS_IMAGEHOME}/${CUSTOMIZATIONFILENAME} -O ${TEMPFILESLOCATION}/${CUSTOMIZATIONFILENAME}
check_status "Downloading new image customization file"

gunzip < $TEMPFILESLOCATION/$1 | dd of=$DATS_SCRATCH_PARTITION

# mount $DATS_SCRATCH_PARTITION
mount $DATS_SCRATCH_PARTITION $SCRATCHMOUNT
check_status "mounting scratch partition"

# untar the customization file to $DATS_SCRATCH_PARTITION
#  NOTE this tarfile is created as follows "tar czvf whatever.tar -T list.of.files.to.tar"
tar xzf $TEMPFILESLOCATION/$CUSTOMIZATIONFILENAME -C $SCRATCHMOUNT
check_status "untarring customization file"

# now copy the kernel from our scratch partition to /boot/vmlinuz.scratch
cp $SCRATCHMOUNT/boot/vmlinuz /boot/vmlinuz.scratch

# now copy the corresponding map file
cp $SCRATCHMOUNT/boot/System.map* /boot/System.map.scratch

# now copy the corresponding vmlinux file
cp $SCRATCHMOUNT/boot/vmlinux /boot/vmlinux.scratch

# unmount the scratch partition
umount $SCRATCHMOUNT

# delete the image file now that we are done with it.
rm $TEMPFILESLOCATION/$1

# delete the customization file now that we are done with it.
rm $TEMPFILESLOCATION/$CUSTOMIZATIONFILENAME


