# /usr/lib/mindi/TryToBeCleverAboutInitrd
#
# Debian Plugin script for mindi to decide whether to create the initrd image
# for boot CDs as ext2 or cpio (or to die if neither is possible).
#
# Changelog:
# 21Jul04AL: - intial version
# 22Jul04AL: - even more comments
#            - deal with isolinux and sylinux configuration files
#            - break log messgae into multiple lines
# 28Jul04AL: - added missing syslinux-H.cfg config file
#            - replaced copying syslinux and isolinux config files with linking
#              to make thins clearer and to save a few bytes
#            - deal with FAILSAFE kernel here to keep mindi changes minimal
#            - create link to init explictly (init or linuxrc depending on
#              initrd image type); required for new symlinks.tgz
# 29Jul04AL: - leave mountpoint before unmounting in UseExt2 :-(
# 11Oct05AL: - check kernelpath variable as well to avoid failure when mindi is
#              run directly rather than from mondoarchive
# 29Jan06AL: - check the kernel image directly for 'magic' strings to establish
#              kernel capabilities (basically a rewrite)
# 26Feb06AL: - added functions WriteSyslinuxFile() and CreateSyslinuxFile()
#            - use above functions to create syslinux config file on the fly
#            - implement new coding rules: function definitions are prepended
#              with 'function for clarity, variables in fucntions are local,
#              variable names are <scope><type><NameInCamelCase> where scope is
#              currently one of l(ocal) and g(lobal) and <type> is currently one
#              of a(rray), c(onstant) and v(ariable)
# 04Feb07AL: - merged code for correct initrd filesystem into mindi (so the
#              name of the script is a bit misleading now as only the syslinux
#              functionality is left)
#




###############################################################################
# Subroutines
###############################################################################


# Low-level function to create syslinux configuration file
# (returns file contents)
#
# Interface definition:
# param #1: default action (syslinux)
# param #2: timeout [1/10s] (syslinux)
# param #3: ramdisk size [kb] (kernel)
# param #4: load ramdisk [0|1] (kernel)
# param #5: prompt fpr ramdisk [0|1] (kernel)
# param #6: list of actions separated by space, actions consist of label
#           and one or multiple boot parameters all separated by commas
# param #7: additional boot parameters applicable to all actions   
#
function WriteSyslinuxFile() {

    # interface test: make sure we have seven parameters
    if [ $# -ne 7 ]; then
	Die "WriteSyslinuxFile(): Expected 7 parameters, got $#."
    fi

    # interface parameters
    local lvDefaultAction=$1
    local lvTimeout=$2
    local lvRamdiskSize=$3
    local lvLoadRamdisk=$4
    local lvPromptRamdisk=$5
    local lvActionList=$6
    local lvAdditionalParameters=$7

    # local constants (separator for parameter parsing)
    local readonly lcSeparator=","

    # internal parameters
    local lvAction=""
    local lvActionLabel=""
    

    # write header
    echo "default $lvDefaultAction"
    echo "prompt 1"
    echo "timeout $lvTimeout"
    echo "display message.txt"
    # write boot entries
    for lvAction in $lvActionList; do
	# split out label
	lvActionLabel=`echo $lvAction | cut -d"$lcSeparator" -f1`
        # remove label including separating comma from action
	lvAction=${lvAction/$lvActionLabel$lcSeparator/}
        # replace commas with spaces for actions
        lvAction=${lvAction//$lcSeparator/ }
        # write single boot entry
	echo "label $lvActionLabel"
	echo "  kernel vmlinuz"
	echo "  append initrd=initrd.img ramdisk_size=$lvRamdiskSize root=/dev/ram0 rw load_ramdisk=$lvLoadRamdisk prompt_ramdisk=$lvPromptRamdisk $lvAction $lvAdditionalParameters"
    done

}


# High-level function to create syslinux configuarion file
#
# Interface definition:
# param #1: name of output file (including path where appropriate)
# param #2: mode: 'ISO' (cd/dvd) or 'SYS' (floppy)
# param #3: ask for user confirmation (for '-H' switch): 'Y(es)' or 'N(o)'
# param #4: size of ramdisk to create in kb
# param #5: further boot parameters for all actions
#
function CreateSyslinuxFile() {

    # interface test: make sure we have five parameters
    if [ $# -ne 5 ]; then
	Die "CreateSyslinuxFile(): Expected 5 parameters, got $#."
    fi

    # interface parameters
    local lvFileName=$1
    local lvMode=$2
    local lvAsk=$3
    local lvRamdiskSize=$4
    local lvAdditionalParameters=$5

    # local constants (booleans)
    local readonly lcTrue="Y"
    local readonly lcFalse="N"


    # create files depending on whether ISO (CD-ROM) or SYS (floppy) is
    # specified and whether user wants to be prompted on restore or not
    LogIt "CreateSyslinuxFile(): Writing to file $lvFileName.\n"
    if [ $lvMode == "ISO" ]; then
	if [ $lvAsk == $lcTrue ]; then
	    WriteSyslinuxFile "interactive"                                                                         \
                              "300"                                                                                 \
                              "$lvRamdiskSize"                                                                      \
                              "1"                                                                                   \
                              "0"                                                                                   \
                              "expert,expert_mode interactive,interactive_mode compare,compare_mode nuke,nuke_mode" \
		              "$lvAdditionalParameters"                                                             \
	    > $lvFileName
	elif [ $lvAsk == $lcFalse ]; then
	    WriteSyslinuxFile "RESTORE"                                 \
                              "10000"                                   \
                              "$lvRamdiskSize"                          \
                              "1"                                       \
                              "0"                                       \
                              "RESTORE,nuke,restore expert,expert_mode" \
		              "$lvAdditionalParameters"                 \
	    > $lvFileName
	else
	    Die "CreateSyslinuxFile(): Expected 'Y' or 'N', got $lvAsk. (Mode: ISO)"
	fi
    elif [ $lvMode == "SYS" ]; then
	if [ $lvAsk == $lcTrue ]; then
	    WriteSyslinuxFile "interactive"                                                                         \
                              "300"                                                                                 \
                              "$lvRamdiskSize"                                                                      \
                              "0"                                                                                   \
                              "0"                                                                                   \
                              "expert,expert_mode interactive,interactive_mode compare,compare_mode nuke,nuke_mode" \
		              "$lvAdditionalParameters"                                                             \
	    > $lvFileName
	elif [ $lvAsk == $lcFalse ]; then
	    WriteSyslinuxFile "RESTORE"                                 \
                              "10000"                                   \
                              "$lvRamdiskSize"                          \
                              "0"                                       \
                              "0"                                       \
                              "RESTORE,nuke,restore expert,expert_mode" \
		              "$lvAdditionalParameters"                 \
	    > $lvFileName
	else
	    Die "CreateSyslinuxFile(): Expected 'Y' or 'N', got $lvAsk. (Mode: SYS)"
	fi
    else
	Die "CreateSyslinuxFile(): Expected 'ISO' or 'SYS', got $lvMode."
    fi

}


###############################################################################
#Main Part
###############################################################################


# create syslinux configuration files
LogIt "Creating [sys|iso]linux configuration files..."
(cd $MINDI_LIB && rm -f isolinux.cfg isolinux-H.cfg syslinux.cfg syslinux-H.cfg)
CreateSyslinuxFile "$MINDI_LIB/isolinux.cfg"   "ISO" "Y" "24000" ""
CreateSyslinuxFile "$MINDI_LIB/isolinux-H.cfg" "ISO" "N" "24000" ""
CreateSyslinuxFile "$MINDI_LIB/syslinux.cfg"   "SYS" "Y" "24000" ""
CreateSyslinuxFile "$MINDI_LIB/syslinux-H.cfg" "SYS" "N" "24000" ""
LogIt "done.\n"
