# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/blindcoder/ezipupdate/mod_ezipupdate.sh
# ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---
#
# [MAIN] 60 ezipupdate EZ-Ipupdate Configuration (dyndns et al)

ezipupdate_new_step3 () {
	cmd=""
	iface="$1"
	tos="$2"
	gui_input "Please enter the dns name you want to update" "" hostname
	gui_input "Please enter the username for $hostname" "" user
	gui_input "Please enter the password for $hostname" "" pass
	gui_input "Please enter the MX record for $hostname" "$hostname" mx
	gui_input "Please enter the number of times the update should be tried" 3 retries
	gui_input "Please enter the mail-address errors should be reported to" "root@localhost" errorsto
	gui_input "Please enter the period to check if the IP-address changed in seconds" 1800 checkperiod
	gui_yesno "Do you want wildcard dnsnames? (*.$hostname would all resolve to your current IP-Address)"
	wildcard=$?
	cmd="-e 's,INTERFACE,$iface,g'"
	cmd="$cmd -e 's,SERVICETYPE,$tos,g'"
	cmd="$cmd -e 's,HOSTNAME,$hostname,g'"
	cmd="$cmd -e 's,USER,$user,g'"
	cmd="$cmd -e 's,PASS,$pass,g'"
	cmd="$cmd -e 's,MXADDRESS,$mx,g'"
	cmd="$cmd -e 's,RETRIES,$retries,g'"
	cmd="$cmd -e 's,EMAILADDRESS,$errorsto,g'"
	cmd="$cmd -e 's,PERIOD,$checkperiod,g'"
	[ $wildcard -eq 1 ] && cmd="$cmd -e 's,wildcard,# nowildcard,g'"
	eval "sed $cmd /etc/ezipupdate/stone.conf.template > /etc/ezipupdate/stone.$iface"
}

ezipupdate_new_step2 () {
	cmd=""
	iface="$1"
	for x in ezip pgpow dhs dyndns dyndns-static dyndns-custom ods tzo \
		easydns easydns-partner gnudip justlinux dyns hn zoneedit \
		heipv6tb ; do
		cmd="$cmd '$x' 'ezipupdate_new_step3 $iface $x'"
	done
	eval "gui_menu ezipupdate_new_step2 'Please select the type of service' $cmd"
}

ezipupdate_new_step1 () {
	cmd=""
	while read iface ; do
		[ -f /etc/ezipupdate/stone.$iface ] && continue
		cmd="$cmd '$iface' 'ezipupdate_new_step2 $iface'"
	done < <( grep interface /etc/network/config | cut -f 2 -d\ | cut -f1 -d\( | sort )
	eval "gui_menu ezipupdate_new_step1 'Please select the interface to use' $cmd"
}

ezipupdate_change_tos () {
	cmd=""
	for x in ezip pgpow dhs dyndns dyndns-static dyndns-custom ods tzo \
		easydns easydns-partner gnudip justlinux dyns hn zoneedit \
		heipv6tb ; do
		cmd="$cmd '$x' 'tos=$x'"
	done
	eval "gui_menu ezipupdate_change_tos 'Please select the new type of service' $cmd"
}

ezipupdate_change_wildcard () {
	gui_yesno "Do you want wildcard dnsnames? (*.$hostname would all resolve to your current IP-Address)"
	case "$?" in
		1) wildcard=0 ; ;;
		0) wildcard=1 ; ;;
	esac
}

ezipdate_change_iface () {
	cmd=""
	while read i ; do
		[ -f /etc/ezipupdate/stone.$i -a $i != $oldiface ] && continue
		cmd="$cmd '$i' 'iface=$i'"
	done < <( grep interface /etc/network/config | cut -f 2 -d\ | cut -f1 -d\( | sort )
	eval "gui_menu ezipupdate_change_iface 'Please select the new interface to use' $cmd"
}
ezipupdate_edit () {
	iface=$1
	oldiface=$1
	conf=/etc/ezipupdate/stone.$iface
	tos="`grep ^service-type $conf | cut -f2 -d=`"
	hostname="`grep ^host $conf | cut -f2 -d=`"
	IFS=":" read user pass < <( grep ^user $conf | cut -f2 -d= )
	mx="`grep ^mx $conf | cut -f2 -d=`"
	retries="`grep ^retrys $conf | cut -f2 -d=`"
	errorsto="`grep ^notify-email $conf | cut -f2 -d=`"
	checkperiod="`grep ^period $conf | cut -f2 -d=`"
	wildcard=`grep -c ^wildcard $conf`

	run=0
	while [ $run -eq 0 ] ; do 
		yesno="yes"
		[ $wildcard -eq 0 ] && yesno="no"
		gui_menu "ezipupdate_edit" "EZ-IPUpdate settings for $iface" \
			 "Type of service: $tos" "ezipupdate_change_tos" \
			 "Interface to check: $iface" "ezipdate_change_iface" \
			 "Hostname: $hostname" "gui_input 'Enter new hostname' '$hostname' hostname" \
			 "Username: $user" "gui_input 'Enter new username' '$user' user" \
			 "Password: $pass" "gui_input 'Enter new password' '$pass' pass" \
			 "MX-Record: $mx" "gui_input 'Enter new MX-Record' '$mx' mx" \
			 "Retries: $retries" "gui_input 'Enter new number of retries' '$retries' retries" \
			 "Report errors to: $errorsto" "gui_input 'Enter new mail address to report errors to' '$errorsto' errorsto" \
			 "Checking period in seconds: $checkperiod" "gui_input 'Enter new period to check if IP-address changed in seconds' '$checkperiod' checkperiod" \
			 "Wildcard DNS: $yesno" "ezipupdate_change_wildcard"
		run=$?
	done
	rm -f /etc/ezipupdate/stone.$oldiface
	cmd="-e 's,INTERFACE,$iface,g'"
	cmd="$cmd -e 's,SERVICETYPE,$tos,g'"
	cmd="$cmd -e 's,HOSTNAME,$hostname,g'"
	cmd="$cmd -e 's,USER,$user,g'"
	cmd="$cmd -e 's,PASS,$pass,g'"
	cmd="$cmd -e 's,MXADDRESS,$mx,g'"
	cmd="$cmd -e 's,RETRIES,$retries,g'"
	cmd="$cmd -e 's,EMAILADDRESS,$errorsto,g'"
	cmd="$cmd -e 's,PERIOD,$checkperiod,g'"
	[ $wildcard -eq 0 ] && cmd="$cmd -e 's,wildcard,# nowildcard,g'"
	eval "sed $cmd /etc/ezipupdate/stone.conf.template > /etc/ezipupdate/stone.$iface"
}

main() {
	run=0
	while [ $run -eq 0 ] ; do
		cmd=""
		for conf in /etc/ezipupdate/stone.* ; do
			[ -f $conf ] || continue
			[ "$conf" == "/etc/ezipupdate/stone.conf.template" ] && continue
			interface="`grep 'interface=' $conf | cut -f2 -d=`"
			if [ -z "$interface" ] ; then
				gui_message "Bogus interface configuration: $interface! Please edit $conf manually!"
				continue
			fi
			cmd="$cmd 'Edit interface $interface' 'ezipupdate_edit $interface'"
		done
		cmd="$cmd 'Add new interface' 'ezipupdate_new_step1'"
		eval "gui_menu ezipupdate 'EZ-Ipupdate Configuration' $cmd"
		run=$?
	done
}

