#!/bin/sh

set -e

DEVICE="${1}"

# User is unprivileged
if [ "$(id -u)" -ne 0 ]
then
	echo "E: need root privileges"
	exit 1
fi

# Searching extlinux directory
echo -n "P: Searching for EXTLINUX directory..."

for LOCATION in /boot/extlinux /boot/boot/exlinux /extlinux
do
	if [ -e "${LOCATION}" ]
	then
		DIRECTORY="${LOCATION}"

		echo " found: ${DIRECTORY}"
		break
	fi
done

if [ -z "${DIRECTORY}" ]
then
	echo " not found."

	echo "E: To create a template run 'mkdir /boot/extlinux' first."
	echo "E: To install extlinux, do it manually or try the 'extlinux-install' command."
	echo "E: Warning, extlinux-install is used to change your MBR."

	exit 1
fi

if [ ! -e /etc/default/extlinux ]
then
	EXTLINUX_UPDATE="true"
	EXTLINUX_ALTERNATIVES="default recovery"
	EXTLINUX_DEFAULT="0"

	if [ -x "$(which lsb_release 2>/dev/null)" ]
	then
		EXTLINUX_MENU_LABEL="$(lsb_release -i -s) GNU/Linux, kernel"
	else
		EXTLINUX_MENU_LABEL="Debian GNU/Linux, kernel"
	fi

	EXTLINUX_PARAMETERS="ro quiet"

	# Find root partition
	while read LINE
	do

read fs_spec fs_file fs_vfstype fs_mntops fs_freq fs_passno << EOF
${LINE}
EOF

		if [ "${fs_spec}" != "#" ] && [ "${fs_file}" = "/" ]
		then
			EXTLINUX_ROOT="root=${fs_spec}"
			break
		fi
	done < /etc/fstab

	if [ -e /usr/share/syslinux/themes/debian/extlinux ]
	then
		EXTLINUX_THEME=debian
	else
		EXTLINUX_THEME=
	fi

	EXTLINUX_TIMEOUT="50"

cat > "/etc/default/extlinux" << EOF
## /etc/default/extlinux

EXTLINUX_UPDATE="${EXTLINUX_UPDATE}"

EXTLINUX_ALTERNATIVES="${EXTLINUX_ALTERNATIVES}"
EXTLINUX_DEFAULT="${EXTLINUX_DEFAULT}"
EXTLINUX_MENU_LABEL="${EXTLINUX_MENU_LABEL}"
EXTLINUX_PARAMETERS="${EXTLINUX_PARAMETERS}"
EXTLINUX_ROOT="${EXTLINUX_ROOT}"
EXTLINUX_THEME="${EXTLINUX_THEME}"
EXTLINUX_TIMEOUT="${EXTLINUX_TIMEOUT}"
EOF

else
	. /etc/default/extlinux
fi

if [ "${EXTLINUX_UPDATE}" != "true" ]
then
	echo "P: extlinux-update is disabled in /etc/default/extlinux."

	exit 0
fi

# Create linux.cfg
cat > "${DIRECTORY}/linux.cfg" << EOF
## ${DIRECTORY}/linux.cfg
##
## IMPORTANT WARNING
##
## The configuration of this file is generated automatically.
## Do not edit this file manually, use: update-extlinux

EOF

# Find linux versions
VERSIONS="$(cd /boot && ls vmlinuz-* | sed -e 's|vmlinuz-||g' | sort -r)"

if [ "$(stat --printf %d /)" = "$(stat --printf %d /boot)" ]
then
	# / and /boot are on the same filesystem
	BOOT="/boot"
else
	# / and /boot are not on the same filesystem
	BOOT=""
fi

for VERSION in ${VERSIONS}
do
	echo "P: Writing config for /boot/vmlinuz-${VERSION}..."

	NUMBER="${NUMBER:-0}"

	if [ -e /boot/initrd.img-${VERSION} ]
	then
		INITRD="initrd=${BOOT}/initrd.img-${VERSION}"
	else
		INITRD=""
	fi

	if echo ${EXTLINUX_ALTERNATIVES} | grep -q default
	then

# Writing default entry
cat >> "${DIRECTORY}/linux.cfg" << EOF

label ${NUMBER}
	menu label ${EXTLINUX_MENU_LABEL} ${VERSION}
	menu default
	kernel ${BOOT}/vmlinuz-${VERSION}
	append ${INITRD} ${EXTLINUX_ROOT} ${EXTLINUX_PARAMETERS}
EOF

	fi

	if echo ${EXTLINUX_ALTERNATIVES} | grep -q live
	then

# Writing live entry
cat >> "${DIRECTORY}/linux.cfg" << EOF

label ${NUMBER}l
	menu label ${EXTLINUX_MENU_LABEL} ${VERSION} (live mode)
	menu default
	kernel ${BOOT}/vmlinuz-${VERSION}
	append ${INITRD} ${EXTLINUX_ROOT} ${EXTLINUX_PARAMETERS} boot=live plainroot
	text help
   This option boots the system into live mode (non-persistent)
	endtext
EOF

	fi

	if echo ${EXTLINUX_ALTERNATIVES} | grep -q recovery
	then

# Writing recovery entry
cat >> "${DIRECTORY}/linux.cfg" << EOF

label ${NUMBER}r
	menu label ${EXTLINUX_MENU_LABEL} ${VERSION} (recovery mode)
	menu default
	kernel ${BOOT}/vmlinuz-${VERSION}
	append ${INITRD} ${EXTLINUX_ROOT} ${EXTLINUX_PARAMETERS} single
	text help
   This option boots the system into recovery mode (single-user)
	endtext
EOF

	fi

	NUMBER="$((${NUMBER} + 1))"
done

cat > "${DIRECTORY}/extlinux.conf" << EOF
## ${DIRECTORY}/extlinux.conf
##
## IMPORTANT WARNING
##
## The configuration of this file is generated automatically.
## Do not edit this file manually, use: update-extlinux


default ${EXTLINUX_DEFAULT}
prompt 1
timeout ${EXTLINUX_TIMEOUT}
EOF

if [ -n "${EXTLINUX_THEME}" ]
then
	if [ ! -e "/usr/share/syslinux/themes/${EXTLINUX_THEME}/extlinux" ]
	then
		echo "E: /usr/share/syslinux/${EXTLINUX_THEME}/extlinux: No such file or directory"
		exit 1
	else
		echo -n "P: Installing ${EXTLINUX_THEME} theme..."
		rm -rf "${DIRECTORY}/themes/${EXTLINUX_THEME}"

		mkdir -p "${DIRECTORY}/themes"
		cp -aL "/usr/share/syslinux/themes/${EXTLINUX_THEME}/extlinux" "${DIRECTORY}/themes/${EXTLINUX_THEME}"
		echo " done."
	fi

cat >> "${DIRECTORY}/extlinux.conf" << EOF

include themes/${EXTLINUX_THEME}/theme.cfg
EOF

else

cat >> "${DIRECTORY}/extlinux.conf" << EOF

display boot.txt
include linux.cfg
EOF

	if [ ! -e "${DIRECTORY}/boot.txt" ]
	then
		echo "Wait 5 seconds or press ENTER to " > "${DIRECTORY}/boot.txt"
	fi
fi
