#!/bin/sh

## live-config(7) - System Configuration Scripts
## Copyright (C) 2006-2013 Daniel Baumann <daniel@debian.org>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


Upstart ()
{
	# Checking if package is installed or already configured
	if [ ! -e /var/lib/dpkg/info/upstart.list ] || \
	   [ -e /var/lib/live/config/upstart ]
	then
		return
	fi

	echo -n " upstart"

	# Reading kernel command line
	for _PARAMETER in ${_CMDLINE}
	do
		case "${_PARAMETER}" in
			live-config.username=*|username=*)
				LIVE_USERNAME="${_PARAMETER#*username=}"
				;;
		esac
	done

	Configure_upstart
}

Configure_upstart ()
{
	for _FILE in /etc/init/tty[0-9]*.conf
	do
		_TTY="$(basename ${_FILE} .conf)"

		if [ -e /sbin/mingetty ] || \
		   grep -qs '^exec.*mingetty' "${_FILE}"
		then
			# mingetty has autologin support
			sed -i -e 's|^\(exec.*getty\) *\(--autologin *[^ ]* *\)*.*$|exec /sbin/mingetty --autologin '"${LIVE_USERNAME} ${_TTY}|g" "${_FILE}"
		else
			# substitute getty with a direct login
			sed -i -e 's|^exec.*getty.*$|exec /bin/login -f '"${LIVE_USERNAME} </dev/${_TTY} >/dev/${_TTY} 2>\&1|" "${_FILE}"
		fi
	done

	init q

	for _FILE in /etc/init/tty[0-9]*.conf
	do
		_TTY="$(basename ${_FILE} .conf)"

		# Try reloading
		if stop ${_TTY}
		then
			start ${_TTY}
		fi
	done

	# Creating state file
	touch /var/lib/live/config/upstart
}

Upstart
