#! /bin/bash
# rcrsn51 2013-03-14

export TEXTDOMAIN=legacy_grub_2013
export OUTPUT_CHARSET=UTF-8

INSTALL () {

# $1=stage1  $2=partition  $3=stage2  $4=windows  $5=Ubuntu  $6:true=frugal,false=full 

TIMEOUT=2
[ -n "$4" -o -n "$5" ] && TIMEOUT=10

mount /dev/$2 /mnt/data
if [ $? -gt 0 ]; then
  Xdialog --title " " --msgbox "$(gettext 'Cannot mount') $2" 0 0
  exit
fi

GRUBPATH="/mnt/data/boot/grub"
mkdir -p $GRUBPATH
if [ $? -gt 0 ]; then
  Xdialog --title " " --msgbox "$(gettext 'Cannot create') $GRUBPATH" 0 0
  exit
fi

cp $(find /usr/lib/grub -name stage1) $GRUBPATH
cp $(find /usr/lib/grub -name stage2) $GRUBPATH

grub --batch --no-floppy << EOF
root $3
setup $1
quit
EOF

[ -f $GRUBPATH/menu.lst ] && mv $GRUBPATH/menu.lst $GRUBPATH/menu.lst.000

cat > $GRUBPATH/menu.lst << EOF
timeout $TIMEOUT
default 0
#
EOF

if [ "$6" = "true" ]; then

cat >> $GRUBPATH/menu.lst << EOF
title Puppy Linux Frugal on $2
root $3
kernel /vmlinuz pmedia=atahd pdev1=$2 pfix=fsck
initrd /initrd.gz
EOF

else

cat >> $GRUBPATH/menu.lst << EOF
title Puppy Linux Full on $2
root $3
kernel /boot/vmlinuz root=/dev/$2 ro vga=normal
EOF

fi

if [ -n "$4" ]; then
cat >> $GRUBPATH/menu.lst << EOF
#
title Windows on $4
root $4
chainloader +1
EOF
fi

if [ -n "$5" ]; then
cat >> $GRUBPATH/menu.lst << EOF
#
title Ubuntu on $5
root $5
kernel /boot/grub/core.img
EOF
fi

sync
umount /mnt/data

}

############### end of functions #############

export PROG="
<window title=\"Legacy GRUB Config 2013\">
<vbox>
  <frame>
    <text xalign=\"0\">
      <label>$(gettext 'Legacy GRUB has two parts. Stage1 is the bootloader code. It can be installed in the MBR of a hard drive or in a partition boot sector. Stage2 is the set of support files, like menu.lst. It is installed in the folder /boot/grub of a partition. Legacy GRUB works with ext or FAT filesystems, but not NTFS.')</label>
    </text>
  </frame>
  <frame $(gettext 'Quick Mode')>
    <text xalign=\"0\">
      <label>$(gettext 'Install Stage1 in the MBR of Drive sda and Stage2 in sda1')</label>
    </text>
    <hbox>
      <text xalign=\"0\"><label>$(gettext 'Puppy Setup')</label></text>
      <radiobutton>
        <variable>QFRUG</variable>
        <label>$(gettext 'Frugal')</label>
       </radiobutton>
       <radiobutton> 
         <variable>QFULL</variable>
         <label>$(gettext 'Full')</label>
      </radiobutton>
      <text><label>\"  \"</label></text>
      <button><label>$(gettext 'Help')</label></button>
      <button><label>$(gettext 'Quick Install')</label></button>
      <button><label>$(gettext 'Cancel')</label></button>
    </hbox>
  </frame>
  <frame $(gettext 'Expert Mode')>
    <hbox>
      <text><label>$(gettext 'Target for Stage1')</label></text>
      <entry>
        <variable>STAGE1</variable>
        <default>(hd0)</default>
      </entry>
    </hbox>
    <hbox>
      <text><label>$(gettext 'Partition for Stage2')</label></text>
      <entry>
        <variable>PART</variable>
        <default>sda1</default>
      </entry>
    </hbox>
    <hbox>
      <text><label>$(gettext 'Target for Stage2')</label></text>
      <entry>
        <variable>STAGE2</variable>
        <default>(hd0,0)</default>
      </entry>
    </hbox>
    <hbox>
      <text><label>$(gettext 'Optional Windows entry')</label></text>
      <entry>
        <variable>WIN</variable>
        <default>(hd0,0)</default>
      </entry>
    </hbox>
    <hbox>
      <text><label>$(gettext 'Optional Ubuntu entry')</label></text>
      <entry>
        <variable>UBUNTU</variable>
        <default>(hd0,0)</default>
      </entry>
    </hbox>
    <hbox>
      <text xalign=\"0\"><label>$(gettext 'Puppy Setup')</label></text>
      <radiobutton>
        <variable>EFRUG</variable>
        <label>$(gettext 'Frugal')</label>
       </radiobutton>
       <radiobutton> 
         <variable>EFULL</variable>
         <label>$(gettext 'Full')</label>
      </radiobutton>
      <text><label>\"           \"</label></text>
      <button><label>$(gettext 'Expert Install')</label></button>
      <button><label>$(gettext 'Cancel')</label></button>
    </hbox>
  </frame>
</vbox>
</window>
"

I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog3 -c --program PROG); do
  eval $STATEMENTS
done
IFS=$I

[ "$EXIT" = "$(gettext 'Cancel')" -o "$EXIT" = "abort" ] && exit

[ "$EXIT" = "$(gettext 'Help')" ] && exec defaultbrowser http://murga-linux.com/puppy/viewtopic.php?p=692147#692147

Xdialog --title " " --yesno "$(gettext 'Do you really want to install GRUB?')" 0 0
[ $? -gt 0 ] && exit

if [ "$EXIT" = "$(gettext 'Quick Install')" ]; then			#quick install
  INSTALL "(hd0)" "sda1" "(hd0,0)" "" "" "$QFRUG" 			#true=frugal, false=full
else									#expert install
  INSTALL "$STAGE1" "$PART" "$STAGE2" "$WIN" "$UBUNTU" "$EFRUG" 	#true=frugal, false=full
fi

Xdialog --title " " --msgbox "$(gettext 'Done!')" 0 0
