#!/bin/bash

BINARYDIR="/z/ug/binary"
LOGTTY=/dev/tty4

echo -e "\n\E[1;32m---------------------------------------"
echo -e   "\E[1;33m Alternative Wininstaller script"
echo -e   "\E[0;37m Windows installation is not sexy!"
echo -e           " So, go make youreself some cofee"
echo -e           " or be a real geek; watch the HDD LED"
echo -e   "\E[1;32m---------------------------------------\E[0;37m\n"

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 write_ini_entry()
	{
	local myfile=$1 mysec=$2 mykey=$3 myval=`echo $4 | sed 's#\\\\#\\\\\\\\#g'` tmp=$1.$$
	cat /dev/null > $tmp
	awk "
		BEGIN	{
			# Indicate if we are within the right section
			in_desired_section = 0;

			# Indicate if we should write the entry as
			# fast as possible.
			write_now = 0;

			# If 1, delete any further occurences
			have_written = 0;
			}

		# Section matching
		/\[.*\]/ {
			if (\$1 ~ /\[$mysec\]/) {
				in_desired_section = 1;
				write_now = 1;
			} else {
				in_desired_section = 0;
			}

			print \$0;
		}

		# Key assignment matching
		/.*=.*/ {
			if (\$1 ~ /$mykey/) {
				if (in_desired_section == 0) {
					print \$0;
				} else {
					# Skip HERE to purge old value
				}
			} else {
				print \$0;
			}
		}

		# Run on every line
		{
			if (write_now == 1) {
				printf \"$mykey = \\\"$myval\\\"\n\";
				write_now = 0;
				have_written = 1;
			}
		}
		END	{
			if (have_written == 0)
				{
				printf \"[$mysec]\n$mykey = \\\"$myval\\\"\n\";
				}
			}
				
	" $myfile >> $tmp
	mv $tmp $myfile
	}

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
. /etc/functions.sh || die "Can not parse shell function lib (/etc/functions.sh)"

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

#message "Fixing up mbr"
#dd if=$BINARYDIR/freedos_mbr_boot_code.bin of=/dev/dsk bs=1 count=446  
message "Writing ntldr_boot_code_sektor0"
dd if=$BINARYDIR/ntldr_boot_code_sektor0 of=/dev/dsk$BOOTNUMBER bs=1 count=420 seek=90 > $LOGTTY 2>&1\
   || die "could not write ntldr_boot_code_sektor0"
message "Writing ntldr_boot_code_sektor12"
dd if=$BINARYDIR/ntldr_boot_code_sektor12 of=/dev/dsk$BOOTNUMBER bs=512 count=1 seek=12 > $LOGTTY 2>&1\
   || die "could not write ntldr_boot_code_sektor12"

#Move staging area for install.pl onto the drive
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!"
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!"

#boot ini
message "Creating boot.ini"
echo "[Boot Loader]" >/c/boot.ini
echo "Timeout=2" >>/c/boot.ini
echo "Default=C:\\\$WIN_NT\$.~BT\BOOTSECT.DAT" >>/c/boot.ini
echo "[Operating Systems]" >>/c/boot.ini
echo "C:\\\$WIN_NT\$.~BT\BOOTSECT.DAT = \"Installation/Update Windows\"">>/c/boot.ini

#more aia-paranoia, ..
cat /c/boot.ini >> /dev/null || die "/c/boot.init gone missing!"

#creating directorys
message "Creating /c/\$"
mkdir /c/\$  

message "Creating /c/\$win_nt\$.~bt"
mkdir /c/\$win_nt\$.~bt  

message "Creating /c/\$win_nt\$.~bt/system32"
mkdir /c/\$win_nt\$.~bt/system32  

message "Creating /c/\$win_nt\$.~ls"
mkdir /c/\$win_nt\$.~ls  


#bootsect.dat
message "Creating bootsect.dat"
dd if=/dev/dsk$BOOTNUMBER of=/c/\$win_nt\$.~bt/bootsect.dat bs=512 count=1 > $LOGTTY 2>&1
message "Writing ntldr_boot_code into bootsect.dat"
dd if=$BINARYDIR/ntldr_boot_code_install\
   of=/c/\$win_nt\$.~bt/bootsect.dat bs=1 count=420 seek=90 conv=notrunc > $LOGTTY 2>&1\
       || die "Failed writing $BINARYDIR/ntldr_boot_code_install into bootsect.dat"

#copy files
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/amd64" ]
then
	ARCH=amd64
	OS_INSTALL_DIR="$OS_DIR/amd64"
else
	ARCH=i386
	OS_INSTALL_DIR="$OS_DIR/i386"
fi

copy_files "$OS_INSTALL_DIR/" "/c/\$win_nt\$.~ls/" 1 "Copy system"
if [ "$ARCH" == "amd64" ]
then
	copy_files "$OS_DIR/i386/" "/c/\$win_nt\$.~ls/" 1 "Copy i386 system"
fi
copy_files "/c/\$win_nt\$.~ls/i386/" "/c/\$win_nt\$.~bt/" 2 "Copy installation files."
copy_files "/c/\$win_nt\$.~ls/i386/system32/" "/c/\$win_nt\$.~bt/system32/" 2 "Copy system32."
copy_files "$OS_DIR/i386/ntldr" "/c/" 3 "Copy ntldr."
copy_files "$OS_DIR/i386/ntdetect.com" "/c/" 3 "Copy ntdetect.com."
copy_files "$OS_DIR/i386/setupldr.bin" "/c/\$ldr\$" 3 "Copy setupldr.bin."
copy_files "$OS_INSTALL_DIR/txtsetup.sif" "/c/" 3 "Copy txtsetup.sif."

message "Add some black magic to unattend.txt -prevents wininstaller to ask for CD"
write_ini_entry /c/netinst/unattend.txt "GuiUnattended" "AutoLogon" "Yes"
write_ini_entry /c/netinst/unattend.txt "Data" "MsDosInitiated" "1"
write_ini_entry /c/netinst/unattend.txt "Data" "floppyless" "1"
write_ini_entry /c/netinst/unattend.txt "Data" "OriSrc" "A:\I386"
write_ini_entry /c/netinst/unattend.txt "Data" "OriTyp" "5"
copy_files "/c/netinst/unattend.txt" "/c/\$win_nt\$.~bt/winnt.sif" 3 "Copy winnt.sif."

#move files/drivers to the right place.....
message "Move driver direcory"
mv /c/\$win_nt\$.~ls/$ARCH/\$[Oo][eE][mM]\$/* /c/\$/  

message "Syncing disk -aiaparanoia :-)"
sync
message "Umount /c"
umount /c

echo -e "Press enter\n"
read
#END
