#!/bin/bash
#
# Copyright 2002 Kent Robotti <robotti@godmail.com> 2-11-2002
# 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.

TMP=/tmp
if [ ! -d $TMP ]; then
mkdir $TMP
else
rm -f $TMP/grub.conf $TMP/fdisk.list
fi

PROBE() {
# listide major minor hd1 hd2 (2 base devs for major)
list_ide() {
    unset fdisk
  if [ "$2" = "0" ]; then
    grep -q "cdrom" /proc/ide/$3/media && fdisk=no
    grep -q "tape" /proc/ide/$3/media && fdisk=no
    if [ ! "$fdisk" = "no" ]; then 
      fdisk -l /dev/$3
    fi
  elif [ "$2" = "64" ]; then
    grep -q "cdrom" /proc/ide/$4/media && fdisk=no
    grep -q "tape" /proc/ide/$4/media && fdisk=no
    if [ ! "$fdisk" = "no" ]; then 
      fdisk -l /dev/$4
    fi
  fi
}

list_scsi() {
  # find drive # 0 - 15
  DRV=$(( $1 / 16 ))
  NUM=$(( $1 % 16 ))
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  if [ "$DRV" = "0" ]; then
    fdisk -l /dev/sda 
  elif [ "$DRV" = "1" ]; then
    fdisk -l /dev/sdb 
  elif [ "$DRV" = "2" ]; then
    fdisk -l /dev/sdc 
  elif [ "$DRV" = "3" ]; then
    fdisk -l /dev/sdd 
  elif [ "$DRV" = "4" ]; then
    fdisk -l /dev/sde 
  elif [ "$DRV" = "5" ]; then
    fdisk -l /dev/sdf 
  elif [ "$DRV" = "6" ]; then
    fdisk -l /dev/sdg 
  elif [ "$DRV" = "7" ]; then
    fdisk -l /dev/sdh 
  elif [ "$DRV" = "8" ]; then
    fdisk -l /dev/sdi 
  elif [ "$DRV" = "9" ]; then
    fdisk -l /dev/sdj 
  elif [ "$DRV" = "10" ]; then
    fdisk -l /dev/sdk 
  elif [ "$DRV" = "11" ]; then
    fdisk -l /dev/sdl 
  elif [ "$DRV" = "12" ]; then
    fdisk -l /dev/sdm 
  elif [ "$DRV" = "13" ]; then
    fdisk -l /dev/sdn 
  elif [ "$DRV" = "14" ]; then
    fdisk -l /dev/sdo 
  elif [ "$DRV" = "15" ]; then
    fdisk -l /dev/sdp 
  fi
}
  cat /proc/partitions | grep "^ " | while read line ; do
    SMASHED_LINE=$line
    MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
    MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
    if [ "$MAJOR" = "3" ]; then
      list_ide $MAJOR $MINOR hda hdb
    elif [ "$MAJOR" = "8" ]; then
      list_scsi $MINOR
    elif [ "$MAJOR" = "22" ]; then
      list_ide $MAJOR $MINOR hdc hdd
    elif [ "$MAJOR" = "33" ]; then
      list_ide $MAJOR $MINOR hde hdf
    elif [ "$MAJOR" = "34" ]; then
      list_ide $MAJOR $MINOR hdg hdh
    elif [ "$MAJOR" = "56" ]; then
      list_ide $MAJOR $MINOR hdi hdj
    fi
  done
}

initrd() {
if [ "$ROOT_DEVICE" = "$1" ]; then
if ls /boot/initrd* 1>/dev/null 2>/dev/null ; then
initrd=`ls -1 -t /boot/initrd* | head -n 1`
  $DIALOG --title "INITRD FOUND" --yesno "Do you want to include \
the initrd \`$initrd' in the grub config file?" 7 66
if [ $? = 0 ]; then
size=`gzip -l $initrd 2>/dev/null | grep -v uncompressed_name | awk '{print $2}'`
if [ ! "$size" = "" ]; then
size=$(( $size / 1024 ))
else
size=$(( `ls -l $initrd | awk '{print $5}'` / 1024 ))
fi
KERNEL="`echo $KERNEL | awk '{print $1,$2,$3}'` ramdisk_size=$size root=/dev/ram0 rw"
  cat << EOF >> $2
# Linux bootable initrd config begins
  title Boot Linux initrd $initrd (on $1)
  root (hd0,$p2)
  $KERNEL
  initrd $initrd
# Linux bootable initrd config ends
EOF
fi
fi
fi
}

other() {
if [ "`echo $1 | cut -b6-8`" = "hdb" -o "`echo $1 | cut -b6-8`" = "sdb" ]; then
hd=hd1
else
hd=hd0
fi

if echo $PART | grep -q HURD ; then
OTHER="root ($hd,$p2)
  kernel /boot/gnumach.gz root=hd1s$p1
  module /boot/serverboot.gz"
elif echo $PART | grep -q BSD ; then
if echo $PART | grep -q FreeBSD ; then
OTHER="root ($hd,$p2,a)
  kernel /boot/loader"
else  
OTHER="root ($hd,$p2)
  chainloader +1"
fi
elif [ "$hd" = "hd1" ]; then
if [ "$p2" = "0" ]; then
OTHER="map (hd0) (hd1)
  map (hd1) (hd0)
  rootnoverify (hd1,$p2)
  chainloader +1
  makeactive"
else
OTHER="map (hd0) (hd1)
  map (hd1) (hd0)
  map (hd0,0) (hd1,$p2)
  map (hd1,$p2) (hd0,0)
  rootnoverify (hd1,$p2)
  chainloader +1
  makeactive"
fi
else
if [ "$p2" = "0" ]; then
OTHER="rootnoverify (hd0,$p2)
  chainloader +1
  makeactive"
else
OTHER="map (hd0,0) (hd0,$p2)
  map (hd0,$p2) (hd0,0)
  rootnoverify (hd0,$p2)
  chainloader +1
  makeactive"
fi
fi
}

# 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.
simplegrub()
{
 $DIALOG --title "SELECT GRUB DESTINATION" --menu \
"GRUB can be installed to a variety of places:\n\
\n\
  1. The superblock of your root Linux partition.  (which could\n\
     be made the bootable partition with DOS or Linux fdisk, or\n\
     booted with a program like OS/2 Boot Manager)\n\
  2. A formatted floppy disk.\n\
  3. The Master Boot Record of your first hard drive.\n\
  4. Just create the /boot/grub/menu.lst or grub.conf\n\
     config file, but don't install grub.\n\
\n\
Options 1 and 2 are the safest, but option 1 does require a little\n\
extra work later (setting the partition bootable with fdisk).\n\
Which option would you like?" \
 23 72 4 \
 "Root" "Install to superblock (which must be made bootable)" \
 "Floppy" "Install to a formatted floppy in /dev/fd0 (A:)" \
 "MBR" "Install to Master Boot Record (possibly unsafe)" \
 "Config" "Just Create grub config file, but don't install grub" \
 2> $TMP/reply
 if [ $? = 1 -o $? = 255 ]; then
  exit
 fi
 TG="`cat $TMP/reply`"
 rm -r $TMP/reply
 if [ "$TG" = "MBR" ]; then
  MBR=YES
 elif [ "$TG" = "Root" ]; then
  MBR=NO
 elif [ "$TG" = "Floppy" ]; then
  GRUB_TARGET="/dev/fd0"
  MBR=NO
 elif [ "$TG" = "Config" ]; then
  INSTALL=NO
 fi

    $DIALOG --title "Working!" --infobox \
    "Creating /boot/grub/$grub_config file..." 3 45
     sleep 1

      if [ -r /boot/grub/$grub_config ]; then
       mv /boot/grub/$grub_config /boot/grub/$grub_config.old.$$
      fi
    
 cat << EOF > /boot/grub/$grub_config
# GRUB configuration file \`/boot/grub/$grub_config'.
# generated by \`grubconfig'.  `date +%c 2>/dev/null`
#
# Start GRUB global section
#timeout 30
# End GRUB global section
EOF

 # Search for Linux partitions:
PROBE | grep "83  Linux" | while read PART ; do 
 if [ ! "$PART" = "" ]; then
  LNXP="`echo $PART | cut -f 1 -d ' '`"
  p1=`echo $LNXP | cut -b9-`
  p2=$(( $p1 - 1 ))
  if [ ! "$found_kernel" = "yes" ]; then
  find_kernel
  found_kernel=yes
  fi
  if grep -q "(hd0,$p2)" $TMP/boot.vmlinuz 2>/dev/null ; then
  KERNEL="kernel /boot/vmlinuz vga=normal root=$LNXP ro"
  elif grep -q "(hd0,$p2)" $TMP/vmlinuz 2>/dev/null ; then
  KERNEL="kernel /vmlinuz vga=normal root=$LNXP ro"
  else
  cant_find_kernel $PART $LNXP
  fi
  cat << EOF >> /boot/grub/$grub_config
# Linux bootable partition config begins
  title Boot Linux (on $LNXP)
  root (hd0,$p2)
  $KERNEL
# Linux bootable partition config ends
EOF
 fi
initrd $LNXP /boot/grub/$grub_config
done

 # OK, now let's look for DOS/WIN/OS2/BSD/HURD partitions:
PROBE | grep -E "DOS|Win|FAT12|FAT16|BSD|HPFS|HURD" | grep -v "Ext'd" | \
grep -v "Extend" | while read PART ; do
  if [ ! "$PART" = "" ]; then
  DOSP="`echo $PART | cut -f 1 -d ' '`"
  if echo $PART | grep -q "Win" ; then
  LABEL="Boot Windows (on $DOSP)"
  elif echo $PART | grep -q -E "DOS|FAT" ; then
  LABEL="Boot DOS (on $DOSP)"
  elif echo $PART | grep -q "HPFS" ; then
  LABEL="Boot OS/2 (on $DOSP)"
  elif echo $PART | grep -q "BSD" ; then
  if echo $PART | grep -q "FreeBSD" ; then
  LABEL="Boot FreeBSD (on $DOSP)"
  elif echo $PART | grep -q "OpenBSD" ; then
  LABEL="Boot OpenBSD (on $DOSP)"
  elif echo $PART | grep -q "NetBSD" ; then
  LABEL="Boot NetBSD (on $DOSP)"
  else
  LABEL="Boot BSD (on $DOSP)"
  fi
  elif echo $PART | grep -q "HURD" ; then
  LABEL="Boot GNU/Hurd (on $DOSP)"
  fi

  p1=`echo $DOSP | cut -b9-`
  p2=$(( $p1 - 1 ))

  other $DOSP

  cat << EOF >> /boot/grub/$grub_config
# OTHER bootable partition config begins
  title $LABEL
  $OTHER
# OTHER bootable partition config ends
EOF
fi
done
 # Done, now we must install grub:
 chmod 644 /boot/grub/$grub_config
 installcolor;
}

checkp_dialog()
{
 if [ ! -r $1 ]; then
  $DIALOG --title "DEVICE FILE NOT FOUND" --yesno "I can't find a \
device named '$1'. Are you sure you want to use this device \
name?" 7 60
  return $?;
 fi
}

find_kernel() {
grub --no-floppy --batch << EOF 1>$TMP/boot.vmlinuz 2>/dev/null
find /boot/vmlinuz
quit
EOF
grub --no-floppy --batch << EOF 1>$TMP/vmlinuz 2>/dev/null
find /vmlinuz
quit
EOF
}

cant_find_kernel() {
cat << EOF > $TMP/tmpmsg
`grep $1 $TMP/fdisk.list`

I can't find the kernel for the above linux system.
I looked in /vmlinuz and /boot/vmlinuz.

Where on the above linux system is the kernel you want to use?
EOF

    $DIALOG --title "KERNEL LOCATION" --inputbox "`cat $TMP/tmpmsg`" 13 68 2> $TMP/kernel
    if [ $? = 1 -o $? = 255 ]; then
     break;
    fi
    KERNEL="kernel `cat $TMP/kernel` vga=normal root=$2 ro"
}

install_grub()
{
     if probe /dev/hda; then
     dd if=/dev/hda of=/boot/grub/mbr.$$ bs=512 count=1 >/dev/null 2>&1
     mbr_dev=/dev/hda
     elif probe /dev/sda; then
     dd if=/dev/sda of=/boot/grub/mbr.$$ bs=512 count=1 >/dev/null 2>&1
     mbr_dev=/dev/sda
     else
     mbr_dev=hd0
     fi
      
     p1=`echo $ROOT_DEVICE | cut -b9-`
     p2=$(( $p1 - 1 ))

cat << EOF >>/boot/grub/$grub_config
title Install grub to MBR on first hard drive ($mbr_dev)
root (hd0,$p2)
setup (hd0)
title Install grub to floppy disk (on /dev/fd0)
pause Insert a floppy disk and press enter!
root (hd0,$p2)
setup (fd0)
title Install grub to root Linux partition (on $ROOT_DEVICE)
root (hd0,$p2)
setup (hd0,$p2)
EOF

 if [ "$INSTALL" = "NO" ]; then
    $DIALOG --title "DONE" --msgbox \
    "Created /boot/grub/$grub_config file." 5 40
    rm -f $TMP/tmpmsg $TMP/reply
    exit
else
if [ "$MBR" = "NO" ]; then
if [ "$GRUB_TARGET" = "/dev/fd0" ]; then
grub --device-map=/boot/grub/device.map --batch << EOF 1>/dev/null 2>/dev/null
root (hd0,$p2)
setup (fd0)
quit
EOF
SUCCESS=$?
installed="installed on the floppy disk"
else
grub --device-map=/boot/grub/device.map --no-floppy --batch << EOF 1>/dev/null 2>/dev/null
root (hd0,$p2)
setup (hd0,$p2)
quit
EOF
SUCCESS=$?
installed="installed on the Linux partition $ROOT_DEVICE"
fi
else
grub --device-map=/boot/grub/device.map --no-floppy --batch << EOF 1>/dev/null 2>/dev/null
root (hd0,$p2)
setup (hd0)
quit
EOF
SUCCESS=$?
installed="installed on the MBR of $mbr_dev"
fi  
fi
}

installcolor()
{
if [ "$GRUB_TARGET" = "/dev/fd0" ]; then
while :
do
     $DIALOG --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 first" 2> $TMP/reply
      if [ ! $? = 0 ]; then
        exit
      elif ! (dd if=/dev/fd0 of=/dev/null bs=1 count=1 1>/dev/null 2>/dev/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
        $DIALOG --title "Formatting floppy..." --infobox \
        "fdformat -n $fd" 3 35
        fdformat -n $fd || continue
      elif [ -x "`type -path superformat`" ]; then
        $DIALOG --title "Formatting floppy..." --infobox \
        "superformat -f $fd" 3 35
        superformat -f $fd || continue
      else
         $DIALOG --title "ERROR" --msgbox \
        "Can't find \`fdformat' or \`superformat' to format floppy." 5 65
         continue
      fi
      fi
        break
done
fi

 if [ ! "$INSTALL" = "NO" ]; then
   $DIALOG --infobox "\nInstalling the Grub BootLoader..." 5 40
   sleep 2 
 fi
 
 install_grub
 
 if [ ! "$SUCCESS" = "0" ]; then # some GRUB error occured
  $DIALOG --title "GRUB INSTALL ERROR # $SUCCESS" --msgbox \
  "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 should still be possible to get GRUB working by \
  editing the /boot/grub/menu.lst or grub.conf and reinstalling GRUB manually. \
  See the GRUB man page and info documentation in /usr/info for more help." 11 60
 else
  $DIALOG --title "GRUB INSTALL SUCCESS" --msgbox \
  "Grub was successfully $installed." 5 70
 fi
}

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

   DIALOG=`type -path dialog`
   if [ ! -x "$DIALOG" ]; then
     echo "This script requires the \`dialog' program to run."
     exit 1
   fi

 if [ ! `whoami` = "root" ]; then
     $DIALOG --title "NOT ROOT" --msgbox \
     "You need to be \`root' to run this script." 5 50
     exit
 fi

 if [ ! -x "`type -path grub`" ]; then
     $DIALOG --title "CAN'T FIND GRUB" --msgbox \
     "I can't find \`grub' on the system." 5 40
     exit
 else
 if grep -q /boot/grub/grub.conf 2>/dev/null `type -path grub` ; then
 grub_config=grub.conf
 else
 grub_config=menu.lst
 fi
 if [ ! -f /boot/grub/stage2 -o ! -f /boot/grub/stage1 ]; then
 if [ -d /usr/share/grub -o -d /usr/lib/grub ]; then
 mkdir -p /boot/grub
 if [ -d /usr/share/grub ]; then
 cp -p /usr/share/grub/*/*stage* /boot/grub 2>/dev/null
 elif [ -d /usr/lib/grub ]; then
 cp -p /usr/lib/grub/*/*stage* /boot/grub 2>/dev/null
 fi
 if [ ! -f /boot/grub/stage2 -o ! -f /boot/grub/stage1 ]; then
 echo "Can't find \`stage1 and or stage2' from the grub package."
 echo "Find and put them in the /boot/grub directory."
 exit
 fi 
 else
 echo "Can't find \`stage1 and or stage2' from the grub package."
 echo "Find and put them in the /boot/grub directory."
 exit
 fi
 
 fi
 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.
# Determine the root partition (such as /dev/hda2)
  ROOT_DEVICE="`mount | grep " on / " | cut -d ' ' -f 1`"
  if [ "$ROOT_DEVICE" = "" ]; then
  ROOT_DEVICE="`grep -v "#" /etc/fstab | grep " / " | awk '{print $1}'`"
  fi  

 # OK, now let's see if we should automate things:
 $DIALOG --title "GRUBCONFIG v1.5 Kent Robotti <robotti@godmail.com>" --menu \
 "GRUB is a generic bootloader.  There's a simple installation which tries\
 to automatically set up GRUB to boot Linux (also DOS, Windows, BSD, HURD,\
 and OS/2 if found).  For more advanced users, the expert option offers\
 more control over the installation process.  Since GRUB does not work in\
 all cases (and can damage partitions if incorrectly installed), there's\
 the third (safe) option, which is to skip installing GRUB for now." 17 67 3 \
 "simple" "Try to install GRUB automatically" \
 "expert" "Use expert grub setup menu" \
 "skip" "Do not install GRUB" 2> $TMP/reply
 if [ $? = 1 -o $? = 255 ]; then
  exit
 fi
 REPLY="`cat $TMP/reply`"
 rm -f $TMP/reply
 if [ "$REPLY" = "skip" ]; then
  exit
 elif [ "$REPLY" = "simple" ]; then
  # Do simple GRUB setup
  simplegrub
  exit
 fi
 # drop through to last option: (use the expert menus)
 while [ 0 ]; do
  $DIALOG --title "EXPERT GRUB INSTALLATION" --menu \
"This menu directs the creation of the GRUB config file. \
To install, you make a new GRUB configuration file by creating a new header \
and then adding one or more bootable partitions to the file. Once you've done \
this, you can select the install option. If you make \
a mistake, you can always start over by choosing 'Begin'. \
Which option would you like?" 19 76 7 \
"Begin" "Start GRUB configuration with a new GRUB header" \
"Linux" "Add a Linux partition to the GRUB config file" \
"OTHER" "Add a DOS/WIN/BSD/OS2/HURD partition to the GRUB config file" \
"Install" "Install GRUB" \
"Skip" "Skip GRUB installation or exit this menu" \
"View" "View your current /boot/grub/menu.lst or grub.conf" \
"Help" "Read the GRUB BootLoader HELP file" 2> $TMP/reply
  if [ $? = 1 -o $? = 255 ]; then
   REPLY="Skip"
  else
   REPLY="`cat $TMP/reply`"
  fi
  rm -r $TMP/reply
  if [ "$REPLY" = "Begin" ]; then
   $DIALOG --title "SELECT GRUB TARGET LOCATION" --menu "GRUB can be installed \
to a variety of places: the master boot record of your first hard drive, \
the superblock of your root Linux partition (which could then be made the \
bootable partition with fdisk), 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:" 17 68 4 \
"Root" "Use superblock of the root Linux partition" \
"Floppy" "Use a formatted floppy disk in the boot drive" \
"MBR" "Use the Master Boot Record (possibly unsafe)" \
"Config" "Just Create grub config file, but don't install grub" \
2> $TMP/reply
   if [ $? = 1 -o $? = 255 ]; then
    HDR="no"
    continue;
   else
    LNX="no"
    TG="`cat $TMP/reply`"
   fi
   rm -r $TMP/reply
   if [ "$TG" = "MBR" ]; then
    MBR=YES
   elif [ "$TG" = "Root" ]; then
    MBR=NO
      elif [ "$TG" = "Floppy" ]; then
    GRUB_TARGET="/dev/fd0"    
    MBR=NO
      elif [ "$TG" = "Config" ]; then
      INSTALL=NO
      else
    HDR="no"
    continue;
   fi
    cat << EOF > $TMP/grub.conf
# GRUB configuration file \`/boot/grub/$grub_config'.
# generated by \`grubconfig'.  `date +%c 2>/dev/null`
#
# Start GRUB global section
#timeout 30
# End GRUB global section
EOF
    HDR="yes"
  elif [ "$REPLY" = "Linux" ]; then
   if [ "$HDR" = "yes" ]; then
echo "   Device Boot    Start       End    Blocks   Id  System" > $TMP/tmpmsg1
PROBE | grep "83  Linux" | grep -v "Ext'd" | grep -v "Extend" > $TMP/tmpmsg2

if [ -s $TMP/tmpmsg2 ]; then
cat $TMP/tmpmsg1 $TMP/tmpmsg2 > $TMP/tmpmsg3
echo "Press the [Space] key and tag with X the ones you want." >> $TMP/tmpmsg3

echo '$DIALOG --title "POSSIBLE LINUX PARTITIONS" --checklist "`cat $TMP/tmpmsg3`" 24 70 14 \' > $TMP/tmpmsg4

cat $TMP/tmpmsg2 | sed "/^$/d" | while read device ; do
device_name=`echo $device | cut -d ' ' -f 1`
echo "$device_name" | sed "/^$/d" | sed -e 's/.*/"&" "" off \\/' >> $TMP/tmpmsg4
done
echo "2> $TMP/parts" >> $TMP/tmpmsg4

    . $TMP/tmpmsg4
    if [ $? = 1 -o $? = 255 ]; then
     rm -f $TMP/tmpmsg*
     continue
    else
     cat $TMP/tmpmsg2 > $TMP/fdisk.list
     rm -f $TMP/tmpmsg*
    fi
fi
    cat $TMP/parts | sed "s/\"//g" | sed "/^$/d" > $TMP/parts.a
    
    for PART in `cat $TMP/parts.a` ; do
    checkp_dialog $PART
    if [ ! $? = 0 ]; then
     break;
    fi

cat << EOF > $TMP/tmpmsg
`grep $PART $TMP/fdisk.list`

Now you must select a unique name for the above partition.
You'll use this name to specify a partition to boot from
the GRUB menu. 'Redhat Linux, Slackware Linux, etc.' might
not be a bad choice.
EOF

    $DIALOG --title "SELECT PARTITION NAME" --inputbox "`cat $TMP/tmpmsg`" 13 65 2> $TMP/reply
    if [ $? = 1 -o $? = 255 ]; then
     break;
    fi
    LABEL="`cat $TMP/reply`"
  p1=`echo $PART | cut -b9-`
  p2=$(( $p1 - 1 ))
  if [ ! "$found_kernel" = "yes" ]; then
  find_kernel
  found_kernel=yes
  fi
  if grep -q "(hd0,$p2)" $TMP/boot.vmlinuz 2>/dev/null ; then
  KERNEL="kernel /boot/vmlinuz vga=normal root=$PART ro"
  elif grep -q "(hd0,$p2)" $TMP/vmlinuz 2>/dev/null ; then
  KERNEL="kernel /vmlinuz vga=normal root=$PART ro"
  else
  cant_find_kernel $PART $PART
  fi
    cat << EOF >> $TMP/grub.conf
# Linux bootable partition config begins
  title $LABEL (on $PART)
  root (hd0,$p2)
  $KERNEL
# Linux bootable partition config ends
EOF
initrd $PART $TMP/grub.conf
done
   else
    $DIALOG --title "CAN'T ADD LINUX PARTITION" --msgbox "You can't add \
partitions unless you start over with a new GRUB header." 6 60
    continue
   fi
   LNX="yes"
  elif [ "$REPLY" = "OTHER" ]; then
   if [ "$HDR" = "yes" ]; then
echo "   Device Boot    Start       End    Blocks   Id  System" > $TMP/tmpmsg1
PROBE | grep "DOS
Win
FAT12
FAT16
HPFS" | grep -v "Ext'd" | grep -v "Extend" > $TMP/tmpmsg2

if [ -s $TMP/tmpmsg2 ]; then
cat $TMP/tmpmsg1 $TMP/tmpmsg2 > $TMP/tmpmsg3
echo "Press the [Space] key and tag with X the ones you want." >> $TMP/tmpmsg3

echo '$DIALOG --title "POSSIBLE DOS/WIN/OS2 PARTITIONS" --checklist "`cat $TMP/tmpmsg3`" 24 70 14 \' > $TMP/tmpmsg4

cat $TMP/tmpmsg2 | sed "/^$/d" | while read device ; do
device_name=`echo $device | cut -d ' ' -f 1`
echo "$device_name" | sed "/^$/d" | sed -e 's/.*/"&" "" off \\/' >> $TMP/tmpmsg4
done
echo "2> $TMP/parts" >> $TMP/tmpmsg4

    . $TMP/tmpmsg4
    if [ $? = 1 -o $? = 255 ]; then
     rm -f $TMP/tmpmsg*
     continue
    else
     cat $TMP/tmpmsg2 > $TMP/fdisk.list
     rm -f $TMP/tmpmsg*
    fi
fi

echo "   Device Boot    Start       End    Blocks   Id  System" > $TMP/tmpmsg1
PROBE | grep "BSD
HURD" | grep -v "Ext'd" | grep -v "Extend" > $TMP/tmpmsg2

if [ -s $TMP/tmpmsg2 ]; then
cat $TMP/tmpmsg1 $TMP/tmpmsg2 > $TMP/tmpmsg3
echo "Press the [Space] key and tag with X the ones you want." >> $TMP/tmpmsg3
echo '$DIALOG --title "POSSIBLE BSD/HURD PARTITIONS" --checklist "`cat $TMP/tmpmsg3`" 24 70 14 \' > $TMP/tmpmsg4

cat $TMP/tmpmsg2 | sed "/^$/d" | while read device ; do
device_name=`echo $device | cut -d ' ' -f 1`
echo "$device_name" | sed "/^$/d" | sed -e 's/.*/"&" "" off \\/' >> $TMP/tmpmsg4
done
echo "2>> $TMP/parts" >> $TMP/tmpmsg4

    . $TMP/tmpmsg4
    if [ $? = 1 -o $? = 255 ]; then
     rm -f $TMP/tmpmsg*
     continue
    else
     cat $TMP/tmpmsg2 >> $TMP/fdisk.list
     rm -f $TMP/tmpmsg*
    fi
fi
    cat $TMP/parts | sed "s/\"//g" | sed "/^$/d" > $TMP/parts.a
    
    for PART in `cat $TMP/parts.a` ; do
    checkp_dialog $PART
    if [ ! $? = 0 ]; then
     break;
    fi

cat << EOF > $TMP/tmpmsg
`grep $PART $TMP/fdisk.list`

Now you must select a unique name for the above partition.
You'll use this name to specify a partition to boot from
the GRUB menu. 'DOS, Win2000, WinXP, FreeBSD, GNU/Hurd,
etc.' might not be a bad choice.
EOF

    $DIALOG --title "SELECT PARTITION NAME" --inputbox "`cat $TMP/tmpmsg`" 13 65 2> $TMP/reply
    if [ $? = 1 -o $? = 255 ]; then
     break;
    fi
    LABEL="`cat $TMP/reply`"

    p1=`echo $PART | cut -b9-`
    p2=$(( $p1 - 1 ))

    other $PART
  
     cat << EOF >> $TMP/grub.conf
# OTHER bootable partition config begins
  title $LABEL (on $PART)
  $OTHER
# OTHER bootable partition config ends
EOF
done
   else
    $DIALOG --title "CAN'T ADD OTHER PARTITION" --msgbox "You can't add \
partitions unless you start over with a new GRUB header." 6 60
    continue
   fi
  elif [ "$REPLY" = "Install" ]; then
   if [ "$LNX" = "no" ]; then
    $DIALOG --title "CAN'T INSTALL GRUB" --msgbox "GRUB could not be \
installed. If you have not created a GRUB configuration file by defining \
a new header and adding at least one Linux bootable partition to it, you \
must do so before installing GRUB." 8 70
    continue
   else
    if [ "$REPLY" = "Install" ]; then
     if [ -r $TMP/grub.conf ]; then
      if [ -r /boot/grub/$grub_config ]; then
       mv /boot/grub/$grub_config /boot/grub/$grub_config.old.$$
      fi
      cp $TMP/grub.conf /boot/grub/$grub_config
      chmod 644 /boot/grub/$grub_config
     fi
    fi    
       installcolor;
   fi
   rm -f $TMP/tmpmsg $TMP/reply
   break
  elif [ "$REPLY" = "Skip" ]; then
   rm -f $TMP/grub.conf $TMP/kernel $TMP/tmpmsg* $TMP/reply $TMP/*vmlinuz
   break
  elif [ "$REPLY" = "View" ]; then
   if [ -r $TMP/grub.conf ]; then
    $DIALOG --title "YOUR NEW /boot/grub/$grub_config" --textbox "$TMP/grub.conf" 22 70
   else
    if [ -r /boot/grub/$grub_config ]; then
     $DIALOG --title "YOUR OLD /boot/grub/$grub_config" --textbox "/boot/grub/$grub_config" 22 70
    else
     $DIALOG --title "NO CONFIG FILE FOUND" --msgbox "Sorry, but you don't \
have a GRUB configuration file that can be viewed." 6 60
    fi
   fi
  elif [ "$REPLY" = "Help" ]; then
cat << EOF > $TMP/tmpmsg
These are the options you can use at the GRUB expert install menu:

1. Start GRUB configuration with a new GRUB header.

   This is the first step to start a new configuration 
   file, /boot/grub/menu.lst or grub.conf, with no OS's
   entered into it (yet).

   You will be asked where you want to install GRUB. This
   can be to your Master Boot Record, your root partition's
   superblock (for OS/2's Boot Manager), or to a 
   formatted floppy disk. 
   
   Try installing to a floppy disk first, if it works
   on the floppy it will probably work on the MBR or
   root partition.
   
   Once you've started a new /boot/grub/menu.lst or grub.conf,
   you must add at least one OS to it before you can install GRUB.

   Next you'll want to add at least one Linux partition, the
   current one.

   You then may want to add OTHER partitions if you have any.
   
2. Menu choices to add Linux and OTHER DOS/WIN/BSD/OS2/HURD
   to the /boot/grub/menu.lst or grub.conf config file.

   These all pretty much work the same way. You'll be asked 
   which partitions you want to include in the grub menu, and 
   you'll also be asked to assign a label to each partition.
   The label can be any name such as "Redhat Linux,
   Slackware Linux" or "Win2000".

3. Install GRUB
   
   Once you've looked at your new config file and are 
   satisfied with the way it looks, pick this choice to 
   install GRUB and exit the GRUB installation menu.

Other options:

  -- View your current /boot/grub/menu.lst or grub.conf file

   If you haven't begun building a new menu.lst or grub.conf, this
   choice will let you take a look at the existing one in /boot/grub
   on your root Linux partition. 

   If you have begun constructing a new menu.lst or grub.conf, this
   option will let you view the progress on that.

 -- Skip GRUB installation or exit this menu

  This option skips GRUB installation. Use this if you don't 
  want to install GRUB, or if you've already installed it and 
  want to exit.

 -- Help - Read this GRUB BootLoader HELP file 
 
-----------

Other GRUB information:

  GRUB on the Master Boot Record can be removed easily. 
  MS-DOS's fdisk has a 'C:\ fdisk /mbr' option that restores the
  master boot record. You can also restore your original boot 
  sector from the copy 'grubconfig' makes in /boot/grub/mbr.NUMBER.
  This is probably the best way to go about it.
  # dd if=/boot/grub/mbr.NUMBER of=/dev/hda bs=512 count=1
  Use 'of=/dev/sda' if you have a SCSI drive.
  
--- end of GRUB help
EOF
   $DIALOG --title "GRUB INSTALLATION HELP" --textbox "$TMP/tmpmsg" 22 72
  fi
 done
