#!/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
BOOT_DEVICE=`cat /proc/mounts | grep '/mnt/boot' | cut -d' ' -f1`
if [ $BOOT_DEVICE".x" = ".x" ]; then
    BOOT_DEVICE=$ROOT_DEVICE
    BOOT_DIR="/boot"
else
  BOOT_DIR=""
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. ϡɥǥΥޥ֡ȥ쥳(MBR) \n\
 2. Linux ΥѡƥƬ(DOSLinuxfdiskǤΥѡƥ \n\
   ưǽˤ뤫WindowsNT/2000/XPOSΤ褦¾Υ֡ \n\
   ȥޥ͡ȤäƵưƤ) \n\
 3.եޥåȺѤΥեåԡǥ \n\
\n\
2  3 ٤аǤ2 ξϸǾȤɬפˤʤޤ\n\
2 ξˤϤġκǸ WindowsNT/2000/XP  OS  \n\
꤬ԤޤɤӤޤ " \
21 74 4 \
"MBR" "Master Boot Recordإ󥹥ȡ" \
"Root" "LinuxѡƥƬ" \
"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" \
11 62 2> $TMP/grubConfig_rootDev$$

ROOT_DEVICE=`cat $TMP/grubConfig_rootDev$$`
#rm -f $TMP/grubConfig_rootDev$$

fi

if [ "$ROOT_DEVICE" = "" ] ; then
  echo "ߤޤ"
  exit
fi

ROOT_DEVICE_NAME=`basename $ROOT_DEVICE`

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

 dialog --title "ͥѥ᡼" --inputbox \
"ͥ뵯ưͿ륫ͥѥ᡼򤳤ǻǤޤ\n\
vga16 unicon=eucjp uniconȤλǤuniconȤʤ \n\
 Enter 򲡤Ƥ\n\
[: ide=nodma (IDE ǥХ DMA  OFF ˤ)] \n\
[: max_scsi_luns=1 (ФԤ SCSI  LUN κ)]\n\
[: nosmp (SMP ޥ򥷥󥰥ץåư)]\n" \
13 72 "vga16 unicon=eucjp $RESUME" 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$$

devname_to_grubdevname $BOOT_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 "MBR˥󥹥ȡ뤹ǥ" \
          --menu "ɤΥǥMBRGRUB򥤥󥹥ȡ뤷ޤ\n" 21 60 13 \
      $TARGET_LIST \
      "Exit" "" 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_DIR}/grub/grubimg.xpm.gz

title Plamo-4.7 (${ROOT_DEVICE_NAME})
  root $GRUB_ROOT
  kernel ${GRUB_ROOT}${BOOT_DIR}/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 "Windowsѡƥ" \
         --menu "Windows 󥹥ȡ뤵Ƥ\n\
ѡƥϡ" 21 60 13 \
      $DOSP $NTP \
      "NoWindows" "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=msdos
    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

# PARTITION_LINUX=$BOOT_DEVICE
# echo "PARTITION_LINUX:$PARTITION_LINUX"
# echo "GRUB_TARGET: $GRUB_TARGET"
# echo "root $GRUB_ROOT"
# echo "install ${GRUB_ROOT}${BOOT_DIR}/grub/stage1 d $GRUB_TARGET ${GRUB_ROOT}${BOOT_DIR}/grub/stage2 0x8000 p ${GRUB_ROOT}${BOOT_DIR}/grub/grub.conf"
# sleep 120
 dialog --infobox "\nGRUB򥤥󥹥ȡ..." 5 40
$grub_shell --batch <<EOF
root $GRUB_ROOT
install ${GRUB_ROOT}${BOOT_DIR}/grub/stage1 d $GRUB_TARGET ${GRUB_ROOT}${BOOT_DIR}/grub/stage2 0x8000 p ${GRUB_ROOT}${BOOT_DIR}/grub/grub.conf
quit
EOF
SUCCESS=$?
 if [ ! "$SUCCESS" = "0" ]; then # some GRUB error occured
  dialog --title "GRUB󥹡륨顼 # $SUCCESS" --msgbox \
"ǰʤ顢GRUB򥤥󥹥ȡ뤹ݤ˥顼ȯ褦Ǥ \
ΤޤޤǤ HDDľܵưǤʤΤǡFD˽񤭽Фưǥ \
ȤäƥޥưƤ" 11 74
  exit
 fi

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

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

if [ $WINP_NUM = 0 ] ; then
  exit
fi
dialog --title "WindowsNT/2000/XP" --yesno \
    "WindowsNT/2000/XPOSꤷޤ" 6 60
if [ ! $? = 0 ]; then
  exit
fi
################
PARTITION_LINUX=$BOOT_DEVICE
################
if [ "$PARTITION_NT" = "" ] ; then
  dialog --title "顼" --msgbox "NTFSFATΥѡƥ󤬤ޤ!" 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 "顼" --msgbox "$PARTITION_NT  boot.ini ޤ!" 6 65
    exit
  fi
  cat $MOUNT_POINT/boot.ini > ${T_PX}/boot/boot.ini
else
  MOUNT_POINT=$TMP/nt$$
  mkdir $MOUNT_POINT
  modprobe ntfs
  modprobe nls_utf8
  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 "顼" --msgbox "$PARTITION_NT  boot.ini ޤ!" 6 65
    exit
  fi
  umount $MOUNT_POINT
  rmdir $MOUNT_POINT
fi
################
LINUX_PBR="`basename $PARTITION_LINUX`"
dialog --infobox "\nPBRե벽..." 5 40
dd if=$PARTITION_LINUX of=${T_PX}/boot/${LINUX_PBR}.pbr bs=512 count=1 1> /dev/null 2> /dev/null
################
PBR_CHECK=`cat ${T_PX}/boot/boot.ini | grep ${LINUX_PBR}.pbr`
if [  $PBR_CHECK".x" = ".x" ] ; 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 " boot.ini γǧ(ܸʸޤ)" --textbox "$TMP/boot.ini$$" 22 74
rm -f $TMP/boot.ini$$
################
dialog --title "ǧڽס" \
    --menu "boot.ini  $PARTITION_NT ޤ\n\
\n\
Ȥꤢߤ ${T_PX}/boot ʲ($PARTITION_LINUX  /boot ʲ)\n\
 ${LINUX_PBR}.pbr  boot.ini ֤ޤ2ĤΥե\n\
ϥեåԡʤɤ¸ơWindows  C:\\ ʲ˥ԡ\n\
ɬפޤ(ʤ Linux ϵưޤ)\n\
ΤˤϡѤ FD ѰդƤΥե \n\
¸μ¤Ǥ쥹塼ǥ \n\
ưŪˤΥեϥ쥹塼ǥ˥ԡ \n\
Τǡ쥹塼ǥɬפʥե򥳥ԡ \n\
ȤǽǤ \n\
Υ˥塼ΡFDפ֤ȡեåԡ Windows ɤ\n\
ǥեޥåȤ${LINUX_PBR}.pbr  boot.ini ¸ޤ\
" 22 65 3 \
"FD" "եåԡ ${LINUX_PBR}.pbr  boot.ini Ȥ¸" \
"Exit" "λ(ǼʬǤʤȤ)" \
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
modprobe floppy
modprobe msdos
modprobe nls_cp932

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

echo եåԡǥ򥻥åȤơEnter򲡤Ƥ
echo FATեޥåȤƤʤΤϡǥեޥåȤޤ
read ANS
clear
echo ޥȤƤޤ...
if [ "$MPF" = "" ] ; then
  mkdir $MOUNT_POINT_FD
fi
mount -t msdos /dev/fd0 $MOUNT_POINT_FD
if [ ! $? = 0 ] ; then
while [ 1 ] ; do
  echo EnterǥեޥåȤ򳫻Ϥޤ
  read ANS
  clear
  echo եåԡʪեޥåȤƤޤ...
  fdformat /dev/fd0u1440
  echo WindowsɤǥƥեޥåȤƤޤ...
  mkdosfs /dev/fd0
  echo եޥåȤʤޤ[y/N]
  read ANS
  if [ "$ANS" = "Y" -o "$ANS" = "y" ]; then
    continue
  fi
  break
done
echo ޥȤƤޤ...
mount -t msdos /dev/fd0 $MOUNT_POINT_FD
fi
echo boot.ini  ${LINUX_PBR}.pbr եåԡ˥ԡƤޤ...
cp ${T_PX}/boot/boot.ini ${T_PX}/boot/${LINUX_PBR}.pbr $MOUNT_POINT_FD/.
echo ޥȤƤޤ...
umount $MOUNT_POINT_FD
if [ "$MPF" = "" ] ; then
  rmdir $MOUNT_POINT_FD
fi
echo λޤEnter򲡤Ƥ
read ANS

dialog --title "λ" --msgbox "\
\n\
եåԡ¸ boot.ini  ${LINUX_PBR}.pbr Ȥ Windows  C:\\ \n\
ʲ˥ԡƤ\n\
\n\
" 8 70

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

dialog --title "λ" --msgbox "\
\n\
ߤ ${T_PX}/boot/ ʲ($PARTITION_LINUX  /boot/ ʲ)\n\
 boot.ini  ${LINUX_PBR}.pbr Ȥ Windows  C:\\ ʲ\n\
ԡƤ\n\
" 9 70

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