#!/bin/sh

################################################################
# Let's use the OS loader of WindowsNT/2000/XP + 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 "Error!" --msgbox "Partition for Windows does not exist!" 6 65
  exit
fi
if [ $LINUXP_NUM = 0 ] ; then
  dialog --title "Error!" --msgbox "Partition for Linux does not exist!" 6 65
  exit
fi

RECYCLE_LILO=0
if [ -f $T_PX/etc/lilo.conf ] ; then
  dialog --title "Found: lilo.conf" \
--menu "lilo.conf already exists.\n\
Do you want to initialize lilo.conf?" 11 60 3 \
"Recycle" "Reinstall only(Don't change lilo.conf)." \
"New"     "Create new lilo.conf." \
"Exit"    "Exit this tool." 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 "Select the Windows partition" \
--menu "Select the Windows NT/2000/XP partition which has OS\n\
loader." 21 60 13 \
$DOSP $NTP \
"Exit" "Exit this tool." 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 "Error!" --msgbox "boot.ini was not found in ${PARTITION_NT}!" 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 "Error!" --msgbox "boot.ini was not found in ${PARTITION_NT}!" 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 "Select the Windows partition" \
--menu "Which is the root partition of Linux?\n\
The LILO will be installed in the PBR of it." 21 60 13 \
$LINUXP \
"Begin" "Go back the beginning." 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 "OPTIONAL append= LINE" \
--inputbox "If you need to pass parameters to the kernel,\n\
enter them here. If you don't need any, just hit ENTER\n\
to continue." \
10 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 "\nInstalling the Linux 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 "\nSaving PBR..." 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 "Save ${LINUX_PBR}.pbr and boot.ini?" \
--menu "New boot.ini has been prepared from ${PARTITION_NT},\n\
and the LILO has been installed in PBR of ${PARTITION_LINUX}.\n\
\n\
For the present, ${LINUX_PBR}.pbr and boot.ini are put on\n\
${T_PX}boot/ (/boot/ of ${PARTITION_LINUX}).  \n\
These two files must be copied to C:\\ of Windows.\n\
Which do you want?\n\
If you select the 'FD', ${LINUX_PBR}.pbr and boot.ini are saved\n\
to a floppy disk after formatting with FAT.\
" 19 70 3 \
"FD"   "Save ${LINUX_PBR}.pbr and boot.ini to a floppy disk." \
"Exit" "Exit this tool(Copy ${LINUX_PBR}.pbr and boot.ini manually)" \
"Begin" "Go back the beginning." 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 "Unmounting ${MOUNT_POINT_FD}..."
  umount $MOUNT_POINT_FD
  MPF=$MOUNT_POINT_FD
else
  MOUNT_POINT_FD=$TMP/fd$$
  MPF=""
fi

echo Insert floppy, then press ENTER.
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 Press ENTER to start format.
  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 Mounting...
mount -t vfat /dev/fd0 $MOUNT_POINT_FD
fi
echo Copying boot.ini and ${LINUX_PBR}.pbr...
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 Complete! Press ENTER.
read ANS

dialog --title "Complete!" --msgbox "\
Don't forget to copy boot.ini and ${LINUX_PBR}.pbr to\n\
C:\\ of Windows after booting Windows.\n\
\n\
After rebuilding the Linux kernel, you must reinstall LILO and \n\
recreate ${LINUX_PBR}.pbr and copy it to C:\\ of Windows. \n\
You can do them using this tool, 'osldrconfig' command, again. \
" 12 70

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

dialog --title "Complete" --msgbox "\
Don't forget to copy boot.ini and ${LINUX_PBR}.pbr to\n\
C:\\ of Windows after booting Windows.\n\
boot.ini and ${LINUX_PBR}.pbr are prepared in ${T_PX}boot/ \n\
(/boot/ of ${PARTITION_LINUX}).\n\
\n\
After rebuilding the Linux kernel, you must reinstall LILO and \n\
recreate ${LINUX_PBR}.pbr and copy it to C:\\ of Windows. \n\
You can do them using this tool, 'osldrconfig' command, again. \
" 13 70

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

