#!/bin/bash
#
# Copyright 2005 Kent Robotti <robotti@godmail.com> 3-24-2006
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# grubconfig is based on liloconfig by Patrick Volkerding fron the
# Slackware Linux distribution.
#
# Copyright 1994, 1998, 1999  Patrick Volkerding, Moorhead, Minnesota USA
# Copyright 2002, 2003, 2004  Slackware Linux, Inc, Concord, CA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

vdir=$(dirname $0)
. $vdir/vasm-functions

check_root

##If we cant find grub then leave
 if [ ! -x "`type -path grub`" ]; then
       $DCMD --backtitle "$BACKTITLE" --title "CAN'T FIND GRUB" --msgbox \
     "I can't find 'grub' on the system." 5 40
     exit
 fi

  if [ ! -r /proc/partitions ]; then
    CHROOT=YES
    mkdir -p /proc
    mount -t proc proc /proc
  fi
     if [ -r /proc/sys/kernel/printk ]; then
        echo 3 >/proc/sys/kernel/printk
        KILL=NO
     else
        killall klogd 2>$TMP/null
        sleep 1
        klogd -c 3 2>$TMP/null
        KILL=NO
     fi

####  VARIABLES  ####
TMP=/tmp/grubconfig
BOOT_TMP=/tmp/boot
CONSOLETYPE=standard
CONSOLENUM=normal

rm -rf $TMP $BOOT_TMP
mkdir -p $TMP $BOOT_TMP

PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin

 BOOT=/boot
 if grep -q $BOOT/grub/grub.conf 2>$TMP/null `type -path grub` ; then
    grub_config=grub.conf
 else
    grub_config=menu.lst
 fi

HDR="no" # this means the header section of /boot/grub/menu.lst has not yet
         # been configured
LNX="no" # this means no Linux partition has been defined as bootable
         # through GRUB. Both of these must change to "yes" before GRUB will
         # install from this script.

####  END VARIABLES  ####

####  FUNCTIONS  ####

installcolor()
{
if [ "$GRUB_TARGET" = "/dev/fd0" ]; then
while :
do
       $DCMD --backtitle "$BACKTITLE" --title "FLOPPY DISK" --menu \
"The floppy should be formatted. \n\
Put the floppy in the drive and press enter." 10 55 2 \
 "Ready" "The floppy is already formatted" \
 "Format" "Format the floppy" 2> $TMP/reply
      if [ ! $? = 0 ]; then
        chroot_umount
        exit
      elif ! (dd if=/dev/fd0 of=$TMP/null bs=1 count=1 1>$TMP/null 2>$TMP/null) ; then
        continue
      fi
      REPLY="`cat $TMP/reply`"
      if [ "$REPLY" = "Format" ]; then
      [ -b /dev/fd0h1440 ] && fd=/dev/fd0h1440
      [ -b /dev/fd0H1440 ] && fd=/dev/fd0H1440
      [ -b /dev/fd0u1440 ] && fd=/dev/fd0u1440
      if [ -x "`type -path fdformat`" ]; then
          $DCMD --backtitle "$BACKTITLE" --title "Formatting floppy..." --infobox \
        "fdformat -n $fd" 3 35
        fdformat -n $fd || continue
      elif [ -x "`type -path superformat`" ]; then
          $DCMD --backtitle "$BACKTITLE" --title "Formatting floppy..." --infobox \
        "superformat -f $fd" 3 35
        superformat -f $fd || continue
      else
           $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox \
        "Can't find 'fdformat' or 'superformat' to format floppy." 5 65
         continue
      fi
      fi
        break
done
fi

     $DCMD --backtitle "$BACKTITLE" --infobox "\nInstalling the GRUB Bootloader..." 10 40
   sleep 2 
 
   if [ ! "$KILL" = "NO" ]; then
     if [ -r /proc/sys/kernel/printk ]; then
        echo 3 >/proc/sys/kernel/printk
     else
        killall klogd 2>$TMP/null
        sleep 1
        klogd -c 3 2>$TMP/null
     fi
   fi
   
   install_grub
 
 if [ ! "$SUCCESS" = "0" ]; then # some GRUB error occured
  if [ ! "$backup_mbr" = "" -a ! "$mbr_dev" = "" ]; then
  if [ -s "$backup_mbr" ]; then
  if dd if=$mbr_dev bs=512 count=1 | grep -q GRUB ; then
     dd if=$backup_mbr of=$mbr_dev bs=512 count=1 >$TMP/null 2>&1
  fi
  fi
  fi
cat $TMP/error >$TMP/tmpmsg
cat << EOF >>$TMP/tmpmsg
-----------------------------------------------------------------------
Sorry, but the attempt to install GRUB has returned an error, so GRUB
has not been correctly installed.  You'll have to use a bootdisk to
start your machine instead.  It may be possible to get GRUB working by
editing the /boot/grub/$grub_config file and reinstalling GRUB manually.
See the GRUB man page and info documentation in /usr/info for more help.
-----------------------------------------------------------------------
EOF
    $DCMD --backtitle "$BACKTITLE" --title "GRUB INSTALL ERROR" --msgbox "`cat $TMP/tmpmsg`" 0 0
 else
  if [ "$message" = "yes" ]; then
cat << EOF >$TMP/tmpmsg

GRUB was successfully $installed.

You should check and edit the '/boot/grub/$grub_config'
file on '$ROOT_DEVICE', if needed. You may want to change
the location '/boot/vmlinuz', and/or options passed to any
Linux kernel listed there.

EOF
     $DCMD --backtitle "$BACKTITLE" --title "GRUB INSTALL SUCCESS" --msgbox "`cat $TMP/tmpmsg`" 12 70
  else
     $DCMD --backtitle "$BACKTITLE" --title "GRUB INSTALL SUCCESS" --msgbox \
   "GRUB was successfully $installed." 5 70
  fi
 fi
   umount $BOOT_TMP 2>$TMP/null
   rm -rf $TMP $BOOT_TMP
   chroot_umount
   exit
}

# This function scans for bootable partitions (making some assumptions along
# the way which may or may not be correct, but usually work), and sets up
# GRUB in either the superblock, or the MBR.
menu_boot()
{
 if grep -q "OS/2 Boot Manager" $TMP/DEV 2> $TMP/null ; then
    $DCMD --backtitle "$BACKTITLE" --title "OS/2 BOOT MANAGER FOUND" --yesno \
  "Your system appears to have Boot Manager, a boot menu system provided \
with OS/2 and Partition Magic.  If you like, we can install a very simple \
GRUB boot block at the start of your Linux partition.  Then, you can \
add the partition to the Boot Manager menu, and you'll be able to use \
Boot Manager to boot Linux.  Would you like to install GRUB in a Boot \
Manager compatible way?" 11 65
  if [ $? = 0 ]; then
    TG=Root
    skip=yes
  fi
fi

if [ ! "$skip" = "yes" ]; then
$DCMD --backtitle "$BACKTITLE" --title "GRUB INSTALL" --menu "\n
GRUB (Grand Unified BootLoader) allows you to boot various operating systems on
multiple partitions. To use it, you must install GRUB on either:\n
 1. The Master Boot Record of your first hard drive. Use this unless\n
    you are using another boot manager (e.g LILO, PQMagic, NT Loader)\n
 2. The boot sector of your Linux partition. Then you need to:\n
      a) make the partition bootable (using fdisk for example).\n
      b) use another boot manager to boot this partition.\n
 3. A formatted floppy disk. The safest and the slowest.\n
Which option would you like?" \
20 74 4 \
"MBR" "install to Master Boot Record (simplest)" \
"SECTOR" "install to boot sector (needs extra manual steps)" \
"FLOPPY" "install to a formatted floppy in /dev/fd0 (A:)" \
"SKIP" "do not install GRUB" \
 2> $TMP/reply
 if [ ! $? = 0 ]; then
   chroot_umount
   exit
 fi
 TG="`cat $TMP/reply`"
fi
 
 if [ "$TG" = "MBR" ]; then
  hd_mbr
  MBR=YES
 elif [ "$TG" = "SECTOR" ]; then
  MBR=NO
 elif [ "$TG" = "FLOPPY" ]; then
  GRUB_TARGET="/dev/fd0"
  MBR=NO
 elif [ "$TG" = "SKIP" ]; then
  clean_exit
 fi
}

chroot_umount() {
if [ "$CHROOT" = "YES" ]; then
  umount /proc
fi
}

create_devices() {
      fdisk -l >$TMP/DEV 2>$TMP/null
}

hd_mbr() {
      mbr_dev="`grep "^Disk /dev/" $TMP/DEV | head -n 1 | cut -d ' ' -f 2 | cut -b 1-8`"
      mbr_dev2="`grep "^Disk /dev/" $TMP/DEV | tail -n 1 | cut -d ' ' -f 2 | cut -b 1-8`"

if [ -z "$mbr_dev" ]; then
           $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox \
        "I can't find a device to install GRUB to!" 5 60
         umount $BOOT_TMP 2>$TMP/null && rmdir $BOOT_TMP
         rm -rf $TMP
         chroot_umount
         exit
elif [ $mbr_dev2 = $mbr_dev ]; then
mbr_grub_dev=hd0
if ! probe $mbr_dev; then
           $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox \
        "I can't find the device '$mbr_dev', so I can't install GRUB!" 5 68
         umount $BOOT_TMP 2>$TMP/null && rmdir $BOOT_TMP
         rm -rf $TMP
         chroot_umount
         exit
fi
else
while :
do
cat << EOF >$TMP/tmpmsg
Usually GRUB is installed to the MBR of the primary
hard drive, in your case '$mbr_dev'.

If you have more than one hard drive and want GRUB installed
to the MBR of another hard drive, put the device below, or
leave the box blank for '$mbr_dev'.
EOF
 
      $DCMD --backtitle "$BACKTITLE" --title "HARD DRIVE MBR" --inputbox "`cat $TMP/tmpmsg`" 14 68 2> $TMP/mbr_dev
    if [ ! $? = 0 ]; then
      umount $BOOT_TMP 2>$TMP/null && rmdir $BOOT_TMP
      rm -rf $TMP
      chroot_umount
      echo
      echo "Cancelled!"
      exit
    fi
    
     MBR_DEV="`cat $TMP/mbr_dev`"

     if [ -n "$MBR_DEV" ]; then
         old_mbr_dev=$mbr_dev
         mbr_dev=$MBR_DEV
     fi	 
     
     if probe $mbr_dev; then
         break;
     else
           $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox \
        "I can't find the device '$mbr_dev'." 5 45
         mbr_dev=$old_mbr_dev
         continue;
     fi
done
NUM=0
grep "^Disk /dev/" $TMP/DEV | cut -d ' ' -f 2 | cut -b 1-8 | while read dev; do
echo "hd$NUM" >$TMP/mbr_grub_dev
if [ "$dev" = "$mbr_dev" ]; then
break
fi
NUM=$(( $NUM + 1 ))
done
mbr_grub_dev="`cat $TMP/mbr_grub_dev`"
fi
}

get_root_dev() {
NUM=0
grep "^Disk /dev/" $TMP/DEV | cut -d ' ' -f 2 | cut -b 1-8 | while read dev; do
echo "hd$NUM" >$TMP/hd_grub
dev2="`echo $1 | cut -b 1-8`"
if [ "$dev" = "$dev2" ]; then
break
fi
NUM=$(( $NUM + 1 ))
done
hd="`cat $TMP/hd_grub`"
}

other() {
 D=`echo "$@" | cut -d ' ' -f 1`
 get_root_dev $D
 
if echo "$@" | grep -q HURD ; then
grub --no-floppy --batch << EOF 1>$TMP/find_file 2>$TMP/null
find ($hd,$p2)/boot/gnumach.gz
quit
EOF
if grep -q "($hd,$p2)" $TMP/find_file ; then
OTHER="root ($hd,$p2)
  kernel /boot/gnumach.gz root=device:$hd"s"$p1
  module /hurd/ext2fs.static --multiboot-command-line=\${kernel-command-line} --host-priv-port=\${host-port} --device-master-port=\${device-port} --exec-server-task=\${exec-task} -T typed \${root} \$(task-create) \$(task-resume)
  module /lib/ld.so.1 /hurd/exec \$(exec-task=task-create)"
echo "GNU/Hurd on ($DOSP)" > $TMP/label
else
OTHER="rootnoverify ($hd,$p2)
  makeactive
  chainloader --force +1"
echo "SCO Unixware on ($DOSP)" > $TMP/label
fi
elif echo "$@" | grep -q BSD ; then
if echo "$@" | grep -q FreeBSD ; then
OTHER="root ($hd,$p2,a)
  kernel /boot/loader"
else  
OTHER="rootnoverify ($hd,$p2)
  chainloader +1"
fi
elif echo "$@" | grep -q QNX ; then
OTHER="map (hd0) ($hd)
  map ($hd) (hd0)
  rootnoverify ($hd,$p2)
  chainloader +4"
elif echo "$@" | grep -q -E "Plan 9|BeOS|Minix" ; then
OTHER="rootnoverify ($hd,$p2)
  chainloader +1"
elif [ ! "$hd" = "hd0" ]; then
if [ "$p2" = "0" ]; then
OTHER="map (hd0) ($hd)
  map ($hd) (hd0)
  rootnoverify ($hd,$p2)
  makeactive
  chainloader +1"
else
OTHER="map (hd0) ($hd)
  map ($hd) (hd0)
  map (hd0,0) ($hd,$p2)
  map ($hd,$p2) (hd0,0)
  rootnoverify ($hd,$p2)
  makeactive
  chainloader +1"
fi
else
if [ "$p2" = "0" ]; then
OTHER="rootnoverify (hd0,$p2)
  makeactive
  chainloader +1"
else
OTHER="map (hd0,0) (hd0,$p2)
  map (hd0,$p2) (hd0,0)
  rootnoverify (hd0,$p2)
  makeactive
  chainloader +1"
fi
fi
}

# Menu to check if we want to use VESA framebuffer support:
menu_fb() {

  # if FB is not supported, skip
  if ! cat /proc/devices | grep "29 fb" 1> /dev/null ; then
    return $1
  fi

  $WCMD --backtitle "$BACKTITLE" --title "GRUB FRAME BUFFER CONSOLE" \
  --default-item $CONSOLETYPE --menu "\n
It seems that your kernel supports the Linux frame buffer console.
If we enable this, it will allow more rows and columns of text on
the screen and give you a cool penguin logo at boot time.\n
However, the frame buffer console needs more memory and is slower 
than the standard mode. Moreover, not every video card or monitor
support all of these video modes. Many blank-screen problems have
been reported on Linux kernel 2.6 (earlier release).\n
If you are going to use GUI mode more often, better stick with
the standard mode:" 20 72 4 \
  "standard" "Use the standard Linux console (the safe choice)" \
  "bootsplash-high" "Show graphical image during boot, 1024x768" \
  "bootsplash-med" "Show graphical image during boot, 800x600" \
  "bootsplash-low" "Show graphical image during boot, 640x480" \
  "640x480x256" "Frame buffer console, 640x480x256" \
  "800x600x256" "Frame buffer console, 800x600x256" \
  "1024x768x256" "Frame buffer console, 1024x768x256" \
  "640x480x32k" "Frame buffer console, 640x480x32k" \
  "800x600x32k" "Frame buffer console, 800x600x32k" \
  "1024x768x32k" "Frame buffer console, 1024x768x32k" \
  "640x480x64k" "Frame buffer console, 640x480x64k" \
  "800x600x64k" "Frame buffer console, 800x600x64k" \
  "1024x768x64k" "Frame buffer console, 1024x768x64k" \
  2> $freply
  
  result=$?
  [ $result != 0 ] && return $result
  
  CONSOLETYPE="`cat $freply`"
  if [ "$CONSOLETYPE" = "1600x1200x16m" ]; then
    CONSOLENUM=799
  elif [ "$CONSOLETYPE" = "1600x1200x64k" ]; then
    CONSOLENUM=798
  elif [ "$CONSOLETYPE" = "1600x1200x32k" ]; then
    CONSOLENUM=797
  elif [ "$CONSOLETYPE" = "1600x1200x256" ]; then
    CONSOLENUM=796
  elif [ "$CONSOLETYPE" = "1280x1024x16m" ]; then
    CONSOLENUM=795
  elif [ "$CONSOLETYPE" = "1280x1024x64k" ]; then
    CONSOLENUM=794
  elif [ "$CONSOLETYPE" = "1280x1024x32k" ]; then
    CONSOLENUM=793
  elif [ "$CONSOLETYPE" = "1280x1024x256" ]; then
    CONSOLENUM=775
  elif [ "$CONSOLETYPE" = "1024x768x16m" ]; then
    CONSOLENUM=792
  elif [ "$CONSOLETYPE" = "1024x768x64k" ]; then
    CONSOLENUM=791
  elif [ "$CONSOLETYPE" = "1024x768x32k" ]; then
    CONSOLENUM=790
  elif [ "$CONSOLETYPE" = "1024x768x256" ]; then
    CONSOLENUM=773
  elif [ "$CONSOLETYPE" = "800x600x16m" ]; then
    CONSOLENUM=789
  elif [ "$CONSOLETYPE" = "800x600x64k" ]; then
    CONSOLENUM=788
  elif [ "$CONSOLETYPE" = "800x600x32k" ]; then
    CONSOLENUM=787
  elif [ "$CONSOLETYPE" = "800x600x256" ]; then
    CONSOLENUM=771
  elif [ "$CONSOLETYPE" = "640x480x16m" ]; then
    CONSOLENUM=786
  elif [ "$CONSOLETYPE" = "640x480x64k" ]; then
    CONSOLENUM=785
  elif [ "$CONSOLETYPE" = "640x480x32k" ]; then
    CONSOLENUM=784
  elif [ "$CONSOLETYPE" = "640x480x256" ]; then
    CONSOLENUM=769
  elif [ "$CONSOLETYPE" = "bootsplash-high" ]; then
    CONSOLENUM=791
    SPLASH="splash=silent"
    RAMDISK="/boot/initrd"
  elif [ "$CONSOLETYPE" = "bootsplash-med" ]; then
    CONSOLENUM=788
    SPLASH="splash=silent"
    RAMDISK="/boot/initrd"        
  elif [ "$CONSOLETYPE" = "bootsplash-low" ]; then
    CONSOLENUM=785
    SPLASH="splash=silent"
    RAMDISK="/boot/initrd"        
  fi
  return 0
}

menu_os()
{ 
infobox "Detecting operating systems ..."

 # Search for Linux partitions:
 grep "83  Linux" $TMP/DEV | while read PART ; do 
 if [ ! "$PART" = "" ]; then
  LNXP="`echo $PART | cut -f 1 -d ' '`"
  p1=`echo $LNXP | cut -b9-`
  p2=$(( $p1 - 1 ))

  get_root_dev $LNXP

  KERNEL="kernel /boot/vmlinuz root=$LNXP ro vga=$CONSOLENUM $SPLASH"

  title="Linux on ($LNXP)"

  num="`echo $LNXP | cut -b8-`" 
if [ "$RAMDISK" ]; then
  cat << EOF > $TMP/part.$num.sort
# Linux bootable partition config begins
  title $title
  root ($hd,$p2)
  $KERNEL
  initrd $RAMDISK
# Linux bootable partition config ends
EOF
else
  cat << EOF > $TMP/part.$num.sort
# Linux bootable partition config begins
  title $title
  root ($hd,$p2)
  $KERNEL
# Linux bootable partition config ends
EOF
fi
 fi
done

 # OK, now let's look for DOS/WIN/OS2/BSD/HURD partitions:
grep "^/dev/" $TMP/DEV | grep -E "DOS|Win95|W95|FAT|BSD|NTFS|HURD|QNX|Minix|Plan|BeOS" | \
grep -v "Ext'd" | grep -v "Extend" | grep -v -i "swap" | while read PART ; do
  if [ ! "$PART" = "" ]; then
  DOSP="`echo $PART | cut -f 1 -d ' '`"
  p1=`echo $DOSP | cut -b9-`
  p2=$(( $p1 - 1 ))
  if echo $PART | grep -q -E "Win95|W95|NTFS" ; then
  LABEL="Windows on ($DOSP)"
  elif echo $PART | grep -q -E "DOS|FAT" ; then
  LABEL="DOS on ($DOSP)"
  elif echo $PART | grep -q "FreeBSD" ; then
  LABEL="FreeBSD on ($DOSP)"
  elif echo $PART | grep -q "OpenBSD" ; then
  LABEL="OpenBSD on ($DOSP)"
  elif echo $PART | grep -q "NetBSD" ; then
  LABEL="NetBSD on ($DOSP)"
  elif echo $PART | grep -q "BSD" ; then
  LABEL="BSD on ($DOSP)"
  elif echo $PART | grep -q "Minix" ; then
  LABEL="Minix on ($DOSP)"
  elif echo $PART | grep -q "QNX" ; then
  LABEL="QNX on ($DOSP)"
  elif echo $PART | grep -q "BeOS" ; then
  LABEL="BeOS on ($DOSP)"
  elif echo $PART | grep -q "Plan 9" ; then
  LABEL="Plan 9 on ($DOSP)"
  fi

  other $PART

  if echo $PART | grep -q "HURD" ; then
  LABEL="`cat $TMP/label`"
  fi

  num="`echo $DOSP | cut -b8-`" 

  cat << EOF > $TMP/part.$num.sort
# Other bootable partition config begins
  title $LABEL
  $OTHER
# Other bootable partition config ends
EOF
fi
done
}

find_kernel() {
cat << EOF > $TMP/tmpmsg
`echo "   Device Boot      Start         End      Blocks   Id  System"`
`grep "^$PART" $TMP/fdisk.list`

Where on the above Linux partition is the kernel you
want GRUB to boot?

The default is '/boot/vmlinuz'. Just press enter for it.
EOF

      $DCMD --backtitle "$BACKTITLE" --title "KERNEL LOCATION" --cr-wrap --inputbox "`cat $TMP/tmpmsg`" 0 0 2> $TMP/kernel
    if [ $? = 1 -o $? = 255 ]; then
      umount $BOOT_TMP 2>$TMP/null && rmdir $BOOT_TMP
      rm -rf $TMP
      chroot_umount
      echo
      echo "Cancelled!"
      exit
    else
      kernel="`cat $TMP/kernel`"
    if [ "$kernel" = "" ]; then
      KERNEL="kernel /boot/vmlinuz root=$PART ro vga=$CONSOLENUM"
    else
      KERNEL="kernel $kernel root=$PART ro vga=$CONSOLENUM"
    fi
   fi

cat << EOF > $TMP/tmpmsg
`echo "   Device Boot      Start         End      Blocks   Id  System"`
`grep "^$PART" $TMP/fdisk.list`

`echo $KERNEL | cut -d ' ' -f 2-`

Do you want to pass any other options to the above kernel?
For example: hdd=scsi

Put then on one line one after the other or leave box blank.
EOF

      $DCMD --backtitle "$BACKTITLE" --title "KERNEL OPTIONS" --cr-wrap --inputbox "`cat $TMP/tmpmsg`" 0 0 2> $TMP/reply
    APPEND="`cat $TMP/reply`"
}

install_grub()
{
NUM=0
grep "^Disk /dev/" $TMP/DEV | cut -d ' ' -f 2 | cut -b 1-8 | while read dev; do
echo "hd$NUM" >$TMP/root_dev
dev2="`echo $ROOT_DEVICE | cut -b 1-8`"
if [ "$dev" = "$dev2" ]; then
break
fi
NUM=$(( $NUM + 1 ))
done
root_dev="`cat $TMP/root_dev`"

     p1=`echo $ROOT_DEVICE | cut -b 9-`
     p2=$(( $p1 - 1 ))

rootpart=`mount|grep " / "|cut -d " " -f1|cut -c 8-9`
cat $TMP/part.$rootpart.sort > $TMP/grub.conf
ROOTDEV=`cat $TMP/part.$rootpart.sort|grep "  root"|cut -c 8-` 
rm $TMP/part.$rootpart.sort
for f in `ls $TMP/part.*.sort`
do
cat $f >> $TMP/grub.conf
done

if [ -f $TMP/initrd.conf ]; then
  cat $TMP/initrd.conf >> $TMP/grub.conf
fi

cat << EOF >> $TMP/grub.conf
title --- For help press 'c', type: 'help'
root (hd0)
title --- For usage examples, type: 'cat /boot/grub/grub.txt'
root (hd0)
EOF

cat << EOF > $BOOT/grub/grub.txt
=======================================================================
This is an example of how to use the GRUB edit function.

Highlight the menu entry you want to edit and press 'e', then
highlight the line you want to edit and press 'e'. Add what
you want to the line 'acpi=off' etc. and press enter, then
'b' to boot.

Examples of the difference between Linux and GRUB device names.

Linux IDE:	GRUB IDE:	Linux SCSI:	GRUB SCSI:
/dev/hda1       (hd0,0)		/dev/sda1       (hd0,0)
/dev/hda2       (hd0,1)		/dev/sda2       (hd0,1)
/dev/hda3       (hd0,2)		/dev/sda3       (hd0,2)
/dev/hda4       (hd0,3)		/dev/sda4       (hd0,3)
/dev/hdb1       (hd1,0)		/dev/sdb1       (hd1,0)
/dev/hdb2       (hd1,1)		/dev/sdb2       (hd1,1)
/dev/hdb3       (hd1,2)		/dev/sdb3       (hd1,2)
/dev/hdb4       (hd1,3)		/dev/sdb4       (hd1,3)

These are some examples of how to use GRUB from the command prompt.

Press the 'c' key for the command prompt.

If you want to boot a Linux system on a partition, using its kernel
/boot/vmlinuz etc., do this.

grub> root (hd0,1)
grub> kernel /boot/vmlinuz root=/dev/hda2 ro
grub> boot

You could do this to find what partition the kernel is on.

For example, show me what partitions have /boot/vmlinuz.

grub> find /boot/vmlinuz
(hd0,1)
(hd0,2)

If you want to boot a Windows partition, do this.

For example, boot partition on /dev/hda1.

grub> rootnoverify (hd0,0)
grub> makeactive
grub> chainloader +1
grub> boot

If you want to boot a FreeBSD partition, using /boot/loader.

For example, boot FreeBSD partition on /dev/hda4.

grub> root (hd0,3,a)
grub> kernel /boot/loader
grub> boot

If that doesn't work, try this.

grub> rootnoverify (hd0,3,a)
grub> chainloader +1
grub> boot
==============================================================

Press the [Esc] key to return to the GRUB menu.
EOF

sync

if [ "$MBR" = "NO" ]; then
if [ "$GRUB_TARGET" = "/dev/fd0" ]; then
rm -f $BOOT/grub/device.map
grub --device-map=$BOOT/grub/device.map --batch << EOF 1>$TMP/error 2>$TMP/null
root ($root_dev,$p2)
setup (fd0)
quit
EOF
SUCCESS=0
grep -q Error $TMP/error && SUCCESS=1
installed="installed on the floppy disk"
else
rm -f $BOOT/grub/device.map
grub --device-map=$BOOT/grub/device.map --no-floppy --batch << EOF 1>$TMP/error 2>$TMP/null
root ($root_dev,$p2)
setup ($root_dev,$p2)
quit
EOF
SUCCESS=0
grep -q Error $TMP/error && SUCCESS=1
installed="installed on the Linux partition $ROOT_DEVICE"
fi
else
dd if=$mbr_dev of=$BOOT/grub/mbr.`echo $mbr_dev | cut -b 6-8`.$$ bs=512 count=1 >$TMP/null 2>&1 && \
backup_mbr=$BOOT/grub/mbr.`echo $mbr_dev | cut -b 6-8`.$$
rm -f $BOOT/grub/device.map
grub --device-map=$BOOT/grub/device.map --no-floppy --batch << EOF 1>$TMP/error 2>$TMP/null
root ($root_dev,$p2)
setup ($mbr_grub_dev)
quit
EOF
SUCCESS=0
grep -q Error $TMP/error && SUCCESS=1
installed="installed on the MBR of $mbr_dev"
fi  

     if [ -r $BOOT/grub/$grub_config ]; then
          mv $BOOT/grub/$grub_config $BOOT/grub/$grub_config.old.$$
     fi

if [ ! "$backup_mbr" = "" ]; then
mbr_backup=`basename $backup_mbr` 
 cat << EOF > $BOOT/grub/$grub_config
# GRUB configuration file '/boot/grub/$grub_config'.
# generated by 'vgrubconf'.  `date +%c 2>$TMP/null`
#
# The backup copy of the MBR for drive '$mbr_dev' is
# here '/boot/grub/$mbr_backup'.  You can restore it like this.
# dd if=$mbr_backup of=$mbr_dev bs=512 count=1
#
# Start GRUB global section
timeout 20
default 0
splashimage=$ROOTDEV/boot/bitmap/boot.xpm
#color light-gray/blue black/light-gray
# End GRUB global section
EOF
else
 cat << EOF > $BOOT/grub/$grub_config
# GRUB configuration file '/boot/grub/$grub_config'.
# generated by 'vgrubconf'.  `date +%c 2>$TMP/null`
#
# Start GRUB global section
timeout 20
default 0
splashimage=$ROOTDEV/boot/bitmap/boot.xpm
#color light-gray/blue black/light-gray
# End GRUB global section
EOF
fi
cat $TMP/grub.conf >> $BOOT/grub/$grub_config
chmod 644 $BOOT/grub/$grub_config
}

# 'probe()' borrowed from GRUB QuickInst.
probe()
{
 [ ! -z "`dd if=$1 bs=1 count=1 2>$TMP/null | tr '\0' x`" ]
 return
}

root_device() {
# Determine the root partition (such as /dev/hda2)
    ROOT_DEVICE="`mount 2>$TMP/null | grep " on / " | awk '{print $1}'`"

  if [ -z "$ROOT_DEVICE" -a -s /etc/fstab ]; then
    ROOT_DEVICE="`grep -v "^#" /etc/fstab 2>$TMP/null | grep " / " | awk '{print $1}'`"
  fi
  
  if [ -n "$ROOT_DEVICE" ]; then
  if ! echo $ROOT_DEVICE | grep -q -E "/dev/sd|/dev/hd" ; then
     unset ROOT_DEVICE
  fi
  fi
    
 fs=`mount | grep "^$ROOT_DEVICE" | awk '{print $5}'`
 [ "$fs" = "ext2" -o "$fs" = "ext3" ] && fs=e2fs
 [ "$fs" = "msdos" ] && fs=fat
 [ "$fs" = "vfat" ] && fs=fat
 
 for f in stage1 stage2 $fs_stage1_5
 do
 if [ ! -f $BOOT/grub/$f ]; then
 echo "ERROR: Can't find '$f' from the grub package."
 echo "If you have the grub package installed, you should have a"
 echo "/boot/grub directory, containing the stage files."
 chroot_umount
 exit 1
 fi 
 done
}

    create_devices
menu_boot()
{
     $DCMD --backtitle "$BACKTITLE" --title "SELECT GRUB TARGET LOCATION" --menu "GRUB can be installed \
to a variety of places: the master boot record of a hard drive, \
the superblock of a Linux partition (which could be made \
active with fdisk, or booted with a boot manager), or a formatted floppy disk, \
If you're using a boot system such as OS/2's Boot Manager, you should use \
the "Root" selection. Please pick a target location:" 15 68 3 \
"Root" "Use superblock of Linux partition" \
"Floppy" "Use a formatted floppy disk in the boot drive" \
"MBR" "Use the Master Boot Record (possibly unsafe)" \
2> $TMP/reply
   if [ $? = 1 -o $? = 255 ]; then
    HDR="no"
    continue;
   else
    LNX="no"
    TG="`cat $TMP/reply`"
   fi
   if [ "$TG" = "MBR" ]; then
    hd_mbr
    MBR=YES
   elif [ "$TG" = "Root" ]; then
    MBR=NO
   elif [ "$TG" = "Floppy" ]; then
    GRUB_TARGET="/dev/fd0"    
    MBR=NO
   else
    HDR="no"
    continue;
   fi
    HDR="yes"
    root_device
}

####  END FUNCTIONS  ####


#################################
# MAIN
wizard create_devices root_device menu_boot menu_fb menu_os installcolor



