#! /bin/sh

set -e

#SCRIPTSCOMMON#

case "$1" in
configure)
	
  load_debconf2 "$@"

  if [ ! -f /etc/ddclient.conf ]; then
    # get the values from the debconf database
    db_get ddclient/protocol 		&& protocol="$RET"
    db_get ddclient/server   		&& server="$RET"
    db_get ddclient/names    		&& names="$RET"
    db_get ddclient/username 		&& username="$RET"
    db_get ddclient/password \
      && password=$(echo "$RET"|sed -e "s/'/\\\\'/g")
    db_get ddclient/interface
	if [ "$RET" ]; then
		interface="use=if, if=$RET"
	else
		db_get ddclient/checkip
		if [ "$RET" = "true" ]; then
			interface="use=web, web=checkip.dyndns.com, web-skip='IP Address'"
		fi
	fi

    # create configuration file /etc/ddclient.conf
    config=`mktemp /etc/ddclient.conf.XXXXXX`
    trap "rm -f $config; exit 1" HUP INT QUIT TERM
    password_quoted=`quote_backslashes "$password"`
    cat <<EOF >>"$config"
# Configuration file for ddclient generated by debconf
#
# /etc/ddclient.conf

protocol=$protocol
$interface
server=$server
login=$username
password='$password_quoted'
$names
EOF
    mv $config /etc/ddclient.conf


    # create /etc/default/ddclient if it does not exist
    if [ ! -f /etc/default/ddclient ]; then
      db_get ddclient/run_daemon			&& run_daemon="$RET"
      db_get ddclient/daemon_interval		&& daemon_interval="$RET"
      db_get ddclient/run_ipup				&& run_ipup="$RET"
      temp=`mktemp /etc/default/ddclient.XXXXXX`
      trap "rm -f $temp; exit 1" HUP INT QUIT TERM
      cat <<EOF >"$temp"
# Configuration for ddclient scripts 
# generated from debconf on $(date)
#
# /etc/default/ddclient

# Set to "true" if ddclient should be run every time a new ppp connection is 
# established. This might be useful, if you are using dial-on-demand.
run_ipup="$run_ipup"

# Set to "true" if ddclient should run in daemon mode
# If this is changed to true, run_ipup must be set to false.
run_daemon="$run_daemon"

# Set the time interval between the updates of the dynamic DNS name in seconds.
# This option only takes effect if the ddclient runs in daemon mode.
daemon_interval="$daemon_interval"
EOF
      mv $temp /etc/default/ddclient
	

    fi
  fi

  db_stop

    ;;


    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

