#!/usr/bin/bash


###
#
# Options Section
#
###

set -e
set -u
set -o pipefail


###
#
# Variable Section
#
###

source '/etc/ddns-client.conf'


###
#
# Runtime Environment
#
###

if [[ "${TIMER}" != 'true' ]]; then
  exit 1
fi
if [[ -z "${HOSTNAME}" ]]; then
  /usr/bin/echo "ERROR: The configuration option 'HOSTNAME' has not been set."
  exit 1
fi
if ! /usr/bin/ping -c '1' 'ident.me' &> '/dev/null'; then
  /usr/bin/echo "ERROR: The Identity Provider 'ident.me' is not reachable."
  exit 1
fi
case "${PROVIDER}" in
  webstuebchen)
    if ! /usr/bin/ping -c '1' 'dns.webstuebchen.com' &> '/dev/null'; then
      /usr/bin/echo "ERROR: The DNS Provider 'webstuenchen' is not reachable."
      exit 1
    fi
    if [[ -z "${USERNAME}" ]]; then
      /usr/bin/echo "ERROR: The configuration option 'USERNAME' has not been set."
      exit 1
    fi
    if [[ -z "${PASSWORD}" ]]; then
      /usr/bin/echo "ERROR: The configuration option 'PASSWORD' has not been set."
      exit 1
    fi
  ;;
  *)
    /usr/bin/echo "Error: The configuration option 'PROVIDER' has a wrong value."
    exit 1
  ;;
esac
case "${IP}" in
  IP4|IP6|IP64)
    true
  ;;
  *)
    /usr/bin/echo "ERROR: The configuration option 'IP' has a wrong value."
    exit 1
  ;;
esac
case "${NETWORK}" in
  private)
    if [[ -z "${INTERFACE}" ]]; then
      /usr/bin/echo 'ERROR: The configuration option 'INTERFACE' has not been set.'
      exit 1
    fi
  ;;
  public)
    true
  ;;
  *)
    /usr/bin/echo "ERROR: The configuration option 'NETWORK' has a wrong value."
    exit 1
  ;;
esac
