#!/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

devname_to_grubdevname() {
  # if $1 is /dev/sdc3,
  # $tmp_disk is /dev/sdc, $tmp_part is 3
  tmp_disk=`echo $1 | sed 's%[0-9]*$%%'`
  tmp_part=`echo $1 | 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_DEV_NAME=`cat $TMP/grubdrv$$`
  rm -f $TMP/grubdrv$$
}

# Figure out if we're installing from the hard drive
if [ -r $TMP/SeTT_PX ]; then
 T_PX="`cat $TMP/SeTT_PX`"
else
 if [ "$T_PX" = "" -a ! "$1" = "" ]; then
  T_PX=$1
 else
  T_PX=/
 fi
fi

if [ ! "`echo $T_PX|sed -e's/.*\/$//'`" = "" ] ; then
  T_PX="${T_PX}/"
fi

# 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 HDD \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\
    OS Loader of Windows NT/2000/XP) \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.  If you select 2, this script provides some help \n\
for configuration of OS Loader in WindowsNT/2000/XP partition. \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\
" \
11 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

ROOT_DEVICE_NAME=`basename $ROOT_DEVICE`

 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. ide=nodma) \n\
" \
12 72 'vga16 unicon=eucjp' 2> $TMP/grubConfig_append$$

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

SWAP=`/sbin/fdisk -l | /bin/grep "Linux swap" | /bin/cut -d' ' -f1
if [ ! -z $SWAP ]; then
  echo " resume=$SWAP " >> $TMP/grubConfig_append$$
fi

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

devname_to_grubdevname $ROOT_DEVICE
GRUB_ROOT=$GRUB_DEV_NAME

 if [ "$TG" = "MBR" ]; then
   TARGET_LIST=`LANG=C $PROBE -l|grep "Disk /dev/"|sed -e 's/Disk //g'|tr -d ' '|tr ':' ' '`

   dialog --title "Select a hard disk to install GRUB to MBR" \
          --menu "Which disk would you like to install GRUB to MBR?\n" 21 60 13 \
      $TARGET_LIST \
      "Exit" "Quit grub installation" 2> $TMP/reply$$
   if [ $? = 1 -o $? = 255 ]; then
     rm -f $TMP/reply$$
     exit
   fi
   REPLY="`cat $TMP/reply$$`"
   rm -f $TMP/reply$$
   if [ "$REPLY" = "Exit" ] ; then
     exit
   fi
   MBR_DEVICE=$REPLY
   MBR_DEVICE_NAME=`basename $MBR_DEVICE`
   devname_to_grubdevname "${MBR_DEVICE}1"
   GRUB_TARGET="`echo $GRUB_DEV_NAME|sed 's/,.*//'`)"
   #echo $GRUB_TARGET
   #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/boot/grub/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-4.7 (${ROOT_DEVICE_NAME})
  root $GRUB_ROOT
  kernel $GRUB_ROOT/boot/vmlinuz root=$ROOT_DEVICE ro vt.default_utf8=0 $APPEND
EOF
( cd $T_RX/boot/grub ; ln -sf grub.conf menu.lst )

 # OK, now let's look for DOS partitions:

################
LANG=C $PROBE -l > $TMP/probe$$
DOSP=`cat $TMP/probe$$     | tr -d '*'|tr -s ' '|grep -e FAT|cut -f 1,6 -d ' '`
DOSP_NUM=`cat $TMP/probe$$ | tr -d '*'|grep -e FAT|wc -l`
NTP=`cat $TMP/probe$$      | tr -d '*'|tr -s ' '|grep -e NTFS|cut -f 1,6 -d ' '`
NTP_NUM=`cat $TMP/probe$$  | tr -d '*'|grep -e NTFS|wc -l`
rm -f $TMP/probe$$
WINP_NUM=`expr ${DOSP_NUM} + ${NTP_NUM}`
################

if [ ! $WINP_NUM = 0 ] ; then
  dialog --title "Select Windows partition" \
         --menu "Which partition is drive C: of Windows?\n\
" 21 60 13 \
      $DOSP $NTP \
      "NoWindows" "I don't have Windows" 2> $TMP/reply$$
  if [ $? = 1 -o $? = 255 ]; then
    rm -f $TMP/reply$$
    exit
  fi
  REPLY="`cat $TMP/reply$$`"
  rm -f $TMP/reply$$
  if [ "$REPLY" = "NoWindows" ] ; then
    WINP_NUM=0
  else
    PARTITION_NT=$REPLY
    ################
    if [ "`echo $DOSP|grep \"$PARTITION_NT\"`" = "" ] ; then
      FS_TYPE=ntfs
    else
      FS_TYPE=vfat
    fi
    ################

    if [ ! "$PARTITION_NT" = "" ]; then
      devname_to_grubdevname $PARTITION_NT
      WIN_ROOT=$GRUB_DEV_NAME
      PARTITION_NT_NAME=`basename $PARTITION_NT`
      cat << EOF >> $T_PX/boot/grub/grub.conf
# DOS bootable partition config begins
title = Windows (${PARTITION_NT_NAME})
  rootnoverify $WIN_ROOT
  makeactive
  chainloader +1
EOF
    fi
  fi
fi

################

 if [ "$TG" = "MBR" ]; then
   MBR_BACKUP_FILE=${T_PX}/boot/${MBR_DEVICE_NAME}_backup.mbr
   if [ ! -f $MBR_BACKUP_FILE ] ; then
     dd if=$MBR_DEVICE of=$MBR_BACKUP_FILE bs=512 count=1
   fi
 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/boot/grub/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
  exit
 fi

if [ ! "$TG" = "Root" ] ; then
  exit
fi

################################################################
################################################################

if [ $WINP_NUM = 0 ] ; then
  exit
fi
dialog --title "Configuration of WindowsNT/2000/XP" --yesno \
    "Do you want to configure OS Loader of WindowsNT/2000/XP ?" 6 62
if [ ! $? = 0 ]; then
  exit
fi
################
PARTITION_LINUX=$ROOT_DEVICE
################
if [ "$PARTITION_NT" = "" ] ; then
  dialog --title "Error" --msgbox "Neither NTFS nor FAT is found!" 6 65
  exit
fi
################################
################
MOUNT_POINT=`mount |grep "$PARTITION_NT "|cut -f 3 -d ' '`
if [ ! "$MOUNT_POINT" = "" ] ; then
  if [ ! -f $MOUNT_POINT/boot.ini ] ; then
    dialog --title "Error" --msgbox "boot.ini is not found in $PARTITION_NT !" 6 65
    exit
  fi
  cat $MOUNT_POINT/boot.ini > $T_PX/boot/boot.ini
else
  MOUNT_POINT=$TMP/nt$$
  mkdir $MOUNT_POINT
  mount -t $FS_TYPE $PARTITION_NT $MOUNT_POINT
  cat $MOUNT_POINT/boot.ini > $T_PX/boot/boot.ini
  if [ ! -f $MOUNT_POINT/boot.ini ] ; then
    dialog --title "Error" --msgbox "boot.ini is not found in $PARTITION_NT !" 6 65
    exit
  fi
  umount $MOUNT_POINT
  rmdir $MOUNT_POINT
fi
################
LINUX_PBR="`basename $PARTITION_LINUX`"
dialog --infobox "\nCreating PBR image..." 5 40
dd if=$PARTITION_LINUX of=$T_PX/boot/${LINUX_PBR}.pbr bs=512 count=1 1> /dev/null 2> /dev/null
################
if [ "`cat $T_PX/boot/boot.ini|grep ${LINUX_PBR}.pbr`" = "" ] ; then
  echo "C:\\${LINUX_PBR}.pbr = \"Plamo Linux (${LINUX_PBR})\"
" >> $T_PX/boot/boot.ini
fi
cat $T_PX/boot/boot.ini|tr -d '\r' > $TMP/boot.ini$$
################
dialog --title "Confirm new boot.ini" --textbox "$TMP/boot.ini$$" 22 74
rm -f $TMP/boot.ini$$
################
dialog --title "Do you want to save ${LINUX_PBR}.pbr and boot.ini to FD?" \
    --menu "New boot.ini has been prepared from $PARTITION_NT.\n\
\n\
New boot.ini and ${LINUX_PBR}.pbr have been put in ${T_PX}boot/.\n\
You must copy these two files to C:\\ of Windows.\n\
If you want to save these two files to a floppy disk, select\n\
FD. Otherwise, these files will copy to rescue FD(bootFD) \n\
and you can copy from rescue FD to Windows." 21 65 3 \
"FD" "Save ${LINUX_PBR}.pbr and boot.ini to a floppy disk" \
"Exit" "Quit grub installation" \
2> $TMP/reply$$

if [ $? = 1 -o $? = 255 ]; then
  rm -f $TMP/reply$$
  exit
fi
REPLY="`cat $TMP/reply$$`"
rm -f $TMP/reply$$
NO_FD=0
if [ "$REPLY" = "Exit" ] ; then
  NO_FD=1
fi
################################

################################################################
if [ $NO_FD = 0 ] ; then
################################################################

clear
MOUNT_POINT_FD=`mount |grep "/dev/fd0 "|cut -f 3 -d ' '`
if [ ! "$MOUNT_POINT_FD" = "" ] ; then
  echo "umount $MOUNT_POINT_FD ..."
  umount $MOUNT_POINT_FD
  MPF=$MOUNT_POINT_FD
else
  MOUNT_POINT_FD=$TMP/fd$$
  MPF=""
fi

echo Insert a floppy disk, and hit Enter key.
echo It will be formatted when unformatted.
read ANS
clear
echo mounting...
if [ "$MPF" = "" ] ; then
  mkdir $MOUNT_POINT_FD
fi
mount -t vfat /dev/fd0 $MOUNT_POINT_FD
if [ ! $? = 0 ] ; then
while [ 1 ] ; do
  echo Hit Enter key to format floppy disk.
  read ANS
  clear
  echo Formatting...
  fdformat /dev/fd0u1440
  echo Creating FAT...
  mkdosfs /dev/fd0
  echo Do you want to retry?[y/N]
  read ANS
  if [ "$ANS" = "Y" -o "$ANS" = "y" ]; then
    continue
  fi
  break
done
echo unmounting...
mount -t vfat /dev/fd0 $MOUNT_POINT_FD
fi
echo Copying boot.ini and ${LINUX_PBR}.pbr to the floppy disk.
cp $T_PX/boot/boot.ini $T_PX/boot/${LINUX_PBR}.pbr $MOUNT_POINT_FD/.
echo unmounting...
umount $MOUNT_POINT_FD
if [ "$MPF" = "" ] ; then
  rmdir $MOUNT_POINT_FD
fi
echo OK. Hit Enter key.
read ANS

dialog --title "Confirmation" --msgbox "\
\n\
Please copy boot.ini and ${LINUX_PBR}.pbr in floppy disk to C:\\ of \n\
Windows. \n\
\n\
" 8 70

################################################################
else
################################################################

dialog --title "Confirmation" --msgbox "\
\n\
Please copy boot.ini and ${LINUX_PBR}.pbr in /boot/ of $PARTITION_LINUX \n\
to C:\\ of Windows.\n\
" 9 70

################################################################
fi
################################################################
