#!/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_FILE_DEFAULT="vmlinuz"
INITRD_FILE_DEFAULT="initrd.gz"
VGA_MODE_DEF="788"
BOOT_PARAM_DEFAULT="boot=casper"
# 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|grub] 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 "-k, --kernel-file FILENAME         Provide the kernel name, which wiil be used in the menu.lst or syslinux.cfg. If unset, $KERNEL_FILE_DEFAULT will be used" 
    echo "-i, --initrd-file FILENAME         Provide the initrd name, which wiil be used in the menu.lst or syslinux.cfg. If unset, $INITRD_FILE_DEFAULT will be used" 
    echo "-p, --boot-param  PARAM            Provide the append strings for kernel bootparam. which wiil be used in the menu.lst or syslinux.cfg. If unset, $BOOT_PARAM_DEFAULT will be used. Note! For clonezilla live (based on Debian Live), boot=casper is a must, so do not forget to include it." 
    echo "-g1, grub-hd-dev DEV Assign grub root device DEV (Ex: hd0)"
    echo "-g2, grub-part-no NO Assign grub root partition number index NO (Ex: 0)"
    echo "-g3, OS-root-dev ROOTDEV Assign ROOTDEV which grub will pass as boot param (Ex. /dev/sda1)"
    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/"
    echo "To create a grub menu in dir grub_tmp/boot/grub/"
    echo "$prog grub grub_tmp/boot/grub/"
}
gen_config_syslinux() {
  local type=$1
  case "$type" in
    isolinux) output_file="$output_dir/isolinux.cfg" ;;
    syslinux) output_file="$output_dir/syslinux.cfg" ;;
  esac
  bg_img="$(basename $ocs_logo_img_syslinux)"
  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 $bg_img
# 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_file
  append initrd=$initrd_file $boot_param vga=$vga_mode

label Clonezilla live without framebuffer
  # MENU DEFAULT
  # MENU HIDE
  MENU LABEL Clonezilla live (no framebuffer)
  # MENU PASSWD
  kernel $kernel_file
  append initrd=$initrd_file $boot_param

# 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
}
#
gen_config_grub(){
  local grub_conf=$1
  local bg_img output_file
  [ -z "$grub_conf" ] && grub_conf=menu.lst
  bg_img="$(basename $ocs_logo_img_grub)"
  output_file="$output_dir/$grub_conf"
  # the timeout is in unit 1/10 sec, convert to sec for grub
  timeout_sec="$(echo "scale=0; $timeout /10" | bc -l)"
  echo "Adding grub menus for Clonezilla live..."
  cat <<-BOOT_MENU_END > $output_file
default	0
timeout $timeout_sec
color cyan/blue white/blue
splashimage=/boot/grub/$bg_img
title Clonezilla live Device
  root ($op_hd_dev_grub,$op_part_no_grub)
  kernel /boot/$kernel_file root=$output_dev ro $boot_param vga=$vga_mode
  initrd /boot/$initrd_file

title Clonezilla live without framebuffer
  root ($op_hd_dev_grub,$op_part_no_grub)
  kernel /boot/$kernel_file root=$output_dev ro $boot_param
  initrd /boot/$initrd_file

# Note! *.bin is specially purpose for syslinux, 
# Do NOT use memtest.bin, use memtest instead of memtest.bin
title memtest
  kernel memtest

title etherboot
  kernel eb.zli
BOOT_MENU_END
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_modify_grub_config_if_necessary:"
  echo "$msg_delimiter_star_line"
  echo "root ($op_hd_dev_grub,$op_part_no_grub)"
  echo "kernel /boot/$kernel_file root=$output_dev ro $boot_param"
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
}
#
# 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
            ;;
    -g1|grub-hd-dev)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              op_hd_dev_grub="$1"
              shift
            fi
	    [ -z "$op_hd_dev_grub" ] && USAGE && exit 1
            ;;
    -g2|grub-part-no)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              op_part_no_grub="$1"
              shift
            fi
	    [ -z "$op_part_no_grub" ] && USAGE && exit 1
            ;;
    -g3|OS-root-dev)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              output_dev="$1"
              shift
            fi
	    [ -z "$output_dev" ] && USAGE && exit 1
            ;;
    -k|--kernel-file)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              kernel_file="$1"
              shift
            fi
	    [ -z "$kernel_file" ] && USAGE && exit 1
            ;;
    -i|--initrd-file)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              initrd_file="$1"
              shift
            fi
	    [ -z "$initrd_file" ] && USAGE && exit 1
            ;;
    -p|--boot-param)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              boot_param="$1"
              shift
            fi
	    [ -z "$boot_param" ] && 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
[ -z "$kernel_file" ] && kernel_file="$KERNEL_FILE_DEFAULT"
[ -z "$initrd_file" ] && initrd_file="$INITRD_FILE_DEFAULT"
[ -z "$boot_param" ] && boot_param="$BOOT_PARAM_DEFAULT"
#
ask_and_load_lang_set $specified_lang

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

#
case "$output_format" in 
   isolinux|ISOLINUX)
      gen_config_syslinux isolinux ;;
   syslinux|SYSLINUX)
      gen_config_syslinux syslinux ;;
   grub|GRUB)
      gen_config_grub menu.lst ;;
esac
