#!/bin/bash -e
# Finish up the install, clean up anything necessary, and get the user to a
# login prompt.

. /usr/share/debconf/confmodule

db_capb backup

if [ "$1" = new ]; then
	# If a display manager is installed, ask about starting it.
	SERVICES=""
	for service in xdm gdm kdm; do
		if test -x /etc/init.d/$service; then
			SERVICES="$service $SERVICES"
		fi
	done
	if [ "$SERVICES" ]; then
		db_fset base-config/start-display-manager seen false
		db_input low base-config/start-display-manager || true
		db_go || exit 30 # back up to menu
	fi
	
	# Display the final message.
	db_fset base-config/login seen false
	db_input high base-config/login || true
	db_go || exit 30 # back up to menu TODO should use state machine
	
	# Turn on console screen blanking again
	if which setterm >/dev/null 2>&1; then
		# It seem to be impossible to get the current timeout
		# value before changing it, so the only way to
		# undo the setting is to set the timeout period to some
		# random value. 10 minutes sounds like a good value.
		setterm -blank 10 </dev/tty >/dev/tty
	fi

	# s/390 has this weird system where you telnet in to run base-config.
	# Now that that's done, clean up after the root telnet hacks the s390
	# installer put in place.
	machine=`uname -m`
	if [ $machine = s390 ]; then
		# Reenable security for s390 now that installation has
		# been finished. Drop base-config call from /root/.profile
		sed -e 's/#password   required   pam_unix.so nullok obscure min=4 max=8/password   required   pam_unix.so nullok obscure min=4 max=8/' </etc/pam.d/passwd >/etc/pam.d/passwd.tmp
		mv -f /etc/pam.d/passwd.tmp /etc/pam.d/passwd
		sed -e '/pts\//d' </etc/securetty >/etc/securetty.tmp
		mv -f /etc/securetty.tmp /etc/securetty
		sed -e '/\/usr\/sbin\/base-config/d' </root/.profile >/root/.profile.tmp
		mv -f /root/.profile.tmp /root/.profile
	fi
	
	# Start the display manager
	db_get base-config/start-display-manager
	if [ "$RET" = true ]; then
		for service in $SERVICES ; do
			# Try to avoid trouble with debconf.
			/etc/init.d/$service restart </dev/tty >/dev/tty 3>/dev/null || true
		done
	fi

	# Finally, put the real inittab into place and tell init.
	if [ -f /etc/inittab.real ]; then
		mv -f /etc/inittab.real /etc/inittab
		telinit q >/dev/null
		rm -f /root/dbootstrap_settings # now that we're done with it
		# Clear the screen, in preparation for the login prompt
		clear </dev/tty >/dev/tty
	fi
fi
