#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: output the pxelinux default

# Load DRBL setting and functions
if [ ! -f "/opt/drbl/sbin/drbl-conf-functions" ]; then
  echo "Unable to find /opt/drbl/sbin/drbl-conf-functions! Program terminated!" 
  exit 1
fi
. /opt/drbl/sbin/drbl-conf-functions

export LC_ALL=C
#
usage() {
  echo "Generate the PXELINUX menu for DRBL clients."
  echo "Usage: $(basename $0) [OPTION]"
  echo "Options:"
  echo "-v, --verbose:  verbose mode."
  echo "-s, --serial OPT:  set the PXE menu to work with serial console output."
  echo "-c, --console OPT: set the console output parameters."
  echo "Ex:"
  echo "$(basename $0) -v -s \"serial 0 38400\" -c \"console=ttyS0,38400n81 console=tty0\" "
  echo "Note! You must put the \" before and after your serial OPT and console OPT."
}

#
check_if_root

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -s|--serial)
            shift; PXE_SERIAL_OUTPUT="$1"
            shift;;
    -c|--console)
            shift; CONSOLE_OUTPUT="$1"
            shift;;
    -f|--fdos)
            shift; fdos_img_output="$1"
            shift;;
    -v|--verbose)
            shift; verbose="on"
            ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            usage >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

# fdos_img_output should be in /opt/drbl/conf/drbl.conf
[ -z "$fdos_img_output" ] && echo "You must provide the FreeDOS image filename! Program terminated!!!" && exit 1

echo "Generating default pxelinux config ($PXELINUX_DIR/default)..."
# clean the old one
[ -f "$PXELINUX_DIR/default" ] && rm -f $PXELINUX_DIR/default

# get the OS_Version and OS_type
check_distribution_name

#
echo "Adding menus for DRBL, local boot, memtest86+, FreeDOS..."
cat <<-PXE_END > $PXELINUX_DIR/default
# Created by generate-pxe-menu! Do NOT edit unless you know what you are doing! 
# Keep those comment "MENU DEFAULT" and "MENU HIDE"! Do NOT remove them.
# Note!!! If "serial" directive exists, it must be the first directive
$PXE_SERIAL_OUTPUT
default vesamenu.c32
timeout 70
prompt 0
noescape 1
MENU MARGIN 5
MENU BACKGROUND drbl-pxe.png
# Set the color for unselected menu item and timout message
MENU COLOR UNSEL 7;32;41 #c0000090 #00000000
MENU COLOR TIMEOUT_MSG 7;32;41 #c0000090 #00000000
MENU COLOR TIMEOUT 7;32;41 #c0000090 #00000000

# MENU MASTER PASSWD

say **********************************************
say Welcome to DRBL.
say NCHC Free Software Labs, Taiwan.
say http://drbl.nchc.org.tw; http://drbl.sf.net
say **********************************************

# Do NOT allow client to edit the parameters
ALLOWOPTIONS 0

# simple menu title
MENU TITLE DRBL (http://drbl.nchc.org.tw, http://drbl.sf.net)

label drbl
  MENU DEFAULT
  # MENU HIDE
  MENU LABEL $FULL_OS_Version $powerful_client_menu_label
  # MENU PASSWD
  kernel vmlinuz-pxe
  append initrd=initrd-pxe.img ramdisk_blocksize=1024 ramdisk_size=$PXE_RAMDISK_SIZE $CONSOLE_OUTPUT devfs=nomount drblthincli=off selinux=0

label local
  # MENU DEFAULT
  # MENU HIDE
  MENU LABEL $local_os_menu_label
  # MENU PASSWD
  # 2 method to boot local device:
  # (1) For localboot 0, it is decided by boot order in BIOS, so uncomment the follow 1 line if you want this method:
  # localboot 0

  # (2) For chain.c32, you can assign the boot device.
  # Ref: extlinux.doc from syslinux
  # Syntax: APPEND [hd|fd]<number> [<partition>]
  # [<partition>] is optional.
  # Ex:
  # Second partition (2) on the first hard disk (hd0);
  # Linux would *typically* call this /dev/hda2 or /dev/sda2, then it's "APPEND hd0 2"
  #
  kernel chain.c32
  append hd0

# Note! *.bin is specially purpose for syslinux, 
# Do NOT use memtest86.bin, use memtest86 instead of memtest86.bin
label memtest
  # MENU DEFAULT
  # MENU HIDE
  MENU LABEL Memory test using Memtest86+
  # MENU PASSWD
  kernel memtest86

# ref: http://syslinux.zytor.com/memdisk.php
label fdos
  # MENU DEFAULT
  MENU HIDE
  MENU LABEL FreeDOS
  # MENU PASSWD
  kernel memdisk
  append initrd=$fdos_img_output

label drbl-terminal
  # MENU DEFAULT
  MENU HIDE
  MENU LABEL $FULL_OS_Version $thin_client_menu_label
  # MENU PASSWD
  kernel vmlinuz-pxe
  append initrd=initrd-pxe.img ramdisk_size=$PXE_RAMDISK_SIZE $CONSOLE_OUTPUT devfs=nomount drblthincli=on

PXE_END

# output the netinstall menu
# awk print $1 is necessary for dpkg.
netinstall_list="$($query_allpkgs_cmd |grep -E "^netinstall" | awk -F" " '{print $1}' | sort)"

for inet in $netinstall_list; do
   # netinstall package name: netinstall-dist-ver-arch, like: netinstall-CentOS-4-i386, and its content is like:
   # /tftpboot/nbi_img/initrd-netinstall-CentOS-4-i386.img
   # /tftpboot/nbi_img/vmlinuz-netinstall-CentOS-4-i386

   # For debina based, back convert x86-64 to x86_64, since alien convert x86_64 to x86-64.
   inet_="$(echo $inet | sed -e "s/x86-64/x86_64/g")"
   img_menu="$(echo "$inet_" | awk -F"-" '{print $1"-"$2"-"$3"-"$4}')"
   img_full="$($query_pkglist_cmd $inet | grep -E "initrd-netinstall-.*img")"
   img="$(basename $img_full)"
   kernel_full="$($query_pkglist_cmd $inet | grep -E "vmlinuz-netinstall-.*")"
   kernel="$(basename $kernel_full)"

   extra_append=""
   if [ -n "$(echo $inet | grep -i "\-debian-sarge" 2>/dev/null)" ]; then
      # For Debian Sarge, we need extra options in append
      # refer to http://opensource.nchc.org.tw/debian/dists/sarge/main/installer-i386/current/images/netboot/pxelinux.cfg/default
      NETINSTALL_RAMDISK_SIZE="$NETINSTALL_RAMDISK_SIZE_DEF"
      extra_append="root=/dev/rd/0 devfs=mount,dall rw  --"
   elif [ -n "$(echo $inet | grep -i "\-debian-woody" 2>/dev/null)" ]; then
      # For Debian Woody, we use extra options bf24
      # refer to http://www.debianplanet.com/node.php?id=818&cid=13384
      NETINSTALL_RAMDISK_SIZE="$NETINSTALL_RAMDISK_SIZE_DEF"
      extra_append="flavor=bf2.4"
   elif [ -n "$(echo $inet | grep -iE "(\-redhat-|\-fedora-)" 2>/dev/null)" ]; then
      # For RedHat/Fedora, enable reiserfs when installing
      NETINSTALL_RAMDISK_SIZE="$NETINSTALL_RAMDISK_SIZE_DEF"
      extra_append="reiserfs"
   elif [ -n "$(echo $inet | grep -iE "(\-mandrake-|\-mandriva-)" 2>/dev/null)" ]; then
      # For Mandrake/Mandriva
      # use larger ramdisk size for Mandrake, overwrite the default one.
      NETINSTALL_RAMDISK_SIZE=128000
      extra_append="acpi=ht vga=788 splash=silent label vgalo"
   elif [ -n "$(echo $inet | grep -i "\-ubuntu-" 2>/dev/null)" ]; then
      # For Ubuntu
      # ref to ftp://linux.nchc.org.tw/distributions/ubuntu/dists/hoary/main/installer-i386/current/images/netboot/ubuntu-installer/i386/pxelinux.cfg/default
      NETINSTALL_RAMDISK_SIZE="$NETINSTALL_RAMDISK_SIZE_DEF"
      extra_append="root=/dev/rd/0 rw  --"
   elif [ -n "$(echo $inet | grep -i "\-suse-" 2>/dev/null)" ]; then
      # For SuSE
      # For extra_append, refer to something like 
      # http://free.nchc.org.tw/SuSE/SuSE/i386/9.3/boot/loader/isolinux.cfg
      # use larger ramdisk size, overwrite the default one.
      NETINSTALL_RAMDISK_SIZE=128000
      extra_append="splash=silent showopts"
   elif [ -n "$(echo $inet | grep -i "\-opensuse-" 2>/dev/null)" ]; then
      # For OpenSuSE
      # For extra_append, refer to something like 
      # http://ftp.cs.pu.edu.tw/Linux/OpenSuse/distribution/SL-10.0-OSS/inst-source/boot/loader/isolinux.cfg
      # use larger ramdisk size, overwrite the default one.
      NETINSTALL_RAMDISK_SIZE=128000
      extra_append="splash=silent showopts"
   else
      NETINSTALL_RAMDISK_SIZE="$NETINSTALL_RAMDISK_SIZE_DEF"
      extra_append=""
   fi

   # skip if we can not find the menu, img or kernel.
   [ -z "$img_menu" ] && continue
   [ -z "$img" -o -z "$kernel" ] && continue

   echo "Adding $img_menu menu..."
   [ -n "$verbose" ] && echo "extra option in append: $extra_append"    
   # Generate the description:
   # EX for i: rh-8.0-netinstall
   # create the description for the netinstall image
   des="$(echo $img_menu | sed -e "s/^netinstall-//g" | sed -e "s/-/ /g")"
   des="${des} installation via network"
   cat <<-PXE_END >> $PXELINUX_DIR/default
label $img_menu
  # MENU DEFAULT
  MENU HIDE
  MENU LABEL $des
  # MENU PASSWD
  kernel $kernel
  append initrd=$img ramdisk_size=$NETINSTALL_RAMDISK_SIZE $extra_append

PXE_END
done

echo "done!"
