#!/bin/sh

stdendinstmessage="Installation complete. Reboot, and select UNetbootin on the GRUB boot menu."

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

#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 /boot/ubnkern
#cdtu inurl=baseinitkernurl$($wgetcommand baseinitkernurl -O - | tr '<' '\n' | tr '>' '\n' | grep initrd | grep -v 'a href' | grep basedeselarch)
#cdtu $wgetcommand $inurl -O /boot/ubninit
#cdtu else
#cdtu knurl=baseinitkernurl$(curl baseinitkernurl | tr '<' '\n' | tr '>' '\n' | grep vmlinuz | grep -v 'a href' | grep basedeselarch)
#cdtu curl $knurl > /boot/ubnkern
#cdtu inurl=baseinitkernurl$(curl baseinitkernurl | tr '<' '\n' | tr '>' '\n' | grep initrd | grep -v 'a href' | grep basedeselarch)
#cdtu curl $inurl > /boot/ubninit
#cdtu fi

#isdl if [ "$(mount | grep " /boot " | sed "s/ /\n/g" | sed "s/\t/\n/g" | grep /dev | head --lines 1)" != "" ]
#isdl then
#isdl ntargetdir="/boot"
#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 /boot/ubnkern
#isdl cp /mnt/ubniso/isolinux/initrd.gz /boot/ubninit
#isdl cp /mnt/ubniso/livecd.sqfs $ntargetdir/
#isdl umount /mnt/ubniso
#isdl umount -f /mnt/ubniso
#isdl umount -l /mnt/ubniso
#isdl rm -r /mnt/ubniso

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

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"
