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

# Remove the temporary OEM configuration user, if possible.
if getent passwd oem >/dev/null; then
	deluser --quiet --remove-home oem || true
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" | debconf-communicate oem-config >/dev/null
done

for try in $(seq 1 5); do
	CODE=0
	/usr/sbin/oem-config-dm vt7 :0 /usr/sbin/oem-config || CODE=$?
	if [ "$CODE" -eq 0 ]; then
		# Don't run again.
		update-rc.d -f oem-config remove >/dev/null 2>&1
		exit 0
	fi
done

# 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 >/dev/null 2>&1

reboot
