#!/bin/sh
# 'probe()' borrowed from LILO QuickInst.
probe()
{
 [ ! -z "`dd if=$1 bs=1 count=1 2>/dev/null | tr '\0' x`" ]
 return
}
if [ "$T_PX" = "" -a ! "$1" = "" ]; then
  T_PX=$1
fi
if [ "$T_PX" = "" ]; then
  T_PX=/
fi
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
ROOT_DEVICE=$2
if [ "$ROOT_DEVICE" = "" ]; then
  ROOT_DEVICE="`mount | cut -b-10 | sed -n "1 p"`"
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
  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
