#! /bin/bash
# Run oem-config on the first boot after shipping to the end user.
set -e

# TODO: This is an awful, awful hack. In Ubuntu intrepid, kernel-helper runs
# from an /etc/event.d script (in parallel to rc2) and uses debconf; as a
# result, it is often running while oem-config is trying to start and locks
# the debconf database. I can't think of a way to declare that
# /etc/event.d/last-good-boot needs to complete before our single init
# script in rc2 starts, so we're left with this piece of pure evil.
while pidof -x kernel-helper; do
	sleep 1
done

DEBUG=
if [ "$1" = '--debug' ]; then
	DEBUG=--debug
fi

# KDM stores the default user here, and apparently gets upset that we've
# just removed its previous default user.
rm -f /var/lib/kdm/kdmsts
# Revert to creating a user at uid 1000.
echo RESET passwd/user-uid | debconf-communicate >/dev/null
# These two templates have been preseeded, which does nasty things to their
# templates. It's probably a bug in cdebconf's debconf-copydb that they end
# up still registered to debian-installer/dummy at this point, but let's
# just work around it for now so that we get sensible translated
# descriptions.
for q in passwd/user-fullname passwd/username; do
	echo REGISTER "$q" "$q"
	echo RESET "$q"
done | debconf-communicate oem-config >/dev/null
# Remove the oem-config-prepare menu item.
rm -f /usr/share/applications/oem-config-prepare-gtk.desktop \
      /usr/share/applications/kde/oem-config-prepare-kde.desktop
# Adjust fontconfig configuration, if possible.
if type fontconfig-voodoo >/dev/null 2>&1; then
	RET="$(echo GET debian-installer/locale | debconf-communicate)"
	if [ "${RET%% *}" = 0 ]; then
		LC_ALL="${RET#* }" fontconfig-voodoo --auto --force --quiet \
			|| true
	fi
fi

# TODO: will this work for X-based frontends when X isn't up yet?
FRONTEND="$(/usr/sbin/oem-config -q)"

if [ "$DEBUG" ]; then
	TRY=1
else
	TRY=5
fi
for try in $(seq 1 $TRY); do
	CODE=0
	if [ "$FRONTEND" = debconf_ui ]; then
		LANG=en_US.UTF-8 \
		/usr/sbin/oem-config $DEBUG --only 2>>/var/log/oem-config.log \
			|| CODE=$?
	else
		/usr/sbin/oem-config-dm vt7 :0 \
			/usr/sbin/oem-config $DEBUG --only || CODE=$?
	fi
	if [ "$CODE" -eq 0 ]; then
		# Don't run again.
		update-rc.d -f oem-config remove >>/var/log/oem-config.log 2>&1
		# Remove the temporary OEM configuration user, if possible.
		if getent passwd oem >/dev/null; then
			deluser --quiet --remove-home oem || true
		fi
		exit 0
	elif [ "$CODE" -eq 10 ]; then
		cat <<EOF
Your system is not yet configured. Press 'a' to try again, 's' for a
recovery shell, or 'r' to reboot.
EOF
		while :; do
			read -p '[asr] ' -n 1 REPLY
			echo
			case $REPLY in
				A|a)	continue 2 ;;
				S|s)
					cat <<EOF
After you type 'exit', your system will reboot.
EOF
					bash
					reboot
					sleep 3600
					exit 1
					;;
				R|r)	reboot; sleep 3600; exit 1 ;;
			esac
		done
		exit 1
	fi
done

if [ -z "$DEBUG" ]; then
	# Panic. At this point, probably the best thing we can do is drop to
	# a shell so that the user has some hope of fixing things up.
	cat <<EOF
ERROR: The OEM installer failed. Your system may not be usable yet. Please
report this as a bug to your vendor.

To create a user so that you can use your new system normally, type:

    adduser USERNAME

... replacing USERNAME with the username you would like to use (your first
name in lower case is normally a reasonable choice), and follow the prompts.
If this succeeds, type 'exit' to reboot the system.

EOF
	bash

	# Don't run again.
	update-rc.d -f oem-config remove >>/var/log/oem-config.log 2>&1

	reboot
fi

exit 0
