#!/bin/bash

# Source common settings
. /etc/owl-setup.conf

NETCFG=$HOME/netcfg
MKFSTAB=$HOME/mkfstab

tempfile=''
f_keytable=''
f_liloconf=''
trap 'rm -f $tempfile $f_keytable $f_liloconf' EXIT HUP INT TERM
tempfile=`mktemp /var/run/owl-setup.XXXXXX` || exit 1
f_keytable=`mktemp $F_KEYTABLE.XXXXXX` || exit 1
f_liloconf=`mktemp $F_LILOCONF.XXXXXX` || exit 1

##
# Set root password
#
##
set_root_password()
{
	local SERVICE

	if [ -f /etc/pam.d/setup ]; then
		SERVICE=/etc/pam.d/setup
	else
		SERVICE=/etc/pam.d/passwd
	fi

	while :; do
		$PASSWD -e "$DIALOG --clear --backtitle '$TITLE' --passwordbox \"\$PAM_MSG\" 0 0" \
			-E "$DIALOG --cr-wrap --clear --backtitle '$TITLE' --inputbox \"\$PAM_MSG\" 0 0" \
			-m "$DIALOG --cr-wrap --clear --backtitle '$TITLE' --msgbox \"\$PAM_MSG\" 0 0" \
			-u root \
			-s "$SERVICE"

		if [ $? -eq 1 ]; then
			$DIALOG --backtitle "$TITLE" \
				--clear \
				--yesno 'Continue without setting a root password?' 0 0

			if [ $? -eq 1 ] ; then
				continue
			else
				break
			fi
		else
			break
		fi
	done
}

##
# Set timezone
#
##

# Return $continent
select_timezone()
{
	$DIALOG --backtitle "$TITLE" \
		--title 'Set timezone' \
		--clear \
		--menu "Please select the geographic area in which you live:" 0 0 0 \
		'Africa'    'Africa' \
		'America'   'America' \
		'US'        'US time zones' \
		'Canada'    'Canada time zones' \
		'Asia'      'Asia' \
		'Atlantic'  'Atlantic Ocean'  \
		'Australia' 'Australia' \
		'Europe'    'Europe' \
		'Indian'    'Indian Ocean'  \
		'Pacific'   'Pacific Ocean' \
		'SystemV'   'Use System V style time zones' \
		'Etc'       'None of the above' \
		2> $tempfile || return

	continent="`cat $tempfile`"

	find $TZ_BASE -type f | sed -e "s,^$TZ_BASE/,," | sort > $tempfile

	local zlist=''
	for i in `grep -i "^$continent/" $tempfile | cut -d / -f2-`; do
		zlist="$zlist $i '' "
	done

	eval "\$DIALOG --backtitle \"\$TITLE\" \
		--title 'Set timezone' \
		--clear \
		--menu \"Please select the name of one of these cities or zones\" 0 0 15 \
		$zlist \
		2> $tempfile" || return

	zone="`cat $tempfile`"
	timezone="$continent/$zone"
	zone_info

	$DIALOG --backtitle "$TITLE" \
		--cr-wrap \
		--clear \
		--title 'Set timezone' \
		--yesno "Set this timezone?\n$extra_info" 0 0 \
		2> $tempfile || return

	if [ -L $LOCALTIME ]; then
		ln -sf $TZ_BASE/$timezone $LOCALTIME
	else
		cp $TZ_BASE/$timezone $LOCALTIME
	fi

	local UTC='false'
	local ARC='false'
	$DIALOG --backtitle "$TITLE" \
		--title 'Set timezone' \
		--clear \
		--yesno "Hardware clock set to UTC?" 0 0 \
		2> $tempfile

	if [ $? -eq 0 ]; then
		UTC='true'
	fi

	echo "UTC=$UTC" > $SYSCFG_CLOCK
	echo "ARC=$ARC" >> $SYSCFG_CLOCK
	echo "ZONE=$timezone" >> $SYSCFG_CLOCK
}

zone_info()
{
	extra_info=
	for i in 1 2 3 4 5 6 7 8
	do
		TZdate=$(LANG=C TZ="$timezone" date)
		UTdate=$(LANG=C TZ=UTC0 date)
		TZsec=$(expr "$TZdate" : '.*:\([0-5][0-9]\)')
		UTsec=$(expr "$UTdate" : '.*:\([0-5][0-9]\)')
		case $TZsec in
			$UTsec)
				extra_info="
Local time is now:      $TZdate.
Universal Time is now:  $UTdate."
				break
			;;
		esac
	done
}

##
# Set keyboard layout
#
##
select_keyboard()
{
	case "`uname -m`" in
		sparc*)
        		maps_path=/lib/kbd/keymaps/sun
        	;;
		#*86|alpha)
		*)
			maps_path=/lib/kbd/keymaps/i386
        	;;
	esac

	find $maps_path -type f -printf '%f\n' | sed -e "s/\([^\.]*\).*/\1 ''/"  | sort -u > $tempfile

	local klist=`cat $tempfile`

	eval "\$DIALOG --backtitle \"\$TITLE\" \
		--title 'Set keyboard layout' \
		--clear \
		--menu \"Please select your keyboard layout:\" 0 0 15 \
		"$klist" \
		2> $tempfile" || return

	layout="`cat $tempfile`"

	$DIALOG --backtitle "$TITLE" \
		--cr-wrap \
		--clear \
		--title 'Set keyboard layout' \
		--yesno "Set this layout?\n$layout" 0 0 \
		2> $tempfile || return

	echo "KEYTABLE=$layout" > $f_keytable
	chmod 644 $f_keytable
	mv -f $f_keytable $F_KEYTABLE

	# Set keyboard layout for setup
	$LOADKEYS $layout
}

##
# LILO boot loader configuration
#
##
lilo_config()
{
	local root
	if [ -f "$FSTAB" ]; then
		root="`grep -v ^# "$FSTAB" |
				while read dev fs other ; do
					if [ "$fs" = '/' ]; then
						echo -n "$dev"
						break
					fi
				done`"
	fi

	boot="`echo "$root" | sed -e 's/[0-9]*$//'`"

	$DIALOG --backtitle "$TITLE" \
		--cr-wrap \
		--clear \
		--inputbox "Boot device:" 0 0 "$boot" \
		2> $tempfile || return

	boot="`cat $tempfile`"

	echo "\
boot=$boot

image=/boot/bzImage
	label=Linux
	read-only
	root=$root" > $f_liloconf

	chmod 600 $f_liloconf
	mv -f $f_liloconf $F_LILOCONF

	local msg="Please place your kernel image in /boot/bzImage of
your newly installed system, check out its /etc/lilo.conf
and lilo.conf(5) manual page. Invoke /sbin/lilo to actually
install LILO."

	$DIALOG --backtitle "$TITLE" \
		--cr-wrap \
		--clear \
		--msgbox "$msg" 0 0
}

##
# Internal functions
#
##

# additem menu Item 'Description'
additem()
{
	eval "$1=\"\$$1 '$2' '$3' \""
}

##
# Main Menu
#
##

main_menu()
{
	local menu

	additem menu Keyboard 'Select keyboard layout'
	additem menu Password 'Set root password'
	additem menu fstab    'Filesystems table'
	additem menu Timezone 'Set timezone'
	additem menu Network  'Network configuration'

	case "`uname -m`" in
		*86)
			additem menu Boot 'LILO boot loader configuration'
        ;;
	esac

	additem menu Exit 'Exit the setup program'

	eval "$DIALOG --backtitle '$TITLE' --clear --menu 'Select the task:' 0 0 0" \
		"$menu" 2> $tempfile

	[ $? -eq 0 ] || return 1

	TASK="`cat $tempfile`"
	return 0
}

##
#   MAIN
##

while main_menu; do
	case "$TASK" in

		Keyboard)
			select_keyboard
		;;

		Password)
			set_root_password
		;;

		fstab)
			$MKFSTAB
		;;

		Timezone)
			select_timezone
		;;

		Network)
			$NETCFG
		;;

		Boot)
			lilo_config
		;;

		Exit)
			break
		;;
	esac
done
