#!/bin/sh
# vliloquiet
# This is a rewrite of vliloconf By Eko M. Budi, 2005, which was
#  originally based off liloconf from Slackware 9.1 by Patrick V.
# This version asks NO questions and requires at least 3 arguments
# 1 = lilo target: MBR, SECTOR, OR FLOPPY
# 2 = the target mbr or partition: /dev/sda
# 3 = consoletype: standard, bootsplash-high, bootsplash-med, or bootsplash-low
# 4 = Optional lilo append: noapic, etc
# 
# By Uel Archuletta, 2009
# License: GNU GPL
#

BADARGS=65

if [ ! -n "$4" ]
then
 echo "# This version asks NO questions and requires at least 4 arguments"
 echo "# 1 = lilo target: MBR, SECTOR, OR FLOPPY"
 echo "# 2 = the target mbr or partition: /dev/sda"
 echo "# 3 = consoletype: standard, bootsplash-high, bootsplash-med, or bootsplash-low"
 echo "# 4 = Install LILO: YES, NO"
 echo "# 5 = Optional lilo append: noapic, etc"
 echo " "
 echo "Usage: `basename $0` MBR /dev/hda bootsplash-high NO noapic"
 exit $BADARGS
fi  


vdir=$(dirname $0)
. $vdir/vasm-functions

check_root
# VARIABLES
TMP=/var/log/setup/tmp
mkdir -p $TMP
BOOT_DIR="/boot"
FRIEND_DIR="$BOOT_DIR/tamu"
BITMAP_DIR="$BOOT_DIR/bitmaps"
BITMAP_MENU="$BOOT_DIR/bitmap/boot.bmp"
LILO_CONF="/etc/lilo.conf"
CONSOLETYPE="standard"
SPLASH=""

# Determine the root partition (such as /dev/hda2)
if [ -z "$ROOT_DEVICE" ]; then
  ROOT_DEVICE=$(cat /etc/fstab | awk '{if ($2 == "/") {print $1}}') 
  # This must be successful
  if [ -z "$ROOT_DEVICE" ]; then
    ROOT_DEVICE=$(mount | awk '{if ($3 == "/") {print $1}}')
  fi
fi

# Figure out where the kernel is:
mkdir -p $BOOT_DIR
if [ -r $BOOT_DIR/vmlinuz ]; then
  KERNEL=$BOOT_DIR/vmlinuz
elif [ -r $ROOT_DIR/vmlinuz ]; then
  KERNEL=$ROOT_DIR/vmlinuz
else
echo "Cannot find kernel (vmlinuz). Aborting LILO install"
  clean_exit 1
fi

# Get old append
APPEND=""
if [ -r $LILO_CONF ]; then
    LINE=`grep -m 1 -E "^[^#].*append *=" $LILO_CONF`
    if [ "$LINE" ]; then
	APPEND=`echo $LINE | cut -f2 -d '"'`
    fi
fi 

append_ide_scsi()
{
    while read LINE; do
	echo $LINE
        DEV=`echo $LINE|cut -f1 -d'|'`
	DEV=${DEV##*/}
	if grep "$DEV=ide-scsi" /proc/cmdline; then
	    APPEND="$APPEND $DEV=ide-scsi"
	elif dmesg | egrep "^${DEV}:" | grep -q "RW"; then
	    APPEND="$APPEND $DEV=ide-scsi"
	fi
    done
}

# do we need ide-scsi ?
if ! echo $APPEND | grep -q "ide-scsi"; then
    if [ `kernelversion` == 2.4 ] && \
	probedisk | grep -qE '(cdrom|cdwriter)' > $freply; then
	append_ide_scsi < $freply
    fi
fi

APPEND=`echo $APPEND`

installcolor()
{
 echo "Installing LILO ..." 
 if [ "$ROOT_DIR" ]; then
  lilo -r $T_PX -m /boot/map -C /etc/lilo.conf 
 else
  lilo 
 fi
 SUCCESS=$?

 if [ ! "$SUCCESS" = "0" ]; then # edit file to try lba32 mode:
  cat $T_PX/etc/lilo.conf | while read line ; do
   echo $line
   if [ "$line" = "# Start LILO global section" ] ; then
     echo "lba32 # Allow booting past 1024th cylinder with a recent BIOS"
   fi
  done > $T_PX/etc/lilo.conf.lba32
  mv $T_PX/etc/lilo.conf.lba32 $T_PX/etc/lilo.conf
  if [ "$ROOT_DIR" ]; then
   lilo -r $T_PX -m /boot/map -C /etc/lilo.conf 1> /dev/null 2> /dev/null
  else
   lilo 1> /dev/null 2> $tmp/liloerror
  fi
  SUCCESS=$?
 fi

 if [ ! "$SUCCESS" = "0" ]; then # some LILO error occurred
   liloerror=`cat $tmp/liloerror | grep Fatal`
echo "Sorry, but the attempt to install LILO has returned an error."
echo "$liloerror"
  clean_exit 1
 fi
}

menu_boot()
{
# $1 must be one of these: MBR, SECTOR, or FLOPPY
# if MBR $2 must be the HD you want ot install to "/dev/sda"
# if SECTOR $2 must be the secotr you want ot install to "/dev/sda1"
#reply="$1"
case "$1" in
  ##if the user chose MBR check to see if more than 1 mbr is available if only 1 use it 
  MBR)
   LILO_TARGET="$2"
  ;;
 SECTOR)
  LILO_TARGET="$2"
  ;;
 FLOPPY)
  LILO_TARGET="/dev/fd0"
  ;;
 *)
  clean_exit
  ;;
esac 
return 0
}

# Menu to check if we want to use VESA framebuffer support:
menu_fb() {
#CONSOLETYPE=$3  
  CONSOLETYPE="$3"
 if [ "$CONSOLETYPE" = "bootsplash-high" ]; then
    CONSOLENUM=791
    SPLASH="splash=silent"
    RAMDISK="/boot/initrd"
  elif [ "$CONSOLETYPE" = "bootsplash-med" ]; then
    CONSOLENUM=788
    SPLASH="splash=silent"
    RAMDISK="/boot/initrd"        
  elif [ "$CONSOLETYPE" = "bootsplash-low" ]; then
    CONSOLENUM=785
    SPLASH="splash=silent"
    RAMDISK="/boot/initrd"        
  fi
  return 0
}

menu_param()
{
#APPEND=$5
#if [ ! "$5"x = "x" ];then
     APPEND="$5 $APPEND"
    return 0
}

menu_os()
{
###########################
## Build array of
#LILO_TYPE
#LILO_PART
#LILO_LABEL
#LILO_DESC
#LILO_TABLE

echo "Detecting operating systems ..."

count1=0
## Bootable windows partition first
PARTS=`fdisk -l 2>/dev/null | grep -E '^/dev/.* \* .*(FAT16)' | cut -f 1 -d ' '`
index=""
for PART in $PARTS; do
  let count1=count1+1
  LILO1_PART[$count1]="$PART"
  LILO1_LABEL[$count1]="dos$index"
  LILO1_DESC[$count1]="DOS ($PART)"
  if [ "$index" ]; then
    let index=$index+1
  else
    index=1
  fi
done

PARTS=`fdisk -l 2>/dev/null | grep -E '^/dev/.* \* .*(FAT32|HPFS|NTFS|Win)' | cut -f 1 -d ' '`
index=""
for PART in $PARTS; do
  let count1=count1+1
  LILO1_PART[$count1]="$PART"
  LILO1_LABEL[$count1]="Win$index"
  LILO1_DESC[$count1]="Windows ($PART)"
  if [ "$index" ]; then
    let index=$index+1
  else
    index=1
  fi
done

## Add current root
LILO_LABEL="linux"
LILO_DESCRIPTION="Linux"
if [ -f /boot/lilo.label ]; then
    . /boot/lilo.label
fi
DEFAULT_LABEL=$LILO_LABEL

count2=1
LILO2_PART[$count2]="$ROOT_DEVICE"
LILO2_LABEL[$count2]="$DEFAULT_LABEL"
LILO2_DESC[$count2]="$LILO_DESCRIPTION ($ROOT_DEVICE)"

## Add current root
let count2=count2+1
LILO2_PART[$count2]="$ROOT_DEVICE"
LILO2_LABEL[$count2]="$DEFAULT_LABEL-tui"
LILO2_DESC[$count2]="$LILO_DESCRIPTION Text mode ($ROOT_DEVICE)"

## Add current root
#let count2=count2+1
#LILO2_PART[$count2]="$ROOT_DEVICE"
#LILO2_LABEL[$count2]="$DEFAULT_LABEL-gui"
#LILO2_DESC[$count2]="$LILO_DESCRIPTION Graphic mode ($ROOT_DEVICE)"

## Add other vmlinuzes
if [ -h /boot/vmlinuz ]; then
    MAIN_VMLINUZ=`readlink /boot/vmlinuz`
    MAIN_VMLINUZ=`basename $MAIN_VMLINUZ`
else
    MAIN_VMLINUZ=vmlinuz
fi

for VMLINUZ in /boot/vmlinuz-*; do
    BVMLINUZ=`basename $VMLINUZ`
    if [ "$BVMLINUZ" = "$MAIN_VMLINUZ" ]; then
	continue
    fi
    BVMLINUZ=${BVMLINUZ#*-}
    # skip if more than 12 chars
    if [ ${#BVMLINUZ} -gt 12 ]; then
	continue
    fi
    let count2=count2+1
    LILO2_PART[$count2]="$ROOT_DEVICE"
    LILO2_DESC[$count2]="Linux $BVMLINUZ ($ROOT_DEVICE)"
    LILO2_LABEL[$count2]="$BVMLINUZ"
done

## Add additional Linux
count3=0
mkdir -p /tmp/lilo_mount &> /dev/null
for PART in `fdisk -l 2>/dev/null | grep -E '83 *Linux' | cut -f 1 -d ' '`; do
  if [ "$PART" != "$ROOT_DEVICE" ]; then
     if mount $PART /tmp/lilo_mount &> /dev/null; then
	if [ -r /tmp/lilo_mount/boot/vmlinuz ]; then
	    # Find out the distro
	    # Check for GNU/Linux distributions
	    if [ -f /tmp/lilo_mount/etc/vector-version ]; then
	      LILO_LABEL="vector"
	      LILO_DESCRIPTION="Vector Linux"
	    elif [ -f /tmp/lilo_mount/etc/sysconfig/vector ]; then
	      LILO_LABEL="vector"
	      LILO_DESCRIPTION="Vector Linux"
	    elif [ -f /tmp/lilo_mount/etc/slackware-version ]; then
	      LILO_LABEL="slackware"
	      LILO_DESCRIPTION="Slackware Linux"
	    elif [ -f /tmp/lilo_mount/etc/SuSE-release -o -f /etc/UnitedLinux-release ]; then
	      LILO_LABEL="suse"
	      LILO_DESCRIPTION="Suse Linux"
	    elif [ -f /tmp/lilo_mount/etc/gentoo-release ]; then
	      LILO_LABEL="gentoo"
	      LILO_DESCRIPTION="Gentoo Linux"
	    elif [ -f /tmp/lilo_mount/etc/debian_version ]; then
	      LILO_LABEL="debian"
	      LILO_DESCRIPTION="Debian Linux"
	    elif [ -f /tmp/lilo_mount/etc/mandrake-release ]; then
	      LILO_LABEL="mandrake"
	      LILO_DESCRIPTION="Mandrake Linux"
	    elif [ -f /tmp/lilo_mount/etc/redhat-release ]; then
	      LILO_LABEL="redhat"
	      LILO_DESCRIPTION="Redhat Linux"
	    elif [ -f /tmp/lilo_mount/etc/fedora-release ]; then
	      LILO_LABEL="fedora"
	      LILO_DESCRIPTION="Fedora Linux"
	    else
	      LILO_LABEL="linux"
	      LILO_DESCRIPTION="Linux"
	    fi
	    # if there is a label, use it
	    if [ -f /tmp/lilo_mount/boot/lilo.label ]; then
		. /tmp/lilo_mount/boot/lilo.label
	    fi
	    let count3=count3+1
	    num="`echo $PART | cut -b6-`"
	    LILO3_PART[$count3]="$PART"
	    LILO3_LABEL[$count3]="$LILO_LABEL-$num"
	    LILO3_DESC[$count3]="$LILO_DESCRIPTION ($PART)"
	fi 
	umount /tmp/lilo_mount     
     fi
  fi  
done

for (( ii=1; ii <= $count1 ; ii=ii+1 )); do
  echo "'${LILO1_LABEL[$ii]}' '${LILO1_DESC[$ii]}' on \\" >> $fmenu
done
for (( ii=1; ii <= $count2 ; ii=ii+1 )); do
  echo "'${LILO2_LABEL[$ii]}' '${LILO2_DESC[$ii]}' on \\" >> $fmenu
done
for (( ii=1; ii <= $count3 ; ii=ii+1 )); do
  echo "'${LILO3_LABEL[$ii]}' '${LILO3_DESC[$ii]}' on \\" >> $fmenu
done
#$fmenu


#if [ "$CMD" = "" ]; then
#  LILO_CHOICES=`cat $freply | sed -e 's/"/ /g' -e 's/  / /g'`
#else
  LILO_CHOICES=`cat $fmenu | sed -e 's!/! !g' -e 's/  / /g'`
#fi
return 0
}

############################################################
# Here we go 
menu_finish()
{
# Make a new boot_message if not exist
cat << EOF > $T_PX/etc/lilo.conf
# LILO configuration file
# generated by 'vliloquiet'
#
## Start LILO global section ##
EOF
if [ "$APPEND" ]; then
  echo "append=\"$APPEND\"" >> $T_PX/etc/lilo.conf
fi
 cat << EOF >> $T_PX/etc/lilo.conf
boot = $LILO_TARGET
default = $DEFAULT_LABEL
prompt
compact
timeout = 300
# Override dangerous defaults that rewrite the partition table:
change-rules
  reset
EOF

# USE bitmap if exist, otherwise text mode
if [ -r $T_PX/boot/bitmap/boot.bmp ]; then
  echo "bitmap = /boot/bitmap/boot.bmp" >> $T_PX/etc/lilo.conf
else
  ## make boot message
  if [ ! -f $T_PX/boot/boot_message.txt ]; then
    cat << EOF > $T_PX/boot/boot_message.txt
Please select the partition you would like to boot then press [enter].
If you want to boot another Linux partition, you may type
boot: linux root=<partition-name>

EOF
  fi

  cat << EOF >> $T_PX/etc/lilo.conf
menu-scheme = wb:bw:wb:bw
menu-title = "Welcome to LILO boot menu"
message = /boot/boot_message.txt
EOF
fi

 if [ "$CONSOLETYPE" = "standard" ]; then
   cat << EOF >> $T_PX/etc/lilo.conf
# Normal VGA console
vga = normal
EOF
else
   cat << EOF >> $T_PX/etc/lilo.conf
# VESA framebuffer console @ $CONSOLETYPE
vga = $CONSOLENUM
# Normal VGA console
# vga = normal
EOF
fi

   cat << EOF >> $T_PX/etc/lilo.conf

## End LILO global section ##
EOF

# Detect initrd
ROOT_INITRD=""
if [ -f /boot/initrd.gz ]; then
    ROOT_INITRD="initrd = /boot/initrd.gz"
    fi
if [ -f /boot/initrd ]; then
if [ ! "$SPLASH" = "" ]; then 
    ROOT_INITRD="initrd = /boot/initrd"
    fi    
    fi
mkdir -p $FRIEND_DIR

echo "Making lilo.conf ..."
number=0
for (( ii=1; ii <= $count1 ; ii=ii+1 )); do
  # Skip if not chosen
  if ! echo $LILO_CHOICES | grep -qw ${LILO1_LABEL[$ii]}; then
    continue
  fi
  let number=$number+1 

  TABLE="`echo ${LILO1_PART[$ii]} | cut -b1-8`"
cat << EOF >> $T_PX/etc/lilo.conf
# Partition $number : DOS/Windows 
other = ${LILO1_PART[$ii]}
    label = ${LILO1_LABEL[$ii]}
    table = $TABLE
EOF

done

for (( ii=1; ii <= $count2 ; ii=ii+1 )); do
  # Skip if not chosen
  if ! echo $LILO_CHOICES | grep -qw ${LILO2_LABEL[$ii]}; then
    continue
  fi
  let number=$number+1 

  case ${LILO2_LABEL[$ii]} in
     "$DEFAULT_LABEL")
  cat << EOF >> $T_PX/etc/lilo.conf
# Partition $number: Linux GUI mode
image = $KERNEL
    root = $ROOT_DEVICE
    label = $DEFAULT_LABEL
    append = "$SPLASH $APPEND"
    read-only
    $ROOT_INITRD
EOF
    ;;

     "${DEFAULT_LABEL}-tui")
  cat << EOF >> $T_PX/etc/lilo.conf
# Partition $number: Linux TUI mode
image = $KERNEL
    root = $ROOT_DEVICE
    label = ${DEFAULT_LABEL}-tui
    append = "2 $SPLASH $APPEND"
    read-only
    $ROOT_INITRD
EOF
    ;;

     "${DEFAULT_LABEL}-gui")
  cat << EOF >> $T_PX/etc/lilo.conf
# Partition $number: Linux GUI mode
image = $KERNEL
    root = $ROOT_DEVICE
    label = ${DEFAULT_LABEL}-gui
    append = "4 $SPLASH $APPEND"
    read-only
    $ROOT_INITRD
EOF
    ;;

    *)

    if [ -f /boot/vmlinuz-${LILO2_LABEL[$ii]} ]; then
	cat << EOF >> $T_PX/etc/lilo.conf
# Partition $number: Linux
image = /boot/vmlinuz-${LILO2_LABEL[$ii]}
    root = $ROOT_DEVICE
    label = ${LILO2_LABEL[$ii]}
    append = "$SPLASH $APPEND"
    read-only
EOF
    else
	echo "Cannot install ${LILO2_LABEL[$ii]}. Skipped." "WARNING"
	sleep 3  
    fi
    ;;
esac
done

for (( ii=1; ii <= $count3 ; ii=ii+1 )); do
    # Skip if not chosen
    if ! echo $LILO_CHOICES | grep -qw ${LILO3_LABEL[$ii]}; then
	continue
    fi
    let number=$number+1 
    INITRD=""
    if mount ${LILO3_PART[$ii]} /tmp/lilo_mount; then
	if cp -u /tmp/lilo_mount/boot/vmlinuz $FRIEND_DIR/vmlinuz-${LILO3_LABEL[$ii]}; then
	    INITRD=`ls -1 /tmp/lilo_mount/boot/initrd.* 2>/dev/null | head -n1`
	    if [ "$INITRD" ]; then
		cp -u $INITRD $FRIEND_DIR/initrd-${LILO3_LABEL[$ii]}
		INITRD="initrd = $FRIEND_DIR/initrd-${LILO3_LABEL[$ii]}"
	    fi
	    cat << EOF >> $T_PX/etc/lilo.conf
# Partition $number: Linux
image = $FRIEND_DIR/vmlinuz-${LILO3_LABEL[$ii]}
    root = ${LILO3_PART[$ii]}
    label = ${LILO3_LABEL[$ii]}
    read-only
    $INITRD
EOF
	else
	    echo "Cannot install ${LILO_LABEL[$ii]}. Skipped." "WARNING"
	    sleep 3
	fi
	umount /tmp/lilo_mount
    fi
done
rmdir /tmp/lilo_mount

# Done, now we must install lilo:
if [ "$4" = "YES" ];then
  installcolor;
  echo "SUCCESS LILO has been installed."
else
  echo "/etc/lilo.conf has been written."
fi
sleep 4
clean_exit 0
}


############################
# MAIN MENU
menu_boot $1 $2 $3 $4 $5
menu_os $1 $2 $3 $4 $5
menu_fb $1 $2 $3 $4 $5
menu_param $1 $2 $3 $4 $5
menu_finish $1 $2 $3 $4 $5

