#!/bin/bash
#
#
#  remastersys-grub-restore is a utility to restore grub on a PC
#
#
#  Created by Tony "Fragadelic" Brijeski
#
#  Copyright 2008,2009,2010 Under the GNU GPL2 License
#
#  Originally Created September 12th,2008
#
#
#
#

#  This script requires either zenity or dialog to run
#

# checking to make sure script is running with root privileges

testroot="`whoami`"

if [ "$testroot" != "root" ]; then
echo " Must be run as root - exiting"
exit 1
fi

# set options depending on mode - text or gui

testdialog=`which dialog`
DIALOG="`which dialog`"
HEIGHT="17"
WIDTH="50"
MENUHEIGHT="12"
TITLE="--title "
TEXT=""
ENTRY="--inputbox "
MENU="--menu"
YESNO="--yesno "
MSGBOX="--msgbox "
PASSWORD="--passwordbox "
PARTITIONPROG="cfdisk"
TITLETEXT="Remastersys Grub Restore"

if [ "$1" = "gui" ]; then
GUI="$DISPLAY"
fi

if [ "$GUI" != "" ]; then

testzenity=`which zenity`

if [ "$testzenity" = "" ]; then
echo " Zenity not present - proceeding to install zenity"
apt-get -y -q install zenity
testzenity=`which zenity`
fi



if [ "$testzenity" != "" ]; then
DIALOG="`which zenity` --width=640 --height=400"
TITLE="--title="
TEXT="--text="
ENTRY="--entry "
ENTRYTEXT="--entry-text "
MENU="--list --column=Pick --column=Info"
YESNO="--question "
MSGBOX="--info "
PASSWORD="--entry --hide-text "
PARTITIONPROG="gparted"
TITLETEXT="Remastersys Grub Restore"
fi

fi

if [ "$DIALOG" = "" ]; then
echo "Cannot find dialog or zenity.  Exiting."
exit 1
fi


if [ "$GUI" = "" ]; then

#
#TEXT MODE
#
#

$DIALOG $TITLE"$TITLETEXT" $YESNO $TEXT"\n\nThis is a grub restore utility.\n\n\n\nDo you want to continue?" $HEIGHT $WIDTH

if [ $? != 0 ]; then
exit 0
fi

os-prober > /tmp/os-prober.remastersys



PARTITIONS=`cat /tmp/os-prober.remastersys | awk -F ":" '{print $1}' | awk -F "/" '{print $3}'`



if [ ! -d /live ]; then

partitiontext="current The_Partition_this_install_is_on"

else

for i in `echo $PARTITIONS`; do
LINE=`cat /tmp/os-prober.remastersys | grep "$i"`
DEV=`echo $LINE | awk -F ":" '{print $1}' | awk -F "/" '{print $3}'`
OSTITLE=`echo $LINE | awk -F ":" '{print $2}'`
OSTITLE=`echo $OSTITLE | sed -r "s/ /_/g"`
OSTYPE=`echo $LINE | awk -F ":" '{print $4}'`
if [ "$OSTYPE" = "linux" ]; then
partitiontext="$partitiontext $DEV $OSTITLE"
fi
done

fi

rm /tmp/os-prober.remastersys

$DIALOG $TITLE"$TITLETEXT" $MENU $TEXT"\nPlease select a partition to install or restore grub to. If the only option you see is to Quit then no valid partitions were found." $HEIGHT $WIDTH $MENUHEIGHT Exit "Quit" $partitiontext 2>/tmp/choice.remastersys

if [ "$?" = "0" ]; then
TARGETPART=`cat /tmp/choice.remastersys`
else
TARGETPART="Exit"
fi
rm /tmp/choice.remastersys

if [ "$TARGETPART" = "Exit" ]; then
  $DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n\nQuiting now." $HEIGHT $WIDTH
  exit 1
elif [ "$TARGETPART" = "current" ]; then
TARGETPART=`mount | grep " / " | awk '{print $1}' | awk -F "/" '{print $3}'`
fi


#grub location
$DIALOG $TITLE"$TITLETEXT" $MENU $TEXT"\nPlease select where to install grub to.\n" $HEIGHT $WIDTH $MENUHEIGHT mbr "Master Boot Record of first drive" root "Root partition of $TARGETPART" 2>/tmp/choice.remastersys

if [ "$?" = "0" ]; then
GRUBLOCTEST=`cat /tmp/choice.remastersys`
fi
rm /tmp/choice.remastersys

if [ "$GRUBLOCTEST" = "root" ]; then
GRUBLOCTEXT="root partition of $TARGETPART"
GRUBLOC="root"
else
GRUBLOCTEXT="master boot record"
GRUBLOC=""
fi


#END TEXT MODE

else

#
#GUI MODE
#
#inform them what this is and ask if they want to continue

$DIALOG $TITLE"$TITLETEXT" $YESNO $TEXT"\n\nThis is a grub restore utility.\n\n\n\nDo you want to continue?"

if [ $? != 0 ]; then
exit 0
fi

os-prober > /tmp/os-prober.remastersys

PARTITIONS=`cat /tmp/os-prober.remastersys | awk -F ":" '{print $1}' | awk -F "/" '{print $3}'`

if [ ! -d /live ]; then

partitiontext="current The_Partition_this_install_is_on"

else

for i in `echo $PARTITIONS`; do
LINE=`cat /tmp/os-prober.remastersys | grep "$i"`
DEV=`echo $LINE | awk -F ":" '{print $1}' | awk -F "/" '{print $3}'`
OSTITLE=`echo $LINE | awk -F ":" '{print $2}'`
OSTITLE=`echo $OSTITLE | sed -r "s/ /_/g"`
OSTYPE=`echo $LINE | awk -F ":" '{print $4}'`
if [ "$OSTYPE" = "linux" ]; then
partitiontext="$partitiontext $DEV $OSTITLE"
fi
done

fi

rm /tmp/os-prober.remastersys

TARGETPART=`$DIALOG $TITLE"$TITLETEXT" $MENU $TEXT"\nPlease select a partition to install or restore grub to. If the only option you see is to Quit then no valid partitions were found." Exit "Quit" $partitiontext`

if [ "$TARGETPART" = "Exit" ]; then
  $DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n\nQuiting now."
  exit 1
elif [ "$TARGETPART" = "current" ]; then
TARGETPART=`mount | grep " / " | awk '{print $1}' | awk -F "/" '{print $3}'`
fi


#grub location
GRUBLOCTEST=`$DIALOG $TITLE"$TITLETEXT" $MENU $TEXT"\nPlease select where to install grub to.\n" mbr "Master Boot Record of first drive" root "Root partition of $TARGETPART"`

if [ "$GRUBLOCTEST" = "root" ]; then
GRUBLOCTEXT="root partition of $TARGETPART"
GRUBLOC="root"
else
GRUBLOCTEXT="master boot record"
GRUBLOC=""
fi


tail -f /usr/bin/remastersys-grub-restore |  $DIALOG $TITLE"$TITLETEXT" --progress --width 400 --pulsate $TEXT"Installing grub now.  Please wait..." &


#END GUI MODE




fi





#
#
#
#do the actual grub install/restore
#
#
#

GRUBDIR=`mount | grep $TARGETPART | awk '{print$3}'`
if [ "$GRUBDIR" != "/" ]; then
GRUBDIR="/tmp/remmnt/TARGET"
#unmount and remount the partition on /tmp/remmnt/TARGET rw
if [ "`mount | grep $TARGETPART`" ]; then
umount /dev/$TARGETPART
fi
sleep 2
if [ ! -d $GRUBDIR ]; then
mkdir -p $GRUBDIR
fi
mount /dev/$TARGETPART $GRUBDIR -o rw
sleep 2

mount -o bind /proc $GRUBDIR/proc
mount -o bind /dev $GRUBDIR/dev
mount -o bind /sys $GRUBDIR/sys

else

GRUBDIR="/"

fi

echo "Installing and setting up grub."
# Setup grub
remastersys-grubconfig $GRUBDIR $GRUBLOC

if [ "$GRUBDIR" != "" ]; then 
umount $GRUBDIR/proc
sleep 1
umount $GRUBDIR/dev
sleep 1
umount $GRUBDIR/sys
sleep 1
umount $GRUBDIR
sleep 1

fi

if [ "$GUI" != "" ]; then
killall -KILL tail
killall -KILL zenity
fi


$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n\nGrub has been installed/restored on $TARGETPART." $HEIGHT $WIDTH


exit 0


