#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: This program will put a syslinux.cfg or isolinux.cfg in target directory, which is used to boot clonezilla live.

# 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

# Load some necessary setting from drbl-ocs.conf
. /opt/drbl/conf/drbl-ocs.conf


# setting
kernel=vmlinuz
initrd=initrd.gz
bootparam="boot=casper"
VGA_MODE_DEF="788"
# default time (Unit: 1/10 secs)
TIMEOUT_DEF="70"

#
prog="$(basename $0)"

# functions
USAGE() {
    echo "Usage:"
    echo "To create syslinux or isolinux menu for clonezilla live:"
    echo "$prog [OPTION] [isolinux|syslinux] OUTPUT-DIR" 
    echo "OPTION:"
    language_help_prompt_by_idx_no
    echo "-b, --bg-mode  [text|graphic]  Assign the back ground of boot menu. Default is graphic"
    echo "-f, --framebuffer-mode  VGA_MODE  Assign the framebuffer mode as VGA_MODE, for example, 788 is resolution 800x600 with 16 bpp."
    echo "-t, --timeout TIME   Assign TIME*0.1 secs for timeout when booting."
    echo "Ex:"
    echo "To create a isolinux menu in dir isolinux_tmp/isolinux/"
    echo "$prog isolinux isolinux_tmp/isolinux/"
}
gen_config() {
  local type=$1
  case "$type" in
    isolinux) output_file="$output_dir/isolinux.cfg" ;;
    syslinux) output_file="$output_dir/syslinux.cfg" ;;
  esac
  echo "Adding $type menus for Clonezilla live..."
  cat <<-BOOT_MENU_END > $output_file
# 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
default $MENUC32
timeout $timeout
prompt 0
noescape 1
MENU MARGIN 5
$mask MENU BACKGROUND ocslogo.png
# Set the color for unselected menu item and timout message
$mask MENU COLOR UNSEL 7;32;41 #c0000090 #00000000
$mask MENU COLOR TIMEOUT_MSG 7;32;41 #c0000090 #00000000
$mask MENU COLOR TIMEOUT 7;32;41 #c0000090 #00000000

# MENU MASTER PASSWD

say **********************************************************************
say Clonezilla, the OpenSource Clone System.
say NCHC Free Software Labs, Taiwan.
say http://drbl.nchc.org.tw; http://drbl.sf.net
say THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK! 
say **********************************************************************

# Allow client to edit the parameters
ALLOWOPTIONS 1

# simple menu title
MENU TITLE http://clonezilla.nchc.org.tw, http://clonezilla.sf.net

label Clonezilla live
  MENU DEFAULT
  # MENU HIDE
  MENU LABEL Clonezilla live
  # MENU PASSWD
  kernel $kernel
  append initrd=$initrd boot=casper vga=$vga_mode

label Clonezilla live without framebuffer
  # MENU DEFAULT
  # MENU HIDE
  MENU LABEL Clonezilla live (no framebuffer)
  # MENU PASSWD
  kernel $kernel
  append initrd=$initrd boot=casper

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

label etherboot
  # MENU DEFAULT
  # MENU HIDE
  MENU LABEL Network boot via etherboot
  # MENU PASSWD
  kernel eb.zli

BOOT_MENU_END
}
#
# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -l|--language)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              specified_lang="$1"
              shift
            fi
	    [ -z "$specified_lang" ] && USAGE && exit 1
            ;;
    -b|--bg-mode)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              bg_mode="$1"
              shift
            fi
	    [ -z "$bg_mode" ] && USAGE && exit 1
            ;;
    -f|--framebuffer-mode)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              vga_mode="$1"
              shift
            fi
	    [ -z "$vga_mode" ] && USAGE && exit 1
            ;;
    -t|--timeout)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              timeout="$1"
              shift
            fi
	    [ -z "$timeout" ] && USAGE && exit 1
            ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

output_format="$1"
output_dir="$2"
[ -z "$output_format" ] && USAGE && exit 1
[ -z "$output_dir" ] && USAGE && exit 1
[ -z "$vga_mode" ] && vga_mode=$VGA_MODE_DEF
[ -z "$timeout" ] && timeout=$TIMEOUT_DEF

#
case "$bg_mode" in 
   txt|TXT|text|TEXT)
      MENUC32="menu.c32"
      mask="#"
      ;;
   *)
      MENUC32="vesamenu.c32" ;;
esac

#
case "$output_format" in 
   isolinux|ISOLINUX)
      gen_config isolinux ;;
   syslinux|SYSLINUX)
      gen_config syslinux ;;
esac
