#!/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='
'

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)
    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:+ \\${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/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 \\
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"
        db_get ddclient/run_dhclient
        run_dhclient="$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 DHCP client ('dhclient'
# from package isc-dhcp-client) updates the systems IP address.
run_dhclient="$run_dhclient"

# 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
      fi
    fi
    ;;
  abort-upgrade|abort-remove|abort-deconfigure)
    ;;
  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 0
    ;;
esac

db_get ddclient/run_daemon
if [ "$RET" = true ]; 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

db_stop

exit 0
