#!/bin/bash
# console-config/src/sbin-scripts/net-setup
# 
#  Copyright: ©2012, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#
# Menu-based configuration for a static IP address scheme. Allows configuration
# of the IP address itself along with (optionally) a default route and primary
# and backup DNS servers.
#

if [ "$#" -ne 6 ]
then
	cat <<EOF
Expecting the following arguments:
	Name of device
	Path to result file
	Current static IP
	Current static default route
	Current static primary nameserver
	Current static secondary nameserver
EOF
	exit 1
fi

DEVICE="$1"
RESULT_FILE="$2"
STATIC_IP="$3"
STATIC_GATEWAY="$4"
STATIC_DNS0="$5"
STATIC_DNS1="$6"

DIALOG_RESULTFILE="`mktemp`"
trap "rm -f \"${DIALOG_RESULTFILE}\"" EXIT



# basic_verify_ip()
#  Test that the IP address in $1 is either null (which is accepted) or a
#  valid IP address. If it is invalid, displays a message box with the error
#  printed by net-setup-verify, and returns non-zero. Otherwise, returns 0.
#  If a dialog needs to be displayed, its title is taken from $2.
basic_verify_ip() {
	local CHECK="$1"
	local TITLE="$2"

	[ -z "${CHECK}" ] && return 0

	net-setup-verify --ip "${CHECK}" 2> "${DIALOG_RESULTFILE}"
	if [ $? -ne 0 ]
	then
		dialog --title "${TITLE}" \
			--msgbox \
			"`cat "${DIALOG_RESULTFILE}"`" \
			0 0
		return 1
	fi
}



#
# Main loop
#
#  Loop until the dialog is rejected (in which case we exit with a non-zero
#  code) or the results of the dialog are verified.
while true
do
	# display dialog with previously-inherited fields populated
	dialog --title "Configure static address for ${DEVICE}" \
		--form \
		"Enter IP address, gateway/router (if present), and DNS server
(if present). IP addresses are entered in CIDR notation, e.g.
x.y.z.w/24 . These correspond to netmasks as follows:
/16    255.255.0.0
/24    255.255.255.0
/28    255.255.255.240
/29    255.255.255.248
/30    255.255.255.252
etc." \
		0 0 0 \
		"IP address" 1 0 "${STATIC_IP}" 2 4 54 100 \
		"Gateway/router (if present)" 3 0 "${STATIC_GATEWAY}" 4 4 54 100 \
		"Primary DNS server (if known)" 5 0 "${STATIC_DNS0}" 6 4 54 100 \
		"Secondary DNS server (if known)" 7 0 "${STATIC_DNS1}" 8 4 54 100 \
		2> "${DIALOG_RESULTFILE}"

	if [ "$?" -ne 0 ]
	then
		clear
		echo "Cancelled."
		rm -f "${DIALOG_RESULTFILE}"
		exit 1
	fi
	{
		read -r STATIC_IP
		read -r STATIC_GATEWAY
		read -r STATIC_DNS0
		read -r STATIC_DNS1
	} < "${DIALOG_RESULTFILE}"
	rm -f "${DIALOG_RESULTFILE}"

	# verify the new fields
	if [ -z "${STATIC_IP}" ]
	then
		dialog --msgbox \
			"IP address required." \
			0 0
		continue
	else
		net-setup-verify --cidr "${STATIC_IP}" 2> "${DIALOG_RESULTFILE}"
		if [ $? -ne 0 ]
		then
			dialog --title "Invalid IP address" \
				--msgbox \
				"`cat "${DIALOG_RESULTFILE}"`" \
				0 0
			continue
		fi
	fi

	basic_verify_ip "${STATIC_GATEWAY}" "Invalid gateway/router" || continue
	basic_verify_ip "${STATIC_DNS0}" "Invalid primary DNS server" || continue
	basic_verify_ip "${STATIC_DNS1}" "Invalid secondary DNS server" || continue
	break
done



#
# Write out the results file
#
cat > "${RESULT_FILE}" <<EOF
${STATIC_IP}
${STATIC_GATEWAY}
${STATIC_DNS0}
${STATIC_DNS1}
EOF

if [ $? -ne 0 ]
then
	rm -f "${RESULT_FILE}"
	exit 1
fi

exit 0
