#! /bin/bash

clear
c=0
dlm=0
yn=0
echo
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "             MakeDisk Script For BootIt NG - TeraByte, Inc."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo

userid=`id | grep -c uid=0`
if [ "$userid" = "0" ]
    then echo "This script must be run as root...exiting"
    echo
    exit 1
fi

fy=0
if [ "$2" = "f" ]; then fy=1; fi  # format floppy
sys=0
if [ -d /sys/block ]; then sys=1; fi

f=0;usb=0
if [ ! "$1" = "" ]; then
    if [ "$1" = "/iso" ]; then
	f=iso
	fy=0
    else    
	for fd in fd0 fd1 sda sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp
	do
	    if [ "$1" = "/dev/$fd" ]; then
		f=$fd
		if [ "/dev/$f" != "/dev/fd0" ] && [ "/dev/$f" != "/dev/fd1" ]; then
		    usb=1
		    fy=0
		fi
		break
	    fi
	done
    fi
fi
    	
if [ "$f" = "0" ]; then
    echo "Incorrect or missing command line parameter, or the drive specified"
    echo "is not supported by makedisk"
    echo
    echo "Drives supported by makedisk: fd0, fd1, sda through sdp"
    echo
    echo "Usage examples:"
    echo "./makedisk /dev/fd0   -> create bootable diskette at /dev/fd0 (A:)"
    echo "./makedisk /dev/fd0 f -> format diskette first (standard floppy drives only)"
    echo "./makedisk /iso       -> create bootitng.iso file to burn to CD/DVD"
    echo "./makedisk /dev/sdd   -> create bootable diskette or UFD at /dev/sdd"
    echo
    echo "Command line entered: $0 $1 $2"
    echo
    exit 1
fi

# clean up temp directories from previous run (normally not needed)
umount tmp2 2> /dev/null
rm -r tmp2 2> /dev/null
rm -r tmp1 2> /dev/null

# check that specified disk exists, is removable, and is not mounted
if [ "$f" !=  "iso" ]; then
    if [ "$sys" = "1" ]; then
	if [ ! -e /sys/block/$f ]; then
	    echo "Device /dev/$f was not detected on this system...exiting"
	    echo
	    exit 1 
	else
	    remov=`cat /sys/block/$f/removable`
	    if [ "$remov" = "0" ]; then
	        echo "Device /dev/$f exists, but is NOT a removable drive...exiting"
		echo
		exit 1
	    fi
	fi
    elif [ "$usb" = "1" ]; then
    	echo "Makedisk cannot safely determine that /dev/$f is a removable drive"
	echo "You can create the boot disk manually with this command:"
	echo "cp DISKIMG3.DAT /dev/$f"
	echo "Before doing this, please ENSURE that /dev/$f is the CORRECT drive"
	echo "ALL DATA data on the drive written to WILL BE LOST"
	echo "Note: All boot disk options (video, mouse, etc.) will be the defaults"
	exit 1
    elif [ ! -e /sys/block/$f ]; then   # std floppy, make sure device exists
	echo "Device /dev/$f was not detected on this system...exiting"
	echo
	exit 1
    fi
fi

if [ "$f" != "iso" ]; then
    mdisk=`mount | grep /dev/$f > /dev/null`
    if [ "$?" = "0" ]; then
	echo "Device /dev/$f is currently MOUNTED"
	echo "Please unmount the drive and then run 'makedisk' again"
	echo
	exit 1
    fi
fi

echo "Checking for necessary files"
if [ ! -e DISKIMG3.DAT ]; then
    echo
    echo "DISKIMG3.DAT not found in this directory...exiting"
    echo
    exit 1
fi

if [ "$f" = "iso" ]; then
    if [ ! -x /usr/bin/mkisofs ]; then
	echo "/usr/bin/mkisofs not found. Cannot create iso file...exiting"
	echo
	exit 1
    fi
fi

if [ "$fy" = "1" ]; then
    if [ ! -x /usr/bin/fdformat ]; then
	fy=0
	echo "Note: /usr/bin/fdformat not found. Cannot format diskette as requested"
	echo -n "Press <Enter> to continue "
	read nfd
    fi
fi
# License agreement question

while [ "$yn" = "0" ]
do
    clear
    echo
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "             MakeDisk Script For BootIt NG - TeraByte, Inc."
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo
    echo "LICENSE AGREEMENT:"
    echo "The use of BootIt NG constitutes acceptance of the terms of the License"
    echo "Agreement as defined in the LICENSE.TXT file in this directory"
    echo
    echo "Do you ACCEPT the terms of the License Agreement?"
    echo -n "Enter y or n, then press <Enter> "
    read yn
    if [ ! "$yn" = "" ]
    then
    if [ "$yn" = "y" ] || [ "$yn" = "Y" ]
        then
        yn=y
        break
    elif [ "$yn" = "n" ] || [ "$yn" = "N" ]
        then
        yn=n
        break
    fi
    fi
    yn=0
done

if [ "$yn" = "n" ]
    then
    echo "You have chosen NOT to accept the terms of the License Agreement"
    echo -n "Press <Enter> to exit..."
    read e
    echo
    exit 1
else
    echo "You have chosen to ACCEPT the License Agreement"
    echo
fi

# reg.txt option
roption=0
if [ -e reg.txt ] || [ -e REG.TXT ]; then
    while [ "$roption" = "0" ]
    do
	echo "Registration option:"
	echo "~~~~~~~~~~~~~~~~~~~~"
	echo "To include reg.txt: Enter "y", then press <Enter>"
	echo "Otherwise, just press <Enter>"
	echo -n "Registration option selection: "
	read roption
	if [ "$roption" = "" ]; then
	    roption=n
	    echo "Not including reg.txt on boot media"
	elif [ "$roption" = "y" ]; then
	    echo "Including reg.txt on boot media"
	else
	    roption=0
	fi
	echo
    done
fi

# third party driver option
tpoption=0
if [ -e udmatsr.com ] || [ -e UDMATSR.COM ]; then
    while [ "$tpoption" = "0" ]
    do
	echo "Third Party UDMA Driver option:"
	echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	echo "To include the UDMA driver: Enter "y", then press <Enter>"
	echo "Otherwise, just press <Enter>"
	echo -n "UDMA Driver option selection: "
	read tpoption
	if [ "$tpoption" = "" ]; then
	    tpoption=n
	    echo "Not including UDMA Driver on boot media"
	elif [ "$tpoption" = "y" ]; then
	    echo "Including UDMA Driver on boot media"
	else
	    tpoption=0
	fi
	echo
    done
fi

# mouse option
moption=0
while [ "$moption" = "0" ]
do
    echo "Mouse support option:"
    echo "~~~~~~~~~~~~~~~~~~~~~"
    echo "To enable mouse support: Just press <Enter>"
    echo "To disable mouse support: Enter "n", then press <Enter>"
    echo -n "Mouse support selection: "
    read moption
    if [ "$moption" = "" ]; then
	moption=y
	echo "Mouse support enabled on boot media"
    elif [ "$moption" = "n" ]; then
	echo "Mouse support disabled on boot media"
    else
	moption=0
    fi
    echo
done

# video option
voption=0
while [ "$voption" = "0" ]
do
    echo "Video options:"
    echo "~~~~~~~~~~~~~~"
    echo "For VESA Video (default): Just press <Enter>"
    echo "For Chipset Video: Enter "c", then press <Enter>"
    echo "For Standard VGA: Enter "s", then press <Enter>"
    echo "For Standard VGA w/VESA: Enter "sv", then press <Enter>"
    echo -n "Video selection: "
    read voption
    if [ "$voption" = "" ]; then
	voption="/V"
	echo "VESA video selected"
    elif [ "$voption" = "c" ]; then
	voption=""
	echo "Chipset video selected"
    elif [ "$voption" = "s" ]; then
	voption="/S"
	echo "Standard VGA video selected"
    elif [ "$voption" = "sv" ]; then
	voption="/C"
	echo "Standard VGA w/VESA video selected"
    else
	voption=0
    fi
    echo
done

# boot option
boption=0
while [ "$boption" = "0" ]
do
    echo "Boot options:"
    echo "~~~~~~~~~~~~~"
    echo "For Normal option (default): Just press <Enter>"
    echo "For Partition Work option: Enter "p", then press <Enter>"
    echo -n "Boot option selection: "
    read boption
    if [ "$boption" = "" ]; then
	boption=""
	echo "Normal boot option selected"
    elif [ "$boption" = "p" ]; then
	boption="/P"
	echo "Partition Work boot option selected"
    else
	boption=0
    fi
    echo
done

if [ "$fy" = "1" ]; then
    echo "Please insert a floppy disk in /dev/$f"
    echo "Note that ALL DATA currently on the floppy disk will be LOST"
    dlm=1
    echo "Press <Enter> when ready..."
    read k
    echo "Formatting and verifying the disk in /dev/$f as requested"
    fdformat /dev/$f
    if [ "$?" != "0" ]; then
	echo
	echo "The format command FAILED...exiting"
	echo "Please make sure the disk is in the correct drive, or try another disk"
	echo
	exit 1
    fi
    sync
elif [ "$usb" = "0" ] && [ "$f" != "iso" ]; then
    echo "Please insert a *FORMATTED* floppy disk in /dev/$f"
    if [ "$dlm" = "0" ]; then
        echo "Note that ALL DATA currently on the floppy disk will be lost"
    fi
    echo "Press <Enter> when ready..."
    read k
elif [ "$usb" = "1" ] && [ "$f" != "iso" ]; then
    mdisk=`mount | grep /dev/$f > /dev/null`
    if [ "$?" = "0" ]; then
	echo "Device /dev/$f is currently MOUNTED"
	echo "Please unmount the drive and then run 'makedisk' again"
	echo
	exit 1
    fi
    showdisk=`/sbin/fdisk -l /dev/$f | grep Disk`
    if [ "$showdisk" = "" ]; then
	echo "No diskette detected in /dev/$f, or other device error"
	echo
	exit 1
    fi
    echo "You have selected the disk shown below to write the BootIt NG image to:"
    echo
    echo $showdisk
    echo
    echo "WARNING: ALL existing DATA on /dev/$f will be LOST"
    echo "Please confirm that /dev/$f shown above is the CORRECT disk"
    echo -n "Type 'yes' and then press <Enter> to confirm: "
    read confirm
    if [ "$confirm" != "yes" ]; then
	echo
	echo "Write to /dev/$f not confirmed. Exiting script"
	echo
	exit 1
    fi
fi

#echo "Creating temporary mount point at tmp1"
mkdir tmp1

#echo "Copying DISKIMG3.DAT to BOOT.BIN in tmp1 directory"
cp -a DISKIMG3.DAT tmp1/BOOT.BIN
if [ -x tmp1/BOOT.BIN ]; then chmod -x tmp1/BOOT.BIN; fi
mkdir tmp2
mount -o loop tmp1/BOOT.BIN tmp2

#echo "Copying reg.txt to BOOT.BIN if it exists"
if [ "$roption" = "y" ]; then
    if [ -e reg.txt ]; then
	cp -a reg.txt tmp2/reg.txt 2> /dev/null
    elif [ -e REG.TXT ]; then
	cp -a REG.TXT tmp2/reg.txt 2> /dev/null
    fi
fi

#echo "Copying UDMATSR.COM to BOOT.BIN if it exists"
if [ "$tpoption" = "y" ]; then
    if [ -e udmatsr.com ]; then
	cp -a udmatsr.com tmp2/udmatsr.com 2> /dev/null
    elif [ -e UDMATSR.COM ]; then
	cp -a UDMATSR.COM tmp2/udmatsr.com 2> /dev/null
    fi
fi

# create BOOTITNG.STR based on selected options
rm tmp2/BOOTITNG.STR
if [ "$tpoption" = "y" ]; then echo -e -n "UDMATSR.COM\r\n" > tmp2/BOOTITNG.STR; fi
if [ "$moption" = "y" ]; then echo -e -n "BOOTITNG.MSE\r\n" >> tmp2/BOOTITNG.STR; fi
echo -e -n "BOOTITNG $voption $boption\r\n" >> tmp2/BOOTITNG.STR

#echo "Unmounting BOOT.BIN from tmp2"
umount tmp2
rmdir tmp2

if [ "$f" != "iso" ]; then
    echo "Writing disk image to /dev/$f "
    cp tmp1/BOOT.BIN /dev/$f
    if [ "$?" != "0" ]; then
	c=1
    else
	c=0
	echo "Reading back contents of /dev/$f to verify the write"
	dd if=/dev/$f of=readback.bin bs=1k count=1440
	cmp -l readback.bin tmp1/BOOT.BIN
	if [ "$?" != "0" ]; then c=1; fi
	rm readback.bin
    fi
    sync
else 	# do iso
    echo "Running mkisofs to create bootitng.iso"
    mkisofs -o bootitng.iso -V BOOTITNG -b BOOT.BIN -c boot.cat  tmp1/
    if [ ! "$?" = "0" ]; then
	echo "Error above may be because we're on vfat...trying again using boot.bin"
	mkisofs -o bootitng.iso -V BOOTITNG -b boot.bin -c boot.cat tmp1/
	if [ ! "$?" = "0" ]; then c=2; fi
    fi
    if [ -e bootitng.iso ]; then
	if [ -x bootitng.iso ]; then chmod -x bootitng.iso; fi
    fi
fi

#echo "Removing temporary dir tmp1"
rm -r tmp1

#echo
echo
if [ "$c" = "1" ]; then
    echo "The copy to /dev/$f FAILED...exiting"
    echo "If floppy drive, please make sure a diskette is in the drive"
    echo
    exit 1
elif [ "$c" = "2" ]; then
    echo "The mkisofs command FAILED to create the bootitng.iso file"
    echo
    exit 1
elif [ "$f" = "iso" ]; then
    echo "COMPLETE - bootitng.iso has been successfully created"
    echo "The file bootitng.iso in this directory can now be burned to CD/DVD"
else    
    echo "COMPLETE - The BootIt NG boot disk in drive /dev/$f is ready to use"
fi
echo
exit 0

