#!/bin/sh
# 'probe()' borrowed from LILO QuickInst.
probe()
{
 [ ! -z "`dd if=$1 bs=1 count=1 2>/dev/null | tr '\0' x`" ]
 return
}
if [ -r /tmp/SeTcolor -o "$COLOR" = "on" ]; then # use color menus, otherwise tty
 if [ -r /tmp/SeTT_PX ]; then
  T_PX="`cat /tmp/SeTT_PX`"
 else
  if [ "$T_PX" = "" -a ! "$1" = "" ]; then
    T_PX=$1
  fi
  if [ "$T_PX" = "" ]; then
   T_PX=/
  fi
 fi
 while [ 0 ]; do
  dialog --title "LILO INSTALLATION" --menu "LILO (the Linux Loader) is the \
program that allows booting Linux directly from the hard drive. If you're \
not familiar with LILO, you might want to read the help file before you \
install it. Which option would you like?" 15 70 5 \
"1" "Use OS/2 Boot Manager" \
"2" "Install LILO to Master Boot Record" \
"3" "Skip LILO installation" \
"4" "Install LILO to a formatted floppy disk" \
"H" "Read the Linux Loader HELP file"  2> /tmp/reply
  if [ $? = 1 -o $? = 255 ]; then
   REPLY="3"
  else
   REPLY="`cat /tmp/reply`"
  fi
  rm -r /tmp/reply
  if [ ! "$REPLY" = "H" ]; then
   break
  fi
  dialog --title "LILO INSTALLATION HELP" --textbox \
  "$T_PX/usr/adm/setup/text.lilohelp" 22 73
 done
 ROOT_DEVICE=$2
 if [ "$ROOT_DEVICE" = "" ]; then
  if [ -r /tmp/SeTrootdev ]; then
   ROOT_DEVICE="`cat /tmp/SeTrootdev`"
  else
   ROOT_DEVICE="`mount | cut -b-10 | sed -n "1 p"`"
  fi
 fi
 if [ -r $T_PX/etc/lilo.conf ]; then
  cp $T_PX/etc/lilo.conf $T_PX/tmp/lilo.conf.YourLastVersionBeforeSetup
 fi 
 LILO_TARGET=`echo $ROOT_DEVICE | cut -b 1-8` # typically this will be your boot sector of the install drive
 if [ "$REPLY" = "1" ]; then
  LILO_TARGET=`echo $ROOT_DEVICE` # Here, we use the partition superblock for OS/2 BM
 elif [ "$REPLY" = "2" -o "$REPLY" = "3" ]; then # we must probe for the device.
  if [ -L $T_PX/dev/hda ]; then
   LILO_TARGET="/dev/sda"
  elif probe /dev/hda; then 
   LILO_TARGET="/dev/hda"
  elif probe /dev/sda; then
   LILO_TARGET="/dev/sda"
  else
   dialog --msgbox "LILO can't find your first hard drive! Sorry, but LILO is \
getting /dev/null'ed..." 10 50
   LILO_TARGET="/dev/null"
  fi
 fi
 if [ "$REPLY" = "4" ]; then
  dialog --menu "Please put a formatted floppy disk into your boot drive and \
press ENTER to install LILO to floppy." 10 50 1 \
 "ADD LILO" "Install LILO to the floppy disk" \
 "SKIP" "Skip LILO installation and go on" 2> /tmp/lilopick
  if [ $? = 1 ]; then
   OKF="SKIP"
  else
   OKF="`cat /tmp/lilopick`"
  fi
  rm -f /tmp/lilopick
  if [ "$OKF" = "SKIP" ]; then
   REPLY="3"
  else
   LILO_TARGET="/dev/fd0"
  fi
 fi
 if [ -r $T_PX/vmlinuz ]; then
  KERNEL='/vmlinuz'
 else
  KERNEL='/zImage'
 fi
 cat > $T_PX/etc/lilo.conf << EOF
boot = $LILO_TARGET
#compact	# faster, but won't work on all systems.
#delay = 5	# optional, for systems that boot very quickly
vga = normal	# force sane state
ramdisk = 0	# paranoia setting
  root = $ROOT_DEVICE
  image = $KERNEL
  label = linux
EOF
 dd if=$LILO_TARGET of=$T_PX/boot/your_original_boot_sector bs=512 count=1 1> /dev/null 2> /dev/null
 if [ "$REPLY" = "1" -o "$REPLY" = "2" -o "$REPLY" = "4" ]; then
  dialog --infobox "\nInstalling the Linux Loader..." 5 40
  (cd $T_PX/sbin; ./lilo -r $T_PX -m /boot/map -C /etc/lilo.conf 1> /dev/null 2> /dev/null)
 else
  dialog --msgbox "Skipping LILO installation, but putting the \
configuration file that would be used to boot from your master \
boot record into /etc/lilo.conf.MBR." 9 60
  cp $T_PX/etc/lilo.conf $T_PX/etc/lilo.conf.MBR
 fi
 if [ "$REPLY" = "2" -a -r $T_PX/etc/lilo.conf.YourLastVersionBeforeSetup ]; then
  cp $T_PX/etc/lilo.conf.YourLastVersionBeforeSetup $T_PX/etc/lilo.conf
 fi
else # nocolor!
 cat << EOF

LILO (Linux Loader) Installation:

LILO, the Linux Loader, allows you to boot Linux directly off your hard drive 
without using a boot floppy disk. 

1. If you are using OS/2's Boot Manager, this choice will allow you to boot 
   Linux from the Boot Manager menu. If you have already added the Linux 
   partition to the Boot Manager menu, this choice will complete the Boot 
   Manager installation process.

2. If you are planning to run Linux as the only operating system on your 
   machine, use this option to boot directly from the boot sector of your 
   drive. By the way, LILO may be removed from your boot sector using MS-DOS
   fdisk with the command: fdisk /mbr

3. If you're not sure, select 3 to skip LILO and use a boot floppy instead.
   You can read more about how to configure LILO manually in /usr/doc/lilo.

4. Install LILO to a formatted floppy. This is a safe choice, and will boot
   considerably faster than a normal boot disk. 

EOF
 echo -n "Which option would you like? (1/2/3/4): "
 read REPLY;
 echo
 T_PX=$1
 ROOT_DEVICE=$2
 if [ -r $T_PX/etc/lilo.conf ]; then
  cp $T_PX/etc/lilo.conf $T_PX/tmp/lilo.conf.YourLastVersionBeforeSetup
 fi 
 LILO_TARGET=`echo $ROOT_DEVICE | cut -b 1-8` # typically this will be your boot sector of the install drive
 if [ "$REPLY" = "1" ]; then
  LILO_TARGET=`echo $ROOT_DEVICE` # Here, we use the partition superblock for OS/2 BM
 elif [ "$REPLY" = "2" -o "$REPLY" = "3" ]; then # we must probe for the device.
  if [ -L $T_PX/dev/hda ]; then
   LILO_TARGET="/dev/sda"
  elif probe /dev/hda; then 
   LILO_TARGET="/dev/hda"
  elif probe /dev/sda; then
   LILO_TARGET="/dev/sda"
  else
   cat << EOF

LILO can't find your first hard drive! Sorry, but LILO is
getting /dev/null'ed...
EOF
   LILO_TARGET="/dev/null"
  fi
 fi
 if [ "$REPLY" = "4" ]; then
  echo "Please put a formatted floppy disk into your boot drive and press [enter],"
  echo -n "to install LILO to floppy, or [s] to skip."
  read OKF;
  echo
  if [ "$OKF" = "s" ]; then
   REPLY="3"
  else
   LILO_TARGET="/dev/fd0"
  fi
 fi
 if [ -r $T_PX/vmlinuz ]; then
  KERNEL='/vmlinuz'
 else
  KERNEL='/zImage'
 fi
 cat > $T_PX/etc/lilo.conf << EOF
boot = $LILO_TARGET
#compact	# faster, but won't work on all systems.
#delay = 5	# optional, for systems that boot very quickly
vga = normal	# force sane state
ramdisk = 0	# paranoia setting
  root = $ROOT_DEVICE
  image = $KERNEL
  label = linux
EOF
 dd if=$LILO_TARGET of=$T_PX/boot/your_original_boot_sector bs=512 count=1 1> /dev/null 2> /dev/null
 if [ "$REPLY" = "1" -o "$REPLY" = "2" -o "$REPLY" = "4" ]; then
  echo "Installing the Linux Loader..."
  (cd $T_PX/sbin; ./lilo -r $T_PX -m /boot/map -C /etc/lilo.conf)
  echo 
 else
  cat << EOF
Skipping LILO installation, but putting the configuration file
that would be used to boot from your master boot record into
/etc/lilo.conf.MBR.
EOF
  echo
  cp $T_PX/etc/lilo.conf $T_PX/etc/lilo.conf.MBR
 fi
 if [ "$REPLY" = "2" -a -r $T_PX/etc/lilo.conf.YourLastVersionBeforeSetup ]; then
  cp $T_PX/etc/lilo.conf.YourLastVersionBeforeSetup $T_PX/etc/lilo.conf
 fi
fi
