#!/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 "GRUBΥ󥹥ȡ" --menu \
"GRUBϤĤξ˥󥹥ȡ뤹뤳ȤǤޤ:\n\
\n\
 1. ǽΥϡɥǥ(/dev/hda/dev/sda)MBR \n\
 2. Linux ΥѡƥƬ(DOSLinuxfdiskǤΥѡƥ \n\
   ưǽˤ뤫¾Υ֡ȥޥ͡ȤäƵưƤ) \n\
 3.եޥåȺѤΥեåԡǥ \n\
\n\
2  3 ٤аǤ2 ξϸǾȤɬפˤʤޤ \n\
(fdiskȤäƤΥѡƥưǽˤ)ɤӤޤ \n" \
20 74 4 \
"MBR" "Master Boot Recordإ󥹥ȡ" \
"Root" "LinuxѡƥƬ(fdiskǵưǽˤ뤳)" \
"Floppy" "/dev/fd0(A:)إ󥹥ȡ" \
"Quit" "GRUB Υ󥹥ȡ" 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 "rootѡƥ" --inputbox \
"ͥ뵯ưľ / ˥ޥȤѡƥ \n\
ϤƤ \n\
[: /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
fi

 dialog --title "ͥѥ᡼" --inputbox \
"ͥ뵯ưͿ륫ͥѥ᡼򤳤ǻǤޤ\n\
פʾϡñ Enter 򲡤Ƥ\n\
[: ide0=dma ide1=dma (IDE ǥХ DMA  ON ˤ)] \n\
[: max_scsi_luns=1 (ФԤ SCSI  LUN κ)]\n\
[: nosmp (SMP ޥ򥷥󥰥ץåư)]\n" \
13 72 2> /tmp/grubConfig_append$$

if [ $? = 1 -o $? = 255 ] ; then
  echo "ߤޤ"
  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 "\nGRUB򥤥󥹥ȡ..." 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󥹡륨顼 # $SUCCESS" --msgbox \
"ǰʤ顢GRUB򥤥󥹥ȡ뤹ݤ˥顼ȯ褦Ǥ \
ΤޤޤǤ HDDľܵưǤʤΤǡFD˽񤭽Фưǥ \
ȤäƥޥưƤ" 11 74
 fi


