#!/bin/sh

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: y=`quote_backslashes "$x"`
# Quotes each backslash in $x with another backslash, outputs result.
quote_backslashes() {
  echo -n "$1" | sed 's,\\,\\\\,g'
}

newline='
'

run_as_daemon() {
  db_get ddclient/method
  [ "${RET}" != "Web-based IP discovery service" ] || return 0
  db_get ddclient/run_mode
  [ "${RET}" = "As a daemon" ]
}

write_ddclient_conf() {
  # get the values from the debconf database
  db_get ddclient/protocol
  protocol="$RET"
  db_get ddclient/server
  server=${RET:+ \\${newline}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/method
  case ${RET} in
    "Web-based IP discovery service")
      db_get ddclient/web-url
      [ -n "${RET}" ] || db_get ddclient/web
      use="web, web=${RET%% *}"
      ;;
    "Network interface")
      db_get ddclient/interface
      use="if, if=${RET}"
      ;;
    *) fatal "unsupported IP discovery method: ${RET}";;
  esac
  db_get ddclient/proxy
  [ -n "${RET}" ] && proxy="${newline}proxy=${RET}${newline}" || proxy=

  # 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
$proxy
protocol=$protocol \\
use=$use$server \\
login=$username \\
password='$password_quoted' \\
$names
EOF
  mv "$config" /etc/ddclient.conf
}

write_etc_default_ddclient() {
  db_get ddclient/daemon_interval
  daemon_interval="$RET"
  run_as_daemon && run_ipup=false || run_ipup=true
  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 DHCP client ('dhclient'
# from package isc-dhcp-client) updates the systems IP address.
run_dhclient="false"

# 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 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
}

case "$1" in
  # 'reconfigure' is here because of this comment from debconf-devel(7):
  #
  #     Make your postinst script accept a first parameter of
  #     "reconfigure". It can treat it just like "configure". This
  #     will be used in a later version of debconf to let postinsts
  #     know when they are reconfigured.
  configure|reconfigure)
    [ -f /etc/ddclient.conf ] || write_ddclient_conf
    [ -f /etc/default/ddclient ] || write_etc_default_ddclient
    ;;
  abort-upgrade|abort-remove|abort-deconfigure)
    ;;
  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 0
    ;;
esac

if run_as_daemon; then
  # We can't use the DEBHELPER magic comment to enable and start the
  # service because of the --no-enable --no-start options in
  # debian/rules. Instead we copy the code that would have been
  # generated had those options not been passed.
  case $1 in
    configure|abort-upgrade|abort-deconfigure|abort-remove)
      #### Begin 1st part generated by dh_installsystemd/13.1
      # This will only remove masks created by d-s-h on package removal.
      deb-systemd-helper unmask 'ddclient.service' >/dev/null || true

      # was-enabled defaults to true, so new installations run enable.
      if deb-systemd-helper --quiet was-enabled 'ddclient.service'; then
        # Enables the unit on first installation, creates new
        # symlinks on upgrades if the unit file has changed.
        deb-systemd-helper enable 'ddclient.service' >/dev/null || true
      else
        # Update the statefile to add new symlinks (if any), which need to be
        # cleaned up on purge. Also remove old symlinks.
        deb-systemd-helper update-state 'ddclient.service' >/dev/null || true
      fi
      #### End 1st part generated by dh_installsystemd/13.1
      #### Begin 2nd part generated by dh_installsystemd/13.1
      if [ -d /run/systemd/system ]; then
        systemctl --system daemon-reload >/dev/null || true
        deb-systemd-invoke start 'ddclient.service' >/dev/null || true
      fi
      #### End 2nd part generated by dh_installsystemd/13.1
      #### Begin part generated by dh_installinit/13.1
      if [ -x "/etc/init.d/ddclient" ]; then
        update-rc.d ddclient defaults >/dev/null
        invoke-rc.d ddclient start || exit 1
      fi
      #### End part generated by dh_installinit/13.1
      ;;
  esac
else
  # The following magic comment (see dh_installdeb(1)) will be
  # replaced with code that will unconditionally disable the service
  # due to the --no-enable --no-start options in debian/rules.
#DEBHELPER#
fi

# Configuration was successful so remove the password from debconf as
# recommended by debconf-devel(7).
db_set ddclient/password ""
db_fset ddclient/password seen false
db_set ddclient/password-repeat ""
db_fset ddclient/password-repeat seen false

db_stop

exit 0
