#!/bin/sh
## debconf config script for ddclient
##
## This script runs before copying the files of the package to the correct
## locations in the system.
##
## It gets the appropriate configuration values and stores them in the debconf
## database. If necessary it asks the user about some needed information.
##
## This file is human readable by using "grep '#' <thisfile>"
#

set -e

fatal() { printf %s\\n "$0: $*" >&2; exit 1; }

. /usr/share/debconf/confmodule
db_version 2.0 || fatal "need DebConf 2.0 or later"

# Usage: hosts=`download_hostlist USER PASSWORD`
# Retrieves the DynDNS hosts of given user
download_hostlist() {
  local username="$1" password="$2"
  # FIXME: protect against wget not installed
  wget "http://$username:$password@update.dyndns.com/text/gethostlist" -q -O -\
    | awk -F \: '{print $2, $4}' | sed -e "N;s/ \n/,/g" | sed -e "s/,/, /g"
}

# Retrieve the list of DynDNS hosts of the user's account from the DynDNS
# server and let the user select which hosts to update.
retrieve_dyndns_hostlist() {
  db_get ddclient/username && username="$RET"
  db_get ddclient/password && password="$RET"

  hostslist=`download_hostlist "$username" "$password"`

  # add the list to our multichoice template, then prompt the user
  db_subst ddclient/hostslist choices "$hostslist"
  db_input critical ddclient/hostslist || true
  db_go

  # set names using the host list to write it to the config file later
  db_get ddclient/hostslist
  hostslist=`echo "$RET" | sed -e "s/, /,/g"`
  db_set ddclient/names "$hostslist"

  # if the hostslist was blank, let the user know some possible reasons
  if [ -z "$hostslist" ]; then
    db_input high ddclient/blankhostslist || true
    db_go
  fi
}

#----------------------------------------------------------------------------
# create_new_config() - create a new config from scratch
#----------------------------------------------------------------------------
create_new_config()
{
  # get the service to use with ddclient
  db_input critical ddclient/service || true
  db_go
  db_get ddclient/service
  case "$RET" in
    no-ip.com)
      server=  # use the default server
      protocol=noip
      ;;
    freedns.afraid.org)
      protocol=freedns
      server=  # use the default server
      ;;
    duckdns.org)
      protocol=duckdns
      server=  # use the default server
      ;;
    domains.google)
      protocol=googledomains
      server=  # use the default server
      ;;
    www.dyndns.com)
      protocol=dyndns2
      server=members.dyndns.org
      ;;
    www.easydns.com)
      protocol=easydns
      server=members.easydns.com
      ;;
    www.dslreports.com)
      protocol=dslreports1
      server=www.dslreports.com
      ;;
    www.zoneedit.com)
      protocol=zoneedit1
      server=dynamic.zoneedit.com
      ;;
  esac
  if [ "$RET" != other ]; then
    # if we know the service we put in the protocol and server
    db_set ddclient/protocol "$protocol"
    db_fset ddclient/protocol seen true
    db_set ddclient/server "$server"
    db_fset ddclient/server seen true
  else
    # if not we ask the user about the protocol and server
    db_input critical ddclient/protocol || true
    db_input critical ddclient/server || true
    db_go
  fi

  # ask the user about the dynamic DNS names, account and interface
  db_input critical ddclient/username || true
  db_input critical ddclient/password || true
  db_input critical ddclient/password-repeat || true
  db_get ddclient/service
  # if the chosen service is dyndns, ask some extra questions.  otherwise, set them to false
  if [ "$RET" = "www.dyndns.com" ]; then
    db_input critical ddclient/checkip || true
    if [ -f /usr/bin/wget ]; then
      db_input critical ddclient/fetchhosts || true
    else
      db_fset ddclient/fetchhosts seen true
      db_set ddclient/fetchhosts Manually
    fi
  else
    db_fset ddclient/checkip seen true
    db_fset ddclient/fetchhosts seen true
    db_set ddclient/checkip false
    db_set ddclient/fetchhosts Manually
  fi
  db_go

  # Re-request passwords until they match
  while true; do
    db_get ddclient/password
    password="$RET"
    db_get ddclient/password-repeat
    if [ "$password" = "$RET" ]; then
      break
    fi
    db_fset ddclient/password seen false
    db_fset ddclient/password-repeat seen false
    db_fset ddclient/password-mismatch seen false
    db_input critical ddclient/password-mismatch || true
    db_input critical ddclient/password || true
    db_input critical ddclient/password-repeat || true
    db_go
  done

  db_set ddclient/password-repeat ""

  db_get ddclient/checkip
  # ask the traditional questions if the dyndns questions weren't answered true
  if [ "$RET" = "false" ]; then
    db_input critical ddclient/interface || true
  fi
  db_get ddclient/fetchhosts

  # check if we should fetch the hostlist for a dyndns account
  db_get ddclient/fetchhosts
  if [ "$RET" = "From list" ]; then
    retrieve_dyndns_hostlist
  else
    db_input critical ddclient/names || true
  fi
  db_go

  # set the default mode ddclient should run in (ip-up | daemon),
  # depending on the entered interface (XpppX or other)
  db_get ddclient/interface
  interface=$RET
  db_get ddclient/checkip
  checkip=$RET

  # if it is actually a ppp or related interface, and we're not using checkip
  if [ "$checkip" != "true"  ] && [ -z "${interface##*ppp*}" ]; then
    db_fget ddclient/run_ipup seen
    if [ "$RET" = "false" ]; then
      db_set ddclient/run_ipup true
    fi
  # if it is an interface of a different type
  else
    db_fget ddclient/run_daemon seen
    if [ "$RET" = "false" ]; then
      db_set ddclient/run_daemon true
    fi
  fi

  # maybe ask the user to override the default values
  db_input medium ddclient/run_ipup || true
  db_go

  # if using ipup dont use daemon, bug #462207
  db_get ddclient/run_ipup
  if [ "$RET" = "true" ]; then
    db_set ddclient/run_daemon false
  else
    db_input medium ddclient/run_daemon || true
    db_go
  fi

  # if ddclient should run in daemon mode we ask for the update interval
  db_get ddclient/run_daemon
  if [ "$RET" = "true" ]; then
    db_input medium ddclient/daemon_interval || true
    db_go
  fi
}

if [ "$1" = "configure" ]; then
  if [ ! -f /etc/ddclient.conf ]; then
    create_new_config
    exit 0
  fi
  exit 0
elif [ "$1" = "reconfigure" ]; then
  create_new_config
  rm -f /etc/ddclient.conf /etc/default/ddclient
fi
