#!/bin/sh
# 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 shouild 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

#
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 thost 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 menu.c32
timeout 70
prompt 0

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 Linux (from DRBL server, mostly use local resources)
  kernel vmlinuz-pxe
  append initrd=initrd-pxe.img ramdisk_size=$PXE_RAMDISK_SIZE $CONSOLE_OUTPUT devfs=nomount drblthincli=off

label local
  # MENU DEFAULT
  # MENU HIDE
  MENU LABEL Operating System in local harddrive (if available)
  localboot 0

# 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+
  kernel memtest86

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

label drbl-thin-client
  # MENU DEFAULT
  MENU HIDE
  MENU LABEL Linux (from DRBL server, mostly use remote resources)
  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
netinstall_list="$(rpm -qa |grep -E "^.*netinstall.*drbl" | sort)"
for inet in $netinstall_list; do
   img_menu="$(echo "$inet" | sed -e "s/netinstall-[0-9]\.[0-9].*drbl/netinstall/g")"
   img_full="$(rpm -ql $inet | grep -E "^.*netinstall.img")"
   img="$(basename $img_full)"
   kernel_full="$(rpm -ql $inet | grep -E "^.*vmlinuz\..*netinstall$")"
   kernel="$(basename $kernel_full)"

   # 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..."
   # note!!! If Debian sarge is released! We have to append more parameters,
   # Ex: append initrd=initrd_sarge_k2.6_netinstall.img ramdisk_size=65535 root=/dev/rd/0 devfs=mount,dall rw  --
   # check http://http.us.debian.org/debian/dists/testing/main/installer-i386/current/images/netboot/pxelinux.cfg/default

   # 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/rh-FC/Fedora Core /g" -e "s/rh-/RedHat /g" -e "s/mdk-/MandrakeLinux /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
  kernel $kernel
  append initrd=$img ramdisk_size=$NETINSTALL_RAMDISK_SIZE

PXE_END
done

echo "done!"
