#!/bin/sh

#rptl fetchinitkern1(){
#rptl wgetcommand="$(whereis wget | sed 's/ /\n/g' | grep /bin | head -1)"
#rptl if [ "$wgetcommand" != "" ]
#rptl then
#rptl $wgetcommand rpubnkernurl -O $targetdir/boot/ubnkern
#rptl $wgetcommand rpubniniturl -O $targetdir/boot/ubninit
#rptl else
#rptl curl rpubnkernurl > $targetdir/boot/ubnkern
#rptl curl rpubniniturl > $targetdir/boot/ubninit
#rptl fi
#rptl }

#cdtu fetchinitkern2() {
#cdtu wgetcommand="$(whereis wget | sed 's/ /\n/g' | grep /bin | head -1)"
#cdtu if [ "$wgetcommand" != "" ]
#cdtu then
#cdtu knurl=baseinitkernurl$($wgetcommand baseinitkernurl -O - | tr '<' '\n' | tr '>' '\n' | grep vmlinuz | grep -v 'a href' | grep basedeselarch)
#cdtu $wgetcommand $knurl -O $targetdir/boot/ubnkern
#cdtu inurl=baseinitkernurl$($wgetcommand baseinitkernurl -O - | tr '<' '\n' | tr '>' '\n' | grep initrd | grep -v 'a href' | grep basedeselarch)
#cdtu $wgetcommand $inurl -O $targetdir/boot/ubninit
#cdtu else
#cdtu knurl=baseinitkernurl$(curl baseinitkernurl | tr '<' '\n' | tr '>' '\n' | grep vmlinuz | grep -v 'a href' | grep basedeselarch)
#cdtu curl $knurl > $targetdir/boot/ubnkern
#cdtu inurl=baseinitkernurl$(curl baseinitkernurl | tr '<' '\n' | tr '>' '\n' | grep initrd | grep -v 'a href' | grep basedeselarch)
#cdtu curl $inurl > $targetdir/boot/ubninit
#cdtu fi
#cdtu }

#isdl fetchinitkern3(){
#isdl if [ "$1" = "" ]
#isdl then
#isdl if [ "$(mount | grep " /boot " | sed "s/ /\n/g" | sed "s/\t/\n/g" | grep /dev | head --lines 1)" != "" ]
#isdl then
#isdl ntargetdirb="/boot"
#isdl fi
#isdl else
#isdl ntargetdir="$1"
#isdl ntargetdirb="$1"
#isdl fi
#isdl if [ ! -e /tmp/ubniso.iso ]
#isdl then
#isdl wgetcommand="$(whereis wget | sed 's/ /\n/g' | grep /bin | head -1)"
#isdl if [ "$wgetcommand" != "" ]
#isdl then
#isdl $wgetcommand isourloc -O /tmp/ubniso.iso
#isdl else
#isdl curl isourloc > /tmp/ubniso.iso
#isdl fi
#isdl fi
#isdl if [ ! -e /mnt/ubniso ]
#isdl then
#isdl mkdir /mnt/ubniso
#isdl fi
#isdl mount /tmp/ubniso.iso -o loop /mnt/ubniso
#isdl cp /mnt/ubniso/isolinux/vmlinuz $ntargetdir/boot/ubnkern
#isdl cp /mnt/ubniso/isolinux/initrd.gz $ntargetdir/boot/ubninit
#isdl cp /mnt/ubniso/livecd.sqfs $ntargetdirb/
#isdl umount /mnt/ubniso
#isdl umount -f /mnt/ubniso
#isdl umount -l /mnt/ubniso
#isdl rm -r /mnt/ubniso
#isdl }

ubnappname='./unetbootin-replacewithubnversion.sh'
stdendinstmessage="Installation complete. Reboot, and select UNetbootin on the GRUB boot menu."

# Check for root privs

if [ "$(whoami)" != "root" ]; then
echo """This script must be run as root. Use 'sudo' or 'su' to become root

To run with sudo, enter:

sudo $ubnappname

To run with su, enter:

su
$ubnappname

"""
zenity --info --text="""This script must be run as root. Use 'sudo' or 'su' to become root

To run with sudo, enter:

sudo $ubnappname

To run with su, enter:

su
$ubnappname

"""
exit 0
fi

# End root priv check

# Begin parameter checks

fdiskcommand=$(whereis fdisk | sed 's/ /\n/g' | grep /sbin | head -1)
if [ "$fdiskcommand" = "" ]
then
fdiskcommand="/sbin/fdisk"
fi

sfdiskcommand=$(whereis sfdisk | sed 's/ /\n/g' | grep /sbin | head -1)
if [ "$sfdiskcommand" = "" ]
then
sfdiskcommand="/sbin/sfdisk"
fi

mke2fscommand=$(whereis mke2fs | sed 's/ /\n/g' | grep /sbin | head -1)
if [ "$mke2fscommand" = "" ]
then
mke2fscommand="/sbin/mke2fs"
fi

mkdosfscommand=$(whereis mkdosfs | sed 's/ /\n/g' | grep /sbin | head -1)
if [ "$mkdosfscommand" = "" ]
then
mkdosfscommand="/sbin/mkdosfs"
fi

lilocommand=$(whereis lilo | sed 's/ /\n/g' | grep /sbin | head -1)
if [ "$lilocommand" != "" ]
then
bootloader="lilo"
fi
if [ -e "/sbin/lilo" ]
then
bootloader="lilo"
lilocommand="/sbin/lilo"
fi
if [ -e "/usr/sbin/lilo" ]
then
bootloader="lilo"
lilocommand="/usr/sbin/lilo"
fi

grubcommand=$(whereis grub | sed 's/ /\n/g' | grep /sbin | head -1)
if [ "$grubcommand" != "" ]
then
bootloader="grub"
fi
if [ -e "/sbin/grub" ]
then
bootloader="grub"
grubcommand="/sbin/grub"
fi
if [ -e "/usr/sbin/grub" ]
then
bootloader="grub"
grubcommand="/usr/sbin/grub"
fi
grubinstallcommand=$(whereis grub-install | sed 's/ /\n/g' | grep /sbin | head -1)
if [ -e "$grubcommand-install" ]
then
grubinstallcommand="$grubcommand-install"
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'bootloader=' | head -1 | sed 's/bootloader=//g')" = "lilo" ]
then
bootloader="lilo"
if [ -e "/sbin/lilo" ]
then
lilocommand="/sbin/lilo"
fi
if [ -e "/usr/sbin/lilo" ]
then
lilocommand="/usr/sbin/lilo"
fi
if [ "$lilocommand" = "" ]
then
lilocommand="/sbin/lilo"
fi
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'bootloader=' | head -1 | sed 's/bootloader=//g')" = "grub" ]
then
bootloader="grub"
if [ -e "/sbin/grub" ]
then
grubcommand="/sbin/grub"
fi
if [ -e "/usr/sbin/grub" ]
then
grubcommand="/usr/sbin/grub"
fi
if [ "$grubcommand" = "" ]
then
grubcommand="/sbin/grub"
fi
if [ -e "/sbin/grub-install" ]
then
grubinstallcommand="/sbin/grub-install"
fi
if [ -e "/usr/sbin/grub-install" ]
then
grubinstallcommand="/usr/sbin/grub-install"
fi
if [ "$grubinstallcommand" = "" ]
then
grubinstallcommand="/sbin/grub-install"
fi
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'bootloader=' | head -1 | sed 's/bootloader=//g')" = "syslinux" ]
then
bootloader="syslinux"
syslinuxcommand="$(whereis syslinux | sed 's/ /\n/g' | grep bin | head -1)"
if [ -e "$syslinuxcommand" ]
then
echo ""
else
syslinuxcommand="/usr/bin/syslinux"
echo "Could not find syslinux command path. Attempting to use $syslinuxcommand instead, please install syslinux if this fails or specify a different bootloader using the bootloader=grub or bootloader=lilo option instead."
fi
fi

if [ "$bootloader" = "" ]
then
echo "Could not find a valid bootloader, either lilo or grub. Attempting to use grub as a fallback, please install a bootloader if this fails or use the bootloader=lilo option to use lilo instead, or use the bootloader=syslinux to use syslinux instead."
bootloader="grub"
if [ -e "/sbin/grub" ]
then
grubcommand="/sbin/grub"
fi
if [ -e "/usr/sbin/grub" ]
then
grubcommand="/usr/sbin/grub"
fi
if [ "$grubcommand" = "" ]
then
grubcommand="/sbin/grub"
fi
if [ -e "/sbin/grub-install" ]
then
grubinstallcommand="/sbin/grub-install"
fi
if [ -e "/usr/sbin/grub-install" ]
then
grubinstallcommand="/usr/sbin/grub-install"
fi
if [ "$grubinstallcommand" = "" ]
then
grubinstallcommand="/sbin/grub-install"
fi
grubfallback=" grub bootloader fallback"
fi

# Check if this is a host or livecd installation

if [ "$(echo $@ | grep 'installmode=tohost')" != "" ]
then
installmode="tohost"
fi

if [ "$(echo $@ | grep 'installmode=nohost')" != "" ]
then
installmode="nohost"
fi

if [ "$(echo $@ | grep 'installmode=usbdrive')" != "" ]
then
installmode="usbdrive"
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'forcepartition=' | head -1 | sed 's/forcepartition=//g')" = "yes" ]
then
forcepartition="yes"
fi

if [ "$installmode" = "" ]
then
posspartsavail=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':')
echo """

If you are running this script from a host, hard-drive Linux install, and want the GRUB bootloader installed in /boot to be used, enter:

$ubnappname installmode=tohost


Otherwise, if you are running this script from a liveCD or other live, non-hard drive media, or the installmode=tohost option fails, or you want to specify your target partition (targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1)) or (optionally) the bootloader (bootloader=grub, bootloader=lilo, or bootloader=syslinux), enter, in addition to the targetpartition and formatpartition options:

$ubnappname installmode=nohost targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes


Otherwise, if you want to install to a USB drive, enter, in addition to the targetpartition and formatpartition options:

$ubnappname installmode=usbdrive targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes

The following partitions are available for use:
$posspartsavail

"""
zenity --info --text="""If you are running this script from a host, hard-drive Linux install, and want the GRUB bootloader installed in /boot to be used, enter:

$ubnappname installmode=tohost


Otherwise, if you are running this script from a liveCD or other live, non-hard drive media, or the installmode=tohost option fails, or you want to specify your target partition (targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1)) or (optionally) the bootloader (bootloader=grub, bootloader=lilo, or bootloader=syslinux), enter, in addition to the targetpartition and formatpartition options:

$ubnappname installmode=nohost targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes


Otherwise, if you want to install to a USB drive, enter, in addition to the targetpartition and formatpartition options:

$ubnappname installmode=usbdrive targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes

The following partitions are available for use:
$posspartsavail"""
exit 0
fi

echo "Using installmode=$installmode"

# End installmode= installmode check

# Begin usbdrive installmode

if [ "$installmode" = "usbdrive" ]
then
# Check the target partition

availpartitions=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':')

if [ "$(echo $@ | grep 'targetpartition=')" != "" ]
then
proposedpartition="$(echo $@ | sed 's/ /\n/g' | grep 'targetpartition=' | head -1 | sed 's/targetpartition=//g')"
fi

if [ "$proposedpartition" = "" ]
then
echo """

Specify a partition to use for the installation. For example, to use $($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1), enter, in addition to the formatpartition=yes or formatpartition=no option:

$ubnappname installmode=$installmode targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes



The following partitions are available for use:

$availpartitions

"""
exit 0
fi

if [ "$(echo $availpartitions | grep $proposedpartition)" = "" ]
then
if [ "$forcepartition" = "yes" ]
then
echo "Using forcepartition=$forcepartition"
else
echo "The specified partition is invalid. Specify, after the 'targetpartition=' option, one of the following:

$availpartitions

Use the command '$fdiskcommand -l' for details on partitions

"
exit 0
fi
fi

if [ "$(echo $availpartitions | grep $proposedpartition)" != "" ]
then
targetpartition="$proposedpartition"
else
if [ "$forcepartition" = "yes" ]
then
targetpartition="$proposedpartition"
fi
fi

if [ "$targetpartition" = "" ]
then
echo """

Specify a partition to use for the installation. For example, to use $($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1), enter, in addition to the formatpartition=yes or formatpartition=no option:

$ubnappname installmode=$installmode targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes



The following partitions are available for use:

$availpartitions

"""
exit 0
fi

echo "Using targetpartition=$targetpartition"

# End targetpartition= check

# Check if partition is to be formatted or not

if [ "$(echo $@ | grep 'formatpartition=')" = "" ]
then
echo """

Specify whether or not to format the target partition. If you have already formatted the target partition and do not want to do so again, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=no



Otherwise, to format the target partition, erasing all data on that partition, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=yes

"""
exit 0
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'formatpartition=' | sed 's/formatpartition=//g')" = "yes" ]
then
formatpartition="yes"
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'formatpartition=' | sed 's/formatpartition=//g')" = "no" ]
then
formatpartition="no"
fi

if [ "$formatpartition" = "" ]
then
echo """

Specify whether or not to format the target partition. If you have already formatted the target partition and do not want to do so again, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=no



Otherwise, to format the target partition, erasing all data on that partition, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=yes

"""
exit 0
fi

echo "Using formatpartition=$formatpartition"

# End formatpartition= check

bootloader="grub"

echo "Using bootloader=$bootloader"

if [ "$bootloader" = "grub" ]
then
echo "Using$grubfallback grubcommand=$grubcommand"
fi

# End parameter checks

# Begin raw commands and code

echo "Unmounting target partition"

if [ -e /media ]
then
echo ""
else
mkdir /media
fi

umount $targetpartition
if [ -e /media/ubntemp ]
then
umount /media/ubntemp
fi
umount -f $targetpartition
if [ -e /media/ubntemp ]
then
umount -f /media/ubntemp
fi
umount -l $targetpartition
if [ -e /media/ubntemp ]
then
umount -l /media/ubntemp
fi

# Begin formatting partitions

if [ "$formatpartition" = "yes" ]
then

echo "Changing partition id to 6, FAT16"

$sfdiskcommand --change-id $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2)) $(echo $targetpartition | tail --bytes 2 | head --bytes 1) 6

echo "Formatting partition as FAT"
$mkdosfscommand $targetpartition
fi

vfatfstyperev=$(vol_id $targetpartition | grep ID_FS_VERSION | sed 's/=/\n/g' | tail --lines 1 | sed s/FAT//g | tail --bytes 3 | head --bytes 2)

if [ "$vfatfstyperev" = "12" ]
then
echo "Changing partition id to 1, FAT12"
$sfdiskcommand --change-id $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2)) $(echo $targetpartition | tail --bytes 2 | head --bytes 1) 1
fi

if [ "$vfatfstyperev" = "32" ]
then
echo "Changing partition id to b, FAT32"
$sfdiskcommand --change-id $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2)) $(echo $targetpartition | tail --bytes 2 | head --bytes 1) b
fi

# End formatting partitions

if [ "$(mount | grep $targetpartition)" != "" ]
then
# Still mounted, could not unmount
targetdir="$(mount | grep $targetpartition | sed 's/ /\n/g' | head -3 | tail -1)"
else
# Mount at /media/ubntemp
targetdir=/media/ubntemp
if [ -e $targetdir ]
then
echo ""
else
mkdir $targetdir
fi
mount $targetpartition $targetdir
fi

if [ -e $targetdir/boot ]
then
echo ""
else
mkdir $targetdir/boot
fi

cp ./ubnkern $targetdir/boot/
cp ./ubninit $targetdir/boot/

#rptl fetchinitkern1

#cdtu fetchinitkern2

#isdl fetchinitkern3 "$targetdir"

# Begin grub install

hostosdrivenum=$(echo $targetpartition | tail --bytes 3 | head --bytes 1 | sed s/a/0/ | sed s/b/1/ | sed s/c/2/ | sed s/e/4/ | sed s/f/5/ | sed s/g/6/ | sed s/h/7/ | sed s/i/8/ | sed s/j/9/ | sed s/k/10/ | sed s/l/11/ | sed s/m/12/ | sed s/n/13/ | sed s/o/14/ | sed s/p/15/ | sed s/q/16/ | sed s/r/17/ | sed s/s/18/ | sed s/t/19/ | sed s/u/20/ | sed s/v/21/ | sed s/w/22/ | sed s/x/23/ | sed s/y/24/ | sed s/z/25/)
hostospartnum=$(expr $(echo $targetpartition | tail --bytes 2 | head --bytes 1) - 1)
hostosgrubnum="(hd$(echo $hostosdrivenum),$(echo $hostospartnum))"

hostosdrivestandalone="(hd$(echo $hostosdrivenum))"

$grubinstallcommand --root-directory="$targetdir" $hostosdrivestandalone

echo "$hostosdrivestandalone   $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2))" > $targetdir/boot/grub/device.map

$grubcommand --batch --device-map="$targetdir/boot/grub/device.map" << EOT
root $hostosgrubnum
setup $hostosdrivestandalone
quit
EOT

if [ -e $targetdir/boot/grub/menu.lst ]
then
cp $targetdir/boot/grub/menu.lst $targetdir/boot/grub/menu-bkubn.lst
fi

touch $targetdir/boot/grub/menu.lst
echo \
"""
default 0
timeout 10

title UNetbootin-replacewithubnversion
kernel /boot/ubnkern
initrd /boot/ubninit
boot
""" > $targetdir/boot/grub/menu.lst

if [ "$(cat $targetdir/boot/ubnkern)" = "" ]
then
echo "Installation failed, kernel was not downloaded or extracted properly, make sure your internet connection is working and that there is free space in /boot and try again"
exit 1
fi

if [ "$(cat $targetdir/boot/ubninit)" = "" ]
then
echo "Installation failed, initrd was not downloaded or extracted properly, make sure your internet connection is working and that there is free space in /boot and try again"
exit 1
fi

umount $targetpartition
umount -f $targetpartition
umount -l $targetpartition

echo "$stdendinstmessage"
zenity --info --text="$stdendinstmessage"

fi

# End usbdrive installmode

# Begin nohost livecd installmode


if [ "$installmode" = "nohost" ]
then

# Check the target partition

availpartitions=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':')

if [ "$(echo $@ | grep 'targetpartition=')" != "" ]
then
proposedpartition="$(echo $@ | sed 's/ /\n/g' | grep 'targetpartition=' | head -1 | sed 's/targetpartition=//g')"
fi

if [ "$proposedpartition" = "" ]
then
echo """

Specify a partition to use for the installation. For example, to use $($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1), enter, in addition to the formatpartition=yes or formatpartition=no option:

$ubnappname installmode=$installmode targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes



The following partitions are available for use:

$availpartitions

"""
exit 0
fi

if [ "$(echo $availpartitions | grep $proposedpartition)" = "" ]
then
if [ "$forcepartition" = "yes" ]
then
echo "Using forcepartition=$forcepartition"
else
echo "The specified partition is invalid. Specify, after the 'targetpartition=' option, one of the following:

$availpartitions

Use the command '$fdiskcommand -l' for details on partitions

"
exit 0
fi
fi

if [ "$(echo $availpartitions | grep $proposedpartition)" != "" ]
then
targetpartition="$proposedpartition"
else
if [ "$forcepartition" = "yes" ]
then
targetpartition="$proposedpartition"
fi
fi

if [ "$targetpartition" = "" ]
then
echo """

Specify a partition to use for the installation. For example, to use $($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1), enter, in addition to the formatpartition=yes or formatpartition=no option:

$ubnappname installmode=$installmode targetpartition=$($fdiskcommand -l | sed 's/ /\n/g' | grep /dev | grep -v ':' | head -1) formatpartition=yes



The following partitions are available for use:

$availpartitions

"""
exit 0
fi

echo "Using targetpartition=$targetpartition"

# End targetpartition= check

# Check if partition is to be formatted or not

if [ "$(echo $@ | grep 'formatpartition=')" = "" ]
then
echo """

Specify whether or not to format the target partition. If you have already formatted the target partition and do not want to do so again, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=no



Otherwise, to format the target partition, erasing all data on that partition, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=yes

"""
exit 0
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'formatpartition=' | sed 's/formatpartition=//g')" = "yes" ]
then
formatpartition="yes"
fi

if [ "$(echo $@ | sed 's/ /\n/g' | grep 'formatpartition=' | sed 's/formatpartition=//g')" = "no" ]
then
formatpartition="no"
fi

if [ "$formatpartition" = "" ]
then
echo """

Specify whether or not to format the target partition. If you have already formatted the target partition and do not want to do so again, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=no



Otherwise, to format the target partition, erasing all data on that partition, enter:

$ubnappname installmode=$installmode targetpartition=$targetpartition formatpartition=yes

"""
exit 0
fi

echo "Using formatpartition=$formatpartition"

# End formatpartition= check

echo "Using bootloader=$bootloader"

if [ "$bootloader" = "grub" ]
then
echo "Using$grubfallback grubcommand=$grubcommand"
fi

if [ "$bootloader" = "lilo" ]
then
echo "Using lilocommand=$lilocommand"
fi

if [ "$bootloader" = "syslinux" ]
then
echo "Using syslinuxcommand=$syslinuxcommand"
fi

# End parameter checks

# Begin raw commands and code

echo "Unmounting target partition"

if [ -e /media ]
then
echo ""
else
mkdir /media
fi

umount $targetpartition
if [ -e /media/ubntemp ]
then
umount /media/ubntemp
fi
umount -f $targetpartition
if [ -e /media/ubntemp ]
then
umount -f /media/ubntemp
fi
umount -l $targetpartition
if [ -e /media/ubntemp ]
then
umount -l /media/ubntemp
fi

# Begin formatting partitions

if [ "$formatpartition" = "yes" ]
then

echo "Changing partition id to 6, FAT16"

$sfdiskcommand --change-id $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2)) $(echo $targetpartition | tail --bytes 2 | head --bytes 1) 6

echo "Formatting partition as FAT"
$mkdosfscommand $targetpartition
fi

vfatfstyperev=$(vol_id $targetpartition | grep ID_FS_VERSION | sed 's/=/\n/g' | tail --lines 1 | sed s/FAT//g | tail --bytes 3 | head --bytes 2)

if [ "$vfatfstyperev" = "12" ]
then
echo "Changing partition id to 1, FAT12"
$sfdiskcommand --change-id $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2)) $(echo $targetpartition | tail --bytes 2 | head --bytes 1) 1
fi

if [ "$vfatfstyperev" = "32" ]
then
echo "Changing partition id to b, FAT32"
$sfdiskcommand --change-id $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2)) $(echo $targetpartition | tail --bytes 2 | head --bytes 1) b
fi

# End formatting partitions

if [ "$(mount | grep $targetpartition)" != "" ]
then
# Still mounted, could not unmount
targetdir="$(mount | grep $targetpartition | sed 's/ /\n/g' | head -3 | tail -1)"
else
# Mount at /media/ubntemp
targetdir=/media/ubntemp
if [ -e $targetdir ]
then
echo ""
else
mkdir $targetdir
fi
mount $targetpartition $targetdir
umntendinst=yes
fi

if [ -e $targetdir/boot ]
then
echo ""
else
mkdir $targetdir/boot
fi

if [ -e $targetdir/boot/grub ]
then
echo ""
else
mkdir $targetdir/boot/grub
fi

cp ./ubnkern $targetdir/boot/
cp ./ubninit $targetdir/boot/

#rptl fetchinitkern1

#cdtu fetchinitkern2

#isdl fetchinitkern3 "$targetdir"

# Begin grub install

if [ "$bootloader" = "grub" ]
then


hostosdrivenum=$(echo $targetpartition | tail --bytes 3 | head --bytes 1 | sed s/a/0/ | sed s/b/1/ | sed s/c/2/ | sed s/e/4/ | sed s/f/5/ | sed s/g/6/ | sed s/h/7/ | sed s/i/8/ | sed s/j/9/ | sed s/k/10/ | sed s/l/11/ | sed s/m/12/ | sed s/n/13/ | sed s/o/14/ | sed s/p/15/ | sed s/q/16/ | sed s/r/17/ | sed s/s/18/ | sed s/t/19/ | sed s/u/20/ | sed s/v/21/ | sed s/w/22/ | sed s/x/23/ | sed s/y/24/ | sed s/z/25/)
hostospartnum=$(expr $(echo $targetpartition | tail --bytes 2 | head --bytes 1) - 1)
hostosgrubnum="(hd$(echo $hostosdrivenum),$(echo $hostospartnum))"

hostosdrivestandalone="(hd$(echo $hostosdrivenum))"

$grubinstallcommand --root-directory="$targetdir" $hostosdrivestandalone

echo "$hostosdrivestandalone   $(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2))" > $targetdir/boot/grub/device.map

$grubcommand --batch --device-map="$targetdir/boot/grub/device.map" << EOT
root $hostosgrubnum
setup $hostosdrivestandalone
quit
EOT

if [ -e $targetdir/boot/grub/menu.lst ]
then
cp $targetdir/boot/grub/menu.lst $targetdir/boot/grub/menu-bkubn.lst
fi

echo \
"""
title UNetbootin-replacewithubnversion
root $hostosgrubnum
kernel /boot/ubnkern
initrd /boot/ubninit
boot
""" >> $targetdir/boot/grub/menu.lst

sed -i "s/$(cat $targetdir/boot/grub/menu.lst | grep timeout | tail --lines 1)/timeout \t10/" $targetdir/boot/grub/menu.lst
sed -i "s/#hiddenmenu/knlknelkntxte/g" $targetdir/boot/grub/menu.lst
sed -i "s/## hiddenmenu/knlknelkntxtj/g" $targetdir/boot/grub/menu.lst
sed -i "s/hiddenmenu/#hiddenmenu/g" $targetdir/boot/grub/menu.lst
sed -i "s/knlknelkntxtj/## hiddenmenu/g" $targetdir/boot/grub/menu.lst
sed -i "s/knlknelkntxte/#hiddenmenu/g" $targetdir/boot/grub/menu.lst

fi

# End grub install

# Begin lilo install

if [ "$bootloader" = "lilo" ]
then

cp ./ubnldr.exe $targetdir/boot/ubnldr.exe

if [ -e $targetdir/boot/grub/menu.lst ]
then
cp $targetdir/boot/grub/menu.lst $targetdir/boot/grub/menu-bkubn.lst
fi

echo \
"""
title UNetbootin-replacewithubnversion
find --set-root /boot/ubnkern
kernel /boot/ubnkern
initrd /boot/ubninit
boot
""" >> $targetdir/boot/grub/menu.lst

rawdrivedev=$(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2))
echo """
boot=$rawdrivedev
prompt
timeout=50
default=unetbootin

image=$targetdir/boot/ubnldr.exe
	label=unetbootin
	root=$targetpartition
""" > $targetdir/lilo.conf
lilo -C $targetdir/lilo.conf
fi

# End lilo install

# Begin syslinux install

if [ "$bootloader" = "syslinux" ]
then

cp ./ubnldr.exe $targetdir/boot/ubnldr.exe

if [ -e $targetdir/boot/grub/menu.lst ]
then
cp $targetdir/boot/grub/menu.lst $targetdir/boot/grub/menu-bkubn.lst
fi

echo \
"""
title UNetbootin-replacewithubnversion
find --set-root /boot/ubnkern
kernel /boot/ubnkern
initrd /boot/ubninit
boot
""" >> $targetdir/boot/grub/menu.lst

rawdrivedev=$(echo $targetpartition | head --bytes $(expr $(echo $targetpartition | wc -c) - 2))
echo """
DEFAULT unetbootin

LABEL unetbootin
KERNEL /boot/ubnldr.exe
""" > $targetdir/syslinux.cfg
$syslinuxcommand -s $targetpartition
sfdisk $rawdrivedev -A$(echo $targetpartition | tail --bytes 2 | head --bytes 1)
fi

# End syslinux install

# End raw commands and code

if [ "$(cat $targetdir/boot/ubnkern)" = "" ]
then
echo "Installation failed, kernel was not downloaded or extracted properly, make sure your internet connection is working and that there is free space in /boot and try again"
exit 1
fi

if [ "$(cat $targetdir/boot/ubninit)" = "" ]
then
echo "Installation failed, initrd was not downloaded or extracted properly, make sure your internet connection is working and that there is free space in /boot and try again"
exit 1
fi

if [ "$umntendinst" = "yes" ]
then
umount $targetpartition
umount -f $targetpartition
umount -l $targetpartition
fi

echo "$stdendinstmessage"
zenity --info --text="$stdendinstmessage"

fi

# End nohost livecd installmode

# Begin tohost hard-drive installmode

if [ "installmode" = "tohost" ]
then

cp ./ubnkern /boot/
cp ./ubninit /boot/

#rptl fetchinitkern1

#cdtu fetchinitkern2

#isdl fetchinitkern3

cp /boot/grub/menu.lst /boot/grub/menu-bkubn.lst

if [ -e /media/host/wubi/disks ]
then

# wubi 7.04 install

hostosroot=$(cat /proc/mounts | grep /media/host | sed "s/\t/\n/g" | sed "s/ /\n/g" | head --lines 1 | grep /dev/)
hostosdrivenum=$(echo $hostosroot | tail --bytes 3 | head --bytes 1 | sed -e s/a/0/ -e s/b/1/ -e s/c/2/ -e s/e/4/ -e s/f/5/ -e s/g/6/ -e s/h/7/ -e s/i/8/ -e s/j/9/ -e s/k/10/ -e s/l/11/ -e s/m/12/ -e s/n/13/ -e s/o/14/ -e s/p/15/ -e s/q/16/ -e s/r/17/ -e s/s/18/ -e s/t/19/ -e s/u/20/ -e s/v/21/ -e s/w/22/ -e s/x/23/ -e s/y/24/ -e s/z/25/)
hostospartnum=$(expr $(echo $hostosroot | tail --bytes 2 | head --bytes 1) - 1)
hostosgrubnum="(hd$(echo $hostosdrivenum),$(echo $hostospartnum))"

echo \
"
title UNetbootin-replacewithubnversion
   root $hostosgrubnum
   kernel /wubi/boot/ubnkern
   initrd /wubi/boot/ubninit
   boot
" >> /boot/grub/menu.lst

instcmplt="yes"
fi

if [ -e /host/ubuntu/disks ]
then

# wubi 7.10 install

hostosroot=$(echo /dev/$(file $(cat /proc/mounts | grep /host | sed "s/\t/\n/g" | sed "s/ /\n/g" | head --lines 1 | grep /dev/) | sed "s/ /\n/g" | tail -1 | sed "s/\//\n/g" | tail -1 | sed "s/'//g"))
hostosdrivenum=$(echo $hostosroot | tail --bytes 3 | head --bytes 1 | sed -e s/a/0/ -e s/b/1/ -e s/c/2/ -e s/e/4/ -e s/f/5/ -e s/g/6/ -e s/h/7/ -e s/i/8/ -e s/j/9/ -e s/k/10/ -e s/l/11/ -e s/m/12/ -e s/n/13/ -e s/o/14/ -e s/p/15/ -e s/q/16/ -e s/r/17/ -e s/s/18/ -e s/t/19/ -e s/u/20/ -e s/v/21/ -e s/w/22/ -e s/x/23/ -e s/y/24/ -e s/z/25/)
hostospartnum=$(expr $(echo $hostosroot | tail --bytes 2 | head --bytes 1) - 1)
hostosgrubnum="(hd$(echo $hostosdrivenum),$(echo $hostospartnum))"

echo \
"
title UNetbootin-replacewithubnversion
   root $hostosgrubnum
   kernel /ubuntu/disks/boot/ubnkern
   initrd /ubuntu/disks/boot/ubninit
   boot
" >> /boot/grub/menu.lst

instcmplt="yes"
fi

if [ "$instcmplt" != "yes" ]
then

# standard install

hostosroot=$(mount | grep " /boot " | sed "s/ /\n/g" | sed "s/\t/\n/g" | grep /dev | head --lines 1)
if [ "$hostosroot" = "" ]
then
hostosroot=$(mount | grep " / " | sed "s/ /\n/g" | sed "s/\t/\n/g" | grep /dev | head --lines 1)
grbootsup="/boot"
fi
hostosdrivenum=$(echo $hostosroot | tail --bytes 3 | head --bytes 1 | sed -e s/a/0/ -e s/b/1/ -e s/c/2/ -e s/e/4/ -e s/f/5/ -e s/g/6/ -e s/h/7/ -e s/i/8/ -e s/j/9/ -e s/k/10/ -e s/l/11/ -e s/m/12/ -e s/n/13/ -e s/o/14/ -e s/p/15/ -e s/q/16/ -e s/r/17/ -e s/s/18/ -e s/t/19/ -e s/u/20/ -e s/v/21/ -e s/w/22/ -e s/x/23/ -e s/y/24/ -e s/z/25/)
hostospartnum=$(expr $(echo $hostosroot | tail --bytes 2 | head --bytes 1) - 1)
hostosgrubnum="(hd$(echo $hostosdrivenum),$(echo $hostospartnum))"

echo \
"
title UNetbootin-replacewithubnversion
root $hostosgrubnum
kernel $grbootsup/ubnkern
initrd $grbootsup/ubninit
boot
" >> /boot/grub/menu.lst

fi

touch /usr/bin/unetbootin-uninst
echo \
'''
#!/bin/sh
if [ "$(echo $USER)" != "root" ]; then
echo "This script must be run as root. Use 'sudo' or 'su' to become root"
zenity --info --text="This script must be run as root. Use 'sudo' or 'su' to become root"
exit 0
fi
rm /boot/ubnkern
rm /boot/ubninit
rm /boot/ubnldr.exe
rm /boot/livecd.sqfs
rm /livecd.sqfs
mv /boot/grub/menu-bkubn.lst /boot/grub/menu.lst
if [ -e /boot/grub/grub-bkubn.conf ]
then
mv /boot/grub/grub-bkubn.conf /boot/grub/grub.conf
fi
rm /usr/bin/unetbootin-uninst
echo "UNetbootin has been successfully uninstalled"
zenity --info --text="UNetbootin has been successfully uninstalled"
''' >> /usr/bin/unetbootin-uninst
chmod +x /usr/bin/unetbootin-uninst

sed -i "s/$(cat /boot/grub/menu.lst | grep timeout | tail --lines 1)/timeout \t10/" /boot/grub/menu.lst
sed -i "s/#hiddenmenu/knlknelkntxte/g" /boot/grub/menu.lst
sed -i "s/## hiddenmenu/knlknelkntxtj/g" /boot/grub/menu.lst
sed -i "s/hiddenmenu/#hiddenmenu/g" /boot/grub/menu.lst
sed -i "s/knlknelkntxtj/## hiddenmenu/g" /boot/grub/menu.lst
sed -i "s/knlknelkntxte/#hiddenmenu/g" /boot/grub/menu.lst

if [ -e /boot/grub/grub.conf ]
then
mv /boot/grub/grub.conf /boot/grub/grub-bkubn.conf
cat /boot/grub/menu.lst /boot/grub/grub-bkubn.conf > /boot/grub/grub.conf
fi

if [ "$(cat /boot/ubnkern)" = "" ]
then
echo "Installation failed, kernel was not downloaded or extracted properly, make sure your internet connection is working and that there is free space in /boot and try again"
exit 1
fi

if [ "$(cat /boot/ubninit)" = "" ]
then
echo "Installation failed, initrd was not downloaded or extracted properly, make sure your internet connection is working and that there is free space in /boot and try again"
exit 1
fi

echo "$stdendinstmessage"
zenity --info --text="$stdendinstmessage"

fi

# End tohost hard-drive installmode
