#!/bin/sh
# Hacks to inject code into various live-build functions

current_script="$(basename "$0"|sed 's/^lb_//')"

# remove isohybrid-acritox space file from md5sums.txt because it will be
# modified afterwards so the checksum will never match
if [ "$current_script" = "binary_iso" ]; then
	echo "HOOK: livebuild-hacks@binary_iso: fix isohybrid-acritox checksums"
    sed -i '/boot\.isohybrid/d' binary/md5sum.txt
fi

# ISO sorting
if [ "$current_script" = "binary_iso" ]; then
	echo "HOOK: livebuild-hacks@binary_iso"
	Genisoimage_options_hack ()
	{
		if [ -f config/binary_iso/isoimage.sort ]
		then
            case "${LB_PARENT_DISTRIBUTION}" in
                squeeze) echo " -sort isoimage.sort";; # genisoimage
                *) # xorriso - emulate the "-sort" argument of genisoimage:
                    set -x
                    echo " --boot-catalog-hide" $( 
                        while read pattern weight
                        do
                            for file in $pattern
                            do
                                [ -e "$file" ] && echo --sort-weight "$(echo "100+0$weight" | bc)" "$file"
                            done
                        done < config/binary_iso/isoimage.sort | sed 's/ binary/ /')
                    set +x
                    ;;
                    # echo ' --boot-catalog-hide $(while read pattern weight; do for file in $pattern; do [ -e "$file" ] && echo --sort-weight "$(echo "100+0$weight" | bc)" "$(echo "$file"|sed 's/^binary//')"; done; done < isoimage.sort)';;
            esac

			case "${LB_BUILD_WITH_CHROOT}" in
				true)
					cp config/binary_iso/isoimage.sort chroot
					;;

				false)
					cp config/binary_iso/isoimage.sort .
					;;
			esac
		fi
		Genisoimage_options_hack () { true; } # only once -> overload
	}
	In_list ()
	{
		. "${LB_BASE}"/functions/aliases.sh
		In_list "$@"
		RC=$?
		GENISOIMAGE_OPTIONS="${GENISOIMAGE_OPTIONS}$(Genisoimage_options_hack)"
		case "$LB_BOOTLOADER" in
		grub2|burg)
			LB_BINARY_IMAGES=iso
			;;
		esac
		return $RC
	}
	Install_package ()
	{
		. "${LB_BASE}"/functions/packages.sh
		Install_package "$@"
		RC=$?
		LB_ISO_APPLICATION="${LB_ISO_APPLICATION}\"$(Genisoimage_options_hack)\""
		case "$LB_BOOTLOADER" in
		grub2|burg)
			LB_BINARY_IMAGES=iso
			;;
		esac
		return $RC
	}
fi

# Grub2: Do not number the default kernel / initrd files.
if [ "$current_script" = "binary_grub2" ]; then
	echo "HOOK: livebuild-hacks@binary_grub2"
	Overload_Grub_live_entry ()
	{
		Grub_live_entry ()
		{
			echo "HACK: overloaded Grub_live_entry called."
			LABEL="${1}"
			KERNEL="${2}"
			INITRD="${3}"
			APPEND="${4}"

			case "${LB_BINARY_IMAGES}" in
				iso*|usb*)
					if [ "${LABEL}" = "live" ]
					then
						# Do not number the default kernel / initrd files.
						_NUMBER=""
					else
						# Use order of flavours in LB_LINUX_FLAVOURS for numbering.
						_NUMBER="$(echo ${LB_LINUX_FLAVOURS} | awk -v FLAVOUR="${FLAVOUR}" 'BEGIN{RS=" "} {if($1 == FLAVOUR) print NR}')"
					fi

					if [ -e binary/${KERNEL} ]
					then
						mv binary/${KERNEL} "binary/$(dirname ${KERNEL})/vmlinuz${_NUMBER}" | :
						KERNEL="$(dirname ${KERNEL})/vmlinuz${_NUMBER}"
					fi

					if [ -e binary/${INITRD} ]
					then
						mv binary/${INITRD} "binary/$(dirname ${INITRD})/initrd${_NUMBER}.img" | :
						INITRD="$(dirname ${INITRD})/initrd${_NUMBER}.img"
					fi
				;;
			esac

			LINUX_LIVE="${LINUX_LIVE}\nmenuentry \"Debian GNU/Linux - ${LABEL}\" {"
			LINUX_LIVE="${LINUX_LIVE}\nlinux\t\t/${KERNEL} boot=${INITFS} config LB_BOOTAPPEND_LIVE ${APPEND}"
			LINUX_LIVE="${LINUX_LIVE}\ninitrd\t\t/${INITRD}"
			LINUX_LIVE="${LINUX_LIVE}\n}"
		}
	}
	Check_multiarchitectures ()
	{
		. "${LB_BASE}"/functions/architectures.sh
		Check_multiarchitectures "$@"
		Overload_Grub_live_entry
		RC=$?
		return $RC
	}
	Check_multiarchitecture ()
	{
		. "${LB_BASE}"/functions/architecture.sh
		Check_multiarchitecture "$@"
		Overload_Grub_live_entry
		RC=$?
		return $RC
	}
fi

if [ "$current_script" = "binary_checksums" ]; then
    echo "HOOK: livebuild-hacks@binary_checksums: check for initrd.img"
    if [ -s binary/live/initrd.img ];then
        KINITRDSIZE=$(stat -c %s "binary/live/initrd.img")
        echo "INFO: initrd size=$KINITRDSIZE"
    else
        echo "E: no initrd found"
    fi
fi