#!/bin/bash
#
# This file is released under GPL version 2
# Mari Gzuk is the orginal author and therefore the copyright holder.
# The file is taken from the unattended-gui project, namely the unattended-nodosemu script.
# See http://unattended.technikz.de or http://unattended-gui.sourceforge.net
# for more info on Marios unattended-gui project
#
# see also Marios writeup on how this work: 
# http://unattended-gui.sourceforge.net/wiki/index.php?title=Win:installation_process
#
# Adapted to the unattended.sf.net project december 2007, Nils Olav Fossum
#

BINARYDIR="/usr/lib/ntldrbin"
LOGTTY=/dev/tty4

echo -e "\n\E[1;32m----------- \E[1;33m $0 \E[1;32m-----------\E[0;37m"

function message()
	{
	echo -e "\E[1;34m*\E[1;37m $1\E[0;37m"
	}

function die ()
	{
	echo -e "\E[1;31m$@\E[1;37m"
	message "Dropping to shell"
	exec /bin/bash
	}


function copy_files ()
        {
        #$1 = file or directory
        #$2 = destination
        #$3 = 1 complete directory and subdirs
        #$3 = 2 only content from directory
        #$3 = 3 only file
        #$4 = title
        DIR=${1%/}
	DIR="$DIR/"
        case "$3" in
                "1")
		message "$4 ($1 $2)"
		#check if directory exist
		DESTINATIONDIR="$2/`basename $1`"
		if [ ! -d $DESTINATIONDIR ]
		then
			 mkdir -p "$DESTINATIONDIR" 
		fi
                cp -Lr "$1" "$2" || die "recursive directory copy failed"
                ;;
                "2")
		message "$4 ($1 $2)"
                cp "$1"* "$2" > $LOGTTY 2>&1 
		# FIXME cp exits nonzero for some reason - die "nonrecursive directory copy failed"
                ;;
                "3")
		message "$4 ($1 $2)"
		cp "$1" "$2" || die "file copy failed"
                ;;
                *)
                #option not given dont do anything...
                ;;
        esac
	sync #  free memory caches, everything is going to disk anyway
        }


#source function lib
. /usr/lib/unatt-functions.sh || die "Can not parse shell function lib (/usr/lib/unatt-functions.sh)"

BOOTNUMBER=`parted /dev/dsk print | grep "boot" | awk '{print $1}'`


fix_disk_geo_heads=`parse_ini_file /c/netinst/unattend.txt _meta fix_disk_geo_heads`
if [ -n "$fix_disk_geo_heads" ] ; then
   message "We have fix_disk_geo_heads ($fix_disk_geo_heads), writing into /dev/dsk$BOOTNUMBER"
   echo -e -n "\x`printf "%02X" $fix_disk_geo_heads`"|dd of=/dev/dsk$BOOTNUMBER bs=1 seek=26 conv=notrunc > $LOGTTY 2>&1\
       || die "could not write fix_disk_geo_heads into /dev/dsk$BOOTNUMBER"
fi

fix_disk_geo_sectors=`parse_ini_file /c/netinst/unattend.txt _meta fix_disk_geo_sectors`
if [ -n "$fix_disk_geo_sectors" ] ; then
   message "We have fix_disk_geo_sectors ($fix_disk_geo_sectors), writing into /dev/dsk$BOOTNUMBER"
   echo -e -n "\x`printf "%02X" $fix_disk_geo_sectors`"|dd of=/dev/dsk$BOOTNUMBER bs=1 seek=24 conv=notrunc > $LOGTTY 2>&1\
       || die "could not write fix_disk_geo_sectors into /dev/dsk$BOOTNUMBER"
fi

message "Saving existing disk geometry for /dev/dsk$BOOTNUMBER"
#..because the premade ntldr_boot_code_sektor0 will overwrite existing partition disk geometry.
dd if=/dev/dsk$BOOTNUMBER of=/c/netinst/partgeo.bin bs=1 skip=24 count=4 > $LOGTTY 2>&1\
   || die "could not save disk geometry for /dev/dsk$BOOTNUMBER"

message "Writing 98dos_boot_code_sektor0 into /dev/dsk$BOOTNUMBER"
dd if=$BINARYDIR/98dos_boot_code_sektor0 of=/dev/dsk$BOOTNUMBER bs=1 count=420 skip=90 seek=90 > $LOGTTY 2>&1\
   || die "could not write $BINARYDIR/98dos_boot_code_sektor0 to disk"

message "Writing 98dos_boot_code_sektor2 into /dev/dsk$BOOTNUMBER"
dd if=$BINARYDIR/98dos_boot_code_sektor2 of=/dev/dsk$BOOTNUMBER bs=512 count=1 seek=2 > $LOGTTY 2>&1\
   || die "could not write $BINARYDIR/98dos_boot_code_sektor2 to disk"

message "Writing saved disk geometry back into /dev/dsk$BOOTNUMBER" 
dd if=/c/netinst/partgeo.bin of=/dev/dsk$BOOTNUMBER bs=1 seek=24 count=4 conv=notrunc > $LOGTTY 2>&1\
   || die "could not write /c/netinst/partgeo.bin into /dev/dsk$BOOTNUMBER"

#Move staging area for install.pl onto the drive
OS_media=`parse_ini_file /c/netinst/unattend.txt _meta OS_media`
OS_DIR=`echo $OS_media | awk -F: '{print $2}' | sed 's/\\\/\\//g'`
OS_DIR="/z$OS_DIR"

if [ -d "$OS_DIR/WINME" ]
then
	OS_NAME=WINME
	OS_INSTALL_DIR="$OS_DIR/WINME"
else
	OS_NAME=WIN98
	OS_INSTALL_DIR="$OS_DIR/WIN98"
fi



mv /c /OLDc
mkdir /c
message "Mounting the soon-to-be 'C:' drive"
mount -t vfat /dev/dsk$BOOTNUMBER /c || die "Mounting /dev/dsk$BOOTNUMBER failed!"


#cp /etc/dosemu/dosemu.conf /tmp
#sed -i "s/dosboot.img/w98boot.img/g"  /tmp/dosemu.conf
#echo \$_hdimage = \"/dev/dsk$active_part\" >> /tmp/dosemu.conf

# DOSEMU always exits non-zero.  FIXME.
#message "run dosemu for sys c:"
#dosemu.bin -f /tmp/dosemu.conf

#copy files
copy_files "$OS_DIR/DOSBOOT/IO.SYS" "/c/" 3 "Copy dos boot IO.SYS"
copy_files "$OS_DIR/DOSBOOT/MSDOS.SYS" "/c/" 3 "Copy dos boot MSDOS.SYS"
copy_files "$OS_DIR/DOSBOOT/DRVSPACE.BIN" "/c/" 3 "Copy DRVSPACE.BIN"
copy_files "$OS_DIR/DOSBOOT/COMMAND.COM" "/c/" 3 "Copy dos boot COMMAND.COM"

copy_files "$OS_DIR/DOSBOOT/SETUP.BAT" "/c/" 3 "Copy dos boot SETUP.BAT"
copy_files "$OS_DIR/DOSBOOT/AUTOEXEC.BAT" "/c/" 3 "Copy dos boot AUTOEXEC.BAT"

message "Moving install.pl staging files to the real soon-to-be 'C:' drive"
mv /OLDc/* /c/

#aia paranoia
cat /c/netinst/unattend.txt >> /dev/null || die "/c/netinst/unattend.txt gone missing!"


copy_files "$OS_INSTALL_DIR/" "/c" 1 "Copy $OS_NAME system"

message "Syncing disk -aiaparanoia :-)"
sync

echo -e "\E[1;32m------- \E[1;33m Done! ($0) \E[1;32m-------\E[0;37m"

#END
