#!/bin/sh -e

# Source debconf library.
. /usr/share/debconf/confmodule
db_version 2.0

# This conf script is capable of backing up
db_capb backup

create_maas_server_choices () {
	file=$1
	local choices=
	if [ -e "$file" ]; then
		OIFS="$IFS"
		IFS="|"
		while read description host_ip other;
		do
			choices="${choices:+$choices, }$description - ($host_ip)"
		done < "$file"
		IFS="$OIFS"
	fi
	choices="${choices:+$choices, }Specify MAAS by name or address, Create a new MAAS on this server"
	if [ -n "$choices" ]; then
		db_subst maas-enlist/select-maas-server CHOICES "$choices"
	fi
}

create_maas_server_default_choice () {
	choice="Specify MAAS by name or address"
	db_subst maas-enlist/select-maas-server CHOICES "$choice"
	db_set maas-enlist/select-maas-server "$choice"
}

set_question() {
	if ! db_fget "$1" seen; then
		db_register debian-installer/dummy "$1"
		db_subst "$1" ID "$1"
		db_fget "$1" seen
	fi
	if [ "$RET" = false ]; then
		db_set "$1" "$2"
		db_fset "$1" seen true
	fi
}

# Show the about/warning note
#db_input high maas-enlist/warning-note || true
#db_go

db_input low maas-enlist/skip-maas-discover || true
db_go
db_get maas-enlist/skip-maas-discover
if [ "$RET" != "true" ]; then
	if [ -x /usr/bin/maas-avahi-discover ]; then
		tempfile=$(mktemp)
		/usr/bin/maas-avahi-discover > "$tempfile"
		create_maas_server_choices "$tempfile"
	fi
	db_input high maas-enlist/select-maas-server
	db_go
else
	create_maas_server_default_choice
fi

db_get maas-enlist/select-maas-server
maas_server="$RET"

if [ "$maas_server" = "Create a new MAAS on this server" ]; then
	db_set pkgsel/include maas
	# We don't need to show tasksel, lets preseed.
	set_question tasksel/first "standard, server"
	exit 0
fi

STATE=1 
while [ "$STATE" != 0 -a "$STATE" != 3 ]; do
    case "$STATE" in
    1)
	if [ "$maas_server" != "Specify MAAS by name or address" ]; then
		serverurl=$(echo "$maas_server" | cut -d"(" -f2 | cut -d")" -f1)
	else
		db_input critical maas-enlist/maas-server-address || true
		db_go
	        db_get maas-enlist/maas-server-address
		serverurl="$RET"
	fi
    ;;

    2)
	# Try to obtain hostname that's been set on installer
	db_get netcfg/get_hostname || true
	netcfg_host="$RET"
	if [ "$netcfg_host" = "ubuntu" ] || [ "$netcfg_host" = "maas-enlist" ] || [ -z "$netcfg_host" ]; then
		# if netcfg_hostname is 'ubuntu', then request one.
		db_input critical maas-enlist/host-name || true
		db_go
	else
		# if netcfg_hostname has been changed to something differently.
		db_set maas-enlist/host-name "$netcfg_host"
	fi
	db_get maas-enlist/host-name
	hostname="$RET"
    ;;

# Disabled, when re-enabling bump while loop to 4, add template.
#    4)
#	db_input high maas-enlist/host-netif || true
#	db_get maas-enlist/host-netif
#	hostnetif=$RET
#    ;;

    esac

    if db_go; then
        STATE=$(($STATE + 1))
    else
        STATE=$(($STATE - 1))
    fi
done


maas-enlist --serverurl="$serverurl" \
              --hostname="$hostname" \
              || true
#              --netif="$hostnetif"

# Power off machine, we don't want to continue the install
db_set debian-installer/exit/poweroff true
/lib/debian-installer/exit
