#!/bin/sh
#
# Copyright 1994, 1998 Patrick Volkerding, Moorhead, Minnesota USA
# 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.
#
#TMP=/var/log/setup/tmp
TMP=/tmp
# Set color menu mode by default:
COLOR=on
# This is a different 'probe' than the function below.
if [ -x /sbin/probe ]; then
 PROBE=/sbin/probe
else
 PROBE=/sbin/fdisk
fi

# check device map for grub
T_RX=/mnt
if [ -f /tmp/SeTrootdev ] ; then
  ROOT_DEVICE=`cat /tmp/SeTrootdev`
else
  ROOT_DEVICE=""
fi
if [ "$ROOT_DEVICE" = "" ] ; then
  T_RX=/
fi
grub_shell=$T_RX/usr/sbin/grub
device_map=$T_RX/boot/grub/device.map

$grub_shell --batch --device-map=$device_map <<EOF
quit
EOF

 dialog --title "Where do you want to install GRUB ?" --menu \
"GRUB could be installed below spaces:\n\
\n\
 1. MBR(Master Boot Record) of First  HDD(/dev/hda or /dev/sda) \n\
 2. Top of Linux partition(You need to set bootable flag for that \n\
    partition or you need some other bootloader, such as \n\
    PartitionMagic) \n\
 3. preformatted floppy disk \n\
\n\
It's more safe to select 2 or 3 option, but you need some additional \n\
configuration \
for 2(set bootable flag for that partition with fdisk). \n\
Where do you want to install ? \n" \
21 74 4 \
"MBR" "install to Master Boot Record" \
"Root" "install to the installed Linux partition" \
"Floppy" "install to floppy(/dev/fd0)" \
"Quit" "Quit grub installation" 2> $TMP/reply
 if [ $? = 1 -o $? = 255 ]; then
  exit
 fi
 TG="`cat $TMP/reply`"
 rm -r $TMP/reply

if [ "$TG" = "Quit" ]; then
  exit
fi

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

 dialog --title "Input root partition" --inputbox \
"Which partition would you like GRUB to boot? \n\
(such as /dev/hda2) \n\
" \
10 62 2> /tmp/grubConfig_rootDev$$

ROOT_DEVICE=`cat /tmp/grubConfig_rootDev$$`
rm -f /tmp/grubConfig_rootDev$$

fi

if [ "$ROOT_DEVICE" = "" ] ; then
  echo "Exit."
  exit
fi

 dialog --title "Extra kernel parameters (append)" --inputbox \
"If you need to pass parameters to the kernel when booting, enter \n\
them here. If you don't need any, just hit ENTER to continue.\n\
(e.g. ide0=dma ide1=dma) \n\
" \
12 72 2> /tmp/grubConfig_append$$

if [ $? = 1 -o $? = 255 ] ; then
  echo "Exit."
  rm -f /tmp/grubConfig_append$$
  exit
fi

APPEND=`cat /tmp/grubConfig_append$$`
rm -f /tmp/grubConfig_append$$

 # if $ROOT_DEVICE is /dev/sdc3,
 # $tmp_disk is /dev/sdc, $tmp_part is 3
 tmp_disk=`echo $ROOT_DEVICE | sed 's%[0-9]*$%%'`
 tmp_part=`echo $ROOT_DEVICE | sed "s%$tmp_disk%%"` 
 tmp_drive=`grep -v '^#' $device_map | grep $tmp_disk \
     | sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'`
 echo $tmp_drive | sed "s%)$%,`expr $tmp_part - 1`)%" > /tmp/grubdrv
GRUB_ROOT=`cat /tmp/grubdrv`

 if [ "$TG" = "MBR" ]; then
   GRUB_TARGET="(hd0)"

 elif [ "$TG" = "Root" ]; then
     GRUB_TARGET=$GRUB_ROOT
 elif [ "$TG" = "Floppy" ]; then
     GRUB_TARGET="(fd0)"
 fi
#echo "tmp_disk = $tmp_disk"
#echo "tmp_part = $tmp_part"
#echo "tmp_drive = $tmp_drive"
#echo "GRUB_TARGET= $GRUB_TARGET"

 cat << EOF > $T_RX/etc/grub.conf
# GRUB configuration file
# generated by 'grubconfig'
#
# Start GRUB global section
default 0
timeout 30
fallback 1
splashimage $GRUB_ROOT/boot/grub/grubimg.xpm.gz

title Plamo-3.3
  root $GRUB_ROOT
  kernel $GRUB_ROOT/vmlinuz root=$ROOT_DEVICE ro $APPEND
EOF

 # OK, now let's look for DOS partitions:
 DOSP="`$PROBE -l | grep "DOS
Win95"`"
 DOSP="`echo $DOSP | cut -f 1 -d ' '`"
 if [ ! "$DOSP" = "" ]; then
  TABLE="`echo $DOSP | cut -b1-8`"
  cat << EOF >> $T_PX/etc/grub.conf
# DOS bootable partition config begins
title = $DOSP
  rootnoverify $GRUB_ROOT
  makeactive
  chainloader +1
EOF
fi

 # Done, now we must install grub
 dialog --infobox "\ninstalling GRUB..." 5 40
$grub_shell --batch <<EOF
root $GRUB_ROOT
install $GRUB_ROOT/boot/grub/stage1 d $GRUB_TARGET $GRUB_ROOT/boot/grub/stage2 0x8000 p $GRUB_ROOT/etc/grub.conf
quit
EOF
SUCCESS=$?
 if [ ! "$SUCCESS" = "0" ]; then # some GRUB error occured
  dialog --title "GRUB installation error # $SUCCESS" --msgbox \
"Sorry, but I encounter some errors when installing GRUB.  \
You cannot boot Plamo Linux from HDD now, so you need bootFD or installation CD \
to boot your installed Plamo Linux." 11 74
 fi


