#!/bin/sh

################################################################
# WindowsNT/2000/XP  OS  + LILO δʰꥹץ
################################################################

TMP=/tmp

# 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
# 
PROBE=/sbin/fdisk

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

LANG=C $PROBE -l > $TMP/probe$$

LINUXP=`cat $TMP/probe$$ | tr -d '*'|tr -s ' '|cut -f 1,5,6 -d ' '|grep -e '83 Linux'|cut -f 1,3 -d ' '`
LINUXP_NUM=`cat $TMP/probe$$ | tr -d '*'|tr -s ' '|cut -f 1,5,6 -d ' '|grep -e '83 Linux'|wc -l`
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 "顼" --msgbox "NTFSFATΥѡƥ󤬤ޤ!" 6 65
  exit
fi
if [ $LINUXP_NUM = 0 ] ; then
  dialog --title "顼" --msgbox "LinuxΥѡƥ󤬤ޤ!" 6 65
  exit
fi

RECYCLE_LILO=0
if [ -f $T_PX/etc/lilo.conf ] ; then
  dialog --title "lilo.conf Ĥޤ" \
--menu "Ǥ lilo.conf ¸ߤޤ\n\
lilo.conf ƺޤ" 11 60 3 \
"Recycle" "¸lilo.confΤޤޤLILOƥ󥹥ȡ" \
"New"     "lilo.confʤ" \
"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
  if [ "$REPLY" = "Recycle" ] ; then
    RECYCLE_LILO=1
  fi
fi

################################################################
while [ 1 ] ; do
################################################################

dialog --title "Windowsѡƥ" \
--menu "WindowsNT,2000,XPOS󥹥ȡ뤵Ƥ\n\
ѡƥϡ" 21 60 13 \
$DOSP $NTP \
"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
PARTITION_NT=$REPLY

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

if [ "`echo $DOSP|grep \"$PARTITION_NT\"`" = "" ] ; then
  FS_TYPE=ntfs
else
  FS_TYPE=vfat
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
    continue
  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 "顼" --msgbox "$PARTITION_NT  boot.ini ޤ!" 6 65
    continue
  fi
  umount $MOUNT_POINT
  rmdir $MOUNT_POINT
fi

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

if [ $RECYCLE_LILO = 1 ] ; then

  PARTITION_LINUX="`cat $T_PX/etc/lilo.conf|tr -d ' '|grep '^boot='|sed -e 's/boot=//'`"

else

  dialog --title "Linuxѡƥ" \
--menu "LinuxΥ롼ȥѡƥϡ\n\
ΥѡƥPBRLILO򥤥󥹥ȡ뤷ޤ" 21 60 13 \
$LINUXP \
"Begin" "ǽ餫ʤ" 2> $TMP/reply$$

  if [ $? = 1 -o $? = 255 ]; then
    rm -f $TMP/reply$$
    exit
  fi
  REPLY="`cat $TMP/reply$$`"
  rm -f $TMP/reply$$
  if [ "$REPLY" = "Begin" ] ; then
    continue
  fi
  PARTITION_LINUX=$REPLY

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

  dialog --title "LILO  append= Ԥɲ(̵Ƥ⹽ޤ)" \
--inputbox "ư˥ͥѥ᡼錄˻ꤷƤ\n" \
9 60 2> $TMP/reply$$
  REPLY="`cat $TMP/reply$$`"
  rm -f $TMP/reply$$
  APPEND=$REPLY

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

  cat <<EOF > $T_PX/etc/lilo.conf
# LILO configuration file
# generated by 'liloconfig'
#
# Start LILO global section
append="$APPEND"
boot = $PARTITION_LINUX
#compact        # faster, but won't work on all systems.
# delay = 5
vga=vga16    # force sane state
lba32
# ramdisk = 0     # paranoia setting
# End LILO global section
# Linux bootable partition config begins
image = /vmlinuz
  root = $PARTITION_LINUX
  label = Linux
  read-only # Non-UMSDOS filesystems should be mounted read-only for checking
# Linux bootable partition config ends
EOF

fi

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

dialog --infobox "\nLinux Loader 򥤥󥹥ȡ..." 5 40
if [ "$T_PX" = "/" ]; then
  /sbin/lilo 1> /dev/null 2> /dev/null
else
  $T_PX/sbin/lilo -r $T_PX -C /etc/lilo.conf 1> /dev/null 2> /dev/null
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

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

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 " boot.ini γǧ(ܸʸޤ)" --textbox "$TMP/boot.ini$$" 22 74
rm -f $TMP/boot.ini$$

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

  dialog --title "ǧڽס" \
--menu "boot.ini  $PARTITION_NT \n\
LILO  $PARTITION_LINUX  PBR ˽񤭹ߤޤ\n\
\n\
Ȥꤢߤ ${T_PX}boot/ ʲ($PARTITION_LINUX  /boot/ ʲ)\n\
 ${LINUX_PBR}.pbr  boot.ini ֤ޤ2ĤΥե\n\
ϥեåԡʤɤ¸ơWindows  C:\\ ʲ˥ԡ\n\
ɬפޤ(ʤ Linux ϵưޤ)\n\
ɤޤ\n\
Υ˥塼ΡFDפ֤ȡեåԡ Windows ɤ\n\
ǥեޥåȤ${LINUX_PBR}.pbr  boot.ini ¸ޤ\
" 19 65 3 \
"FD" "եåԡ ${LINUX_PBR}.pbr  boot.ini Ȥ¸" \
"Exit" "λ(ǼʬǤʤȤ)" \
"Begin" "ǽ餫ʤ" 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 [ "$REPLY" = "Begin" ] ; then
  continue
fi

break

################################################################
done
################################################################

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

clear
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 vfat /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 vfat /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 "\
եåԡ¸ boot.ini  ${LINUX_PBR}.pbr Ȥ Windows  C:\\ \n\
ʲ˥ԡƤ\n\
\n\
ʤͥκƹۤ򤷤顤LILOƥ󥹥ȡ뤷${LINUX_PBR}.pbr\n\
ʤɬפޤʬϤġƤӻȤ\n\
ǤǽǤ󥹥ȡ塤ġ osldrconfig \n\
ޥɤǤĤǤƤӽФǤޤ\
" 12 70

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

dialog --title "λ" --msgbox "\
ߤ ${T_PX}boot/ ʲ($PARTITION_LINUX  /boot/ ʲ)\n\
 boot.ini  ${LINUX_PBR}.pbr Ȥ Windows  C:\\ ʲ\n\
ԡƤ\n\
\n\
ʤͥκƹۤ򤷤顤LILOƥ󥹥ȡ뤷${LINUX_PBR}.pbr\n\
ʤɬפޤʬϤġƤӻȤ\n\
ǤǽǤ󥹥ȡ塤ġ osldrconfig \n\
ޥɤǤĤǤƤӽФǤޤ\
" 12 70

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

