#!/bin/bash

set +o histexpand

shopt -s sourcepath

. paths.sh
. common.sh
. config.sh
. sysinv.sh
. sysdef_setup.sh
. disk_setup.sh
. net_setup.sh
. swset_setup.sh

readonly BASENAME=`basename $0`
readonly VERSION="0.1"

readonly THIS_SHELL=`ps -p$$ -o cmd= 2>/dev/null`

readonly HELP_SETUP="setup.hlp"


usage()
{
	cat <<_EOF

Usage: $BASENAME <OPTIONS>

Run guided setup for installing Gomtuu Linux on an Alpha system. The script
creates a configuration file which is used by the subsequent Install program
which performs the actual installation.

OPTIONS

  -a        skip setup if config file exists (auto-continue) and return success
            immediately; the Install program is not automatically run
  -D        show debug messages
  -h        show this help and exit
  -V        show version and exit
_EOF

	exit 1
}

version()
{
	cat <<_EOF
$BASENAME $VERSION
_EOF

	exit 1
}

welcome()
{
	mhdr "GOMTUU SETUP"

	cat <<_EOF

Welcome to the Gomtuu Setup program. This program will guide you through the
setup procedure to install a Linux system on your awesome Alpha machine.

The Setup program generates a configuration file which is subsequently used
by the Install program. In it self, the Setup program does not install any
software nor partitions any disks. Default values for the configuration file
are automatically filled in by the Setup program. These values are based on
your system, but can be modified if so desired.






_EOF

	wait_for_keypress
	clear
}

invoke_shell()
{
	local alt_sh="ksh tcsh"
	local shells=""
	local sh=""
	local -i ret=1

	shells="$THIS_SHELL $alt_sh sh"
	for sh in $shells; do
		type $sh >/dev/null 2>&1
		if test $? -ne 0; then
			continue
		fi
		#
		#
		env $sh
		if test $? -lt 124 -o $? -gt 127; then
			ret=0
			break
		fi
	done

	if test $ret -eq 1; then
		msg "Could not start shell interpreter."
	fi
	return $ret
}

setup_save()
{
	msg "Saving configuration in $CF"

	if test -r "$CF"; then
		mv -f "$CF" "$CF~"	# backup existing config
	fi

	write_config "$CF"
}

setup_list()
{
	sysinv_list
	sysdef_list
	disk_list "$INV_DISKS"
	net_list
	swset_list
}

setup_load()
{
	local cf=""

	cf=`input_field "Enter file" validate_pathname`
	if test $? -ne 0; then
		return
	fi

	clear_config
	read_config "$cf"
	if test $? -ne 0; then
		msg "Error reading configuration file."
	fi
}

setup_defaults()
{
	local failed=""
	local bootdev=""

	msg "WARNING: Loading default settings will overwrite current settings."
	confirm || return

	sysdef_defaults
	if test $? -ne 0; then
		failed="${failed} - System Definitions\n"
	fi

	bootdev=`sysinv_get_bootdev`
	partition_defaults "$bootdev"
	if test $? -ne 0; then
		failed="${failed} - Disk Layout\n"
	fi

	net_defaults
	if test $? -ne 0; then
		failed="${failed} - Network settings\n"
	fi

	swset_defaults
	if test $? -ne 0; then
		failed="${failed} - Software Sets\n"
	fi

	if test -n "$failed"; then
		msg ""
		msg "Failed setting defaults. The following categories failed:"
		msg ""
		msg "$failed"
	fi
}

setup_clear()
{
	confirm "Erase current configuration?"
	if test $? -eq 0; then
		clear_config
	fi
}

setup()
{
	local old_PS3="$PS3"
	local item=""
	local -i done=0
	local -i ret=0

	sysinv

	PS3="[Enter number] "
	while test $done -eq 0; do
		mhdr "MAIN SETUP"

		select item in "Shell" "Help" "Back" "Save" "List" \
				"System Definition" "Disk Layout" "Network" \
				"Software Sets" "Rescan System" "Load Config" \
				"Load Defaults" "Clear"
		do
			case "$item" in
			"Shell")             invoke_shell ;;
			"Help")              show_help "$HELP_SETUP" ;;
			"Back")              ret=1; done=1 ;;
			"Save")              setup_save; ret=$?; done=1 ;;
			"List")              setup_list ;;
			"System Definition") sysdef_setup ;;
			"Disk Layout")       disk_setup ;;
			"Network")           net_setup ;;
			"Software Sets")     swset_setup ;;
			"Rescan System")     sysinv ;;
			"Load Config")       setup_load ;;
			"Load Defaults")     setup_defaults ;;
			"Clear")             setup_clear ;;
			esac
			break
		done
	done

	PS3=$old_PS3

	return $ret
}


TARGET_SN=`sysinv_get_serial`
CF="$CONFIG_DIR/$CONFIG_FILE.${TARGET_SN:-default}"  # prefer S/N based config
AUTOCONT=0
DEBUG=0

while getopts :aDhV OPT
do
	case $OPT in
	a) AUTOCONT=1
	   ;;
	D) DEBUG=1
	   ;;
	h) usage
	   ;;
	V) version
	   ;;
	?) echo "$OPTARG -- unknown option" 2>&1
	   usage
	   ;;
	:) echo "$OPTARG -- missing argument" 2>&1
	   usage
	   ;;
	esac
done

if test $AUTOCONT -eq 1 -a -r "$CF"; then
	exit 0
fi

welcome
setup
