# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../syslinux/boot/boot.in
# Copyright (C) 2008 The OpenSDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

# converts a filename into norock,nojoliet names
isolinux_filename() {
	local dir= name= ext= extlen=
	# only the last dot is kept, the rest are converted into _s
	# and -s are also overted into _s
	echo "$1" |
		sed -e 's,\(.*/\)\?\([^/]*\)\.\([^\.]\+\)$,\1\t\2\t\3,' -e 's,\.,_,g' -e 's,-,_,g' |
	while read dir name ext; do
		# we have dir, name, ext separated and encoded now
		# to be able to restrict to len( name + '.' + ext ) < 32
		if [ -n "$ext" ]; then
			extlen=$( echo -n "$ext" | wc -c )
			name=$( echo -n "$name" | cut -c1-`expr 30 - $extlen` )
		else
			name=$( echo -n "$name" | cut -c1-31 )
		fi
			
		echo "${dir}${name}${ext:+.$ext}"
	done
}

# generates a label isolinux can understand
isolinux_label() {
	echo "$1" | md5sum | cut -d' ' -f1
}

boot_cd_pre() {
	local isolinuxdir="$isofsdir/boot/isolinux"
	mkdir -p "$isolinuxdir"
	cp -f "$build_root/usr/lib/syslinux/"{isolinux.bin,menu.c32} "$isolinuxdir"

	cat > "$isolinuxdir/isolinux.cfg" <<-EOT
	DEFAULT menu.c32
	PROMPT 0

	MENU TITLE OpenSDE Installer

	EOT
}

boot_cd_add() {
	cat >> "$1/boot/isolinux/isolinux.cfg" <<-EOT
	LABEL $( isolinux_label "$3 (Kernel: $2)" )
	    MENU LABEL $3 (Kernel: $2)
	    KERNEL $( isolinux_filename $4 )
	    APPEND initrd=$( isolinux_filename $5 )

	EOT
}

boot_cd_post() {
	echo_status "Creating isofs_arch.txt file .."

	cat > $build_toolchain/isofs_arch.txt <<- EOT
	BOOT    -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -no-emul-boot
	BOOTx   -boot-load-size 4 -boot-info-table
	DISK1   $isofsdir/ /
	EOT
}