#!/usr/bin/bash


###
#
# Options Section
#
###

set -e
set -o pipefail


###
#
# Variable Section
#
###

source '/etc/ddns-client.conf'


###
#
# Function Section
#
###

function ip_check () {
  case "${IP}" in
    IP4)
      if [[ -z "${IP4_ADDRESS}" ]]; then
        /usr/bin/echo 'ERROR: The Identity Provider did not respond with an valid IP address.'
        exit 1
      fi
    ;;
    IP6)
      if [[ -z "${IP6_ADDRESS}" ]]; then
        /usr/bin/echo 'ERROR: The Identity Provider did not respond with an valid IP address.'
        exit 1
      fi
    ;;
    IP64)
      if [[ -z "${IP4_ADDRESS}" ]] || \
         [[ -z "${IP6_ADDRESS}" ]]; then
        /usr/bin/echo 'ERROR: The Identity Provider did not respond with an valid IP address.'
        exit 1
      fi
    ;;
  esac
}


###
#
# Runtime Environment
#
###

if [[ -n "${INVOCATION_ID}" ]]; then
  if ! source '/usr/lib/ddns-client/check_conditions'; then
    exit 1
  fi
fi
case "${NETWORK}" in
  private)
    if [[ ! -L "/sys/class/net/${INTERFACE}" ]]; then
      /usr/bin/echo 'ERROR: The options 'INTERFACE' in the configuration file is not existing.'
      exit 1
    fi
    case "${IP}" in
      IP4)
        if type ip >/dev/null 2>&1; then
          IP4_ADDRESS=$(/usr/bin/ip -4 addr show "${INTERFACE}" | /usr/bin/grep ---only-matching --perl-regexp '(?<=inet\s)\d+(\.\d+){3}')
        elif type ifconfig >/dev/null 2>&1; then
          IP4_ADDRESS=$(/usr/sbin/ifconfig "${INTERFACE}" | /usr/bin/grep 'inet ' | /usr/bin/mawk '{print $2}')
        fi
      ;;
      IP6)
        if type ip >/dev/null 2>&1; then
          IP6_ADDRESS=$(/usr/bin/ip -6 addr show "${INTERFACE}" | /usr/bin/grep 'inet6' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}' | /usr/bin/grep --extended-regexp '^fd[0-9a-fA-F]{2}::')
        elif type ifconfig >/dev/null 2>&1; then
          IP6_ADDRESS=$(/usr/sbin/ifconfig "${INTERFACE}" | /usr/bin/grep --extended-regexp 'inet6 (fd[0-9a-fA-F]{2}::|::1)' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}')
        fi
      ;;
      IP64)
        if type ip >/dev/null 2>&1; then
          IP4_ADDRESS=$(/usr/bin/ip -4 addr show "${INTERFACE}" | /usr/bin/grep ---only-matching --perl-regexp '(?<=inet\s)\d+(\.\d+){3}')
          IP6_ADDRESS=$(/usr/bin/ip -6 addr show "${INTERFACE}" | /usr/bin/grep 'inet6' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}' | /usr/bin/grep --extended-regexp '^fd[0-9a-fA-F]{2}::')
        elif type ifconfig >/dev/null 2>&1; then
          IP4_ADDRESS=$(/usr/sbin/ifconfig "${INTERFACE}" | /usr/bin/grep 'inet ' | /usr/bin/mawk '{print $2}')
          IP6_ADDRESS=$(/usr/sbin/ifconfig "${INTERFACE}" | /usr/bin/grep --extended-regexp 'inet6 (fd[0-9a-fA-F]{2}::|::1)' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}')
        fi
    esac
    ip_check
  ;;
  public)
    case "${IP}" in
      IP4)
        IP4_ADDRESS=$(/usr/bin/wget --quiet --output-document='-' --timeout='3' --inet4-only --https-only --no-hsts 'ident.me')
      ;;
      IP6)
        if type ip >/dev/null 2>&1; then
          IP6_ADDRESS=$(/usr/bin/ip -6 addr show "${INTERFACE}" | /usr/bin/grep 'inet6' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}' | /usr/bin/grep --invert-match --extended-regexp '^(fd[0-9a-fA-F]{2}::|fe[0-9a-fA-F]{2}::)')
        elif type ifconfig >/dev/null 2>&1; then
          IP6_ADDRESS=$(/usr/sbin/ifconfig "${INTERFACE}" | /usr/bin/grep 'inet6' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}' | /usr/bin/grep --invert-match --extended-regexp '^(fd[0-9a-fA-F]{2}::|fe[0-9a-fA-F]{2}::)')
        fi
      ;;
      IP64)
        IP4_ADDRESS=$(/usr/bin/wget --quiet --output-document='-' --timeout='3' --inet4-only --https-only --no-hsts 'ident.me')
        if type ip >/dev/null 2>&1; then
          IP6_ADDRESS=$(/usr/bin/ip -6 addr show "${INTERFACE}" | /usr/bin/grep 'inet6' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}' | /usr/bin/grep --invert-match --extended-regexp '^(fd[0-9a-fA-F]{2}::|fe[0-9a-fA-F]{2}::)')
        elif type ifconfig >/dev/null 2>&1; then
          IP6_ADDRESS=$(/usr/sbin/ifconfig "${INTERFACE}" | /usr/bin/grep 'inet6' | /usr/bin/mawk '{split($2,a,"/"); print a[1]}' | /usr/bin/grep --invert-match --extended-regexp '^(fd[0-9a-fA-F]{2}::|fe[0-9a-fA-F]{2}::)')
        fi
      ;;
    esac
    ip_check
  ;;
esac
for HOST in ${HOSTNAME}; do
  case "${PROVIDER}" in
    webstuebchen)
      URLIP4="https://dns.webstuebchen.com/${USERNAME}/update.php?user=${USERNAME}&password=${PASSWORD}&ipv4=${IP4_ADDRESS}&domain=${HOST}"
      URLIP6="https://dns.webstuebchen.com/${USERNAME}/update.php?user=${USERNAME}&password=${PASSWORD}&ipv6=${IP6_ADDRESS}&domain=${HOST}"
      URLIP64="https://dns.webstuebchen.com/${USERNAME}/update.php?user=${USERNAME}&password=${PASSWORD}&ipv4=${IP4_ADDRESS}&ipv6=${IP6_ADDRESS}&domain=${HOST}"
    ;;
  esac
  case "${IP}" in
    IP4)
      if /usr/bin/wget --quiet --output-document='-' --no-hsts "${URLIP4}"; then
        /usr/bin/echo "The DNS [A] Record named '${HOST}' was updated to '${IP4_ADDRESS}'."
      else
        /usr/bin/echo "ERROR: The DNS [A] Record named '${HOST}' can not be updated."
        exit 1
      fi
    ;;
    IP6)
      if /usr/bin/wget --quiet --output-document='-' --no-hsts "${URLIP6}"; then
        /usr/bin/echo "The DNS [AAAA] Record named '${HOST}' was updated to '${IP6_ADDRESS}'."
      else
        /usr/bin/echo "ERROR: The DNS [AAAA] Record named '${HOST}' can not be updated."
        exit 1
      fi
    ;;
    IP64)
      if /usr/bin/wget --quiet --output-document='-' --no-hsts "${URLIP4}"; then
        /usr/bin/echo "The DNS [A] Record named '${HOST}' was updated to '${IP4_ADDRESS}'."
      else
        /usr/bin/echo "ERROR: The DNS [A] Record named '${HOST}' can not be updated."
        exit 1
      fi
      if /usr/bin/wget --quiet --output-document='-' --no-hsts "${URLIP6}"; then
        /usr/bin/echo "The DNS [AAAA] Record named '${HOST}' was updated to '${IP6_ADDRESS}'."
      else
        /usr/bin/echo "ERROR: The DNS [AAAA] Record named '${HOST}' can not be updated."
        exit 1
      fi
    ;;
  esac
done
