#!/bin/sh

set -eu

if [ "${ADDRFAM:-}" != 'inet' ]; then
  # we should support both "inet" and "inet6" but we do not want to run
  # this twice (separately for both)
  exit 0
fi

if [ "${IFACE:-}" = 'lo' -o "${IFACE:-}" = '--all' ]; then
  # skip these interfaces
  exit 0
fi

if [ "${PHASE:-}" != 'post-up' ]; then
  # run when interface gets up
  exit 0
fi

(
  sync_done=false

  sleep 2

  case "${IFACE:-}" in
    vpn*)
      ntp_server=$(awk -F= '$1 == "NTP" { print $2; exit(0) }' \
                     /etc/systemd/timesyncd.conf.d/puavo.conf)
      if [ -n "$ntp_server" ]; then
        if ntpdig -Ss -M 10000 "$ntp_server"; then
          sync_done=true
        fi
      fi
      ;;
  esac

  if ! $sync_done; then
    fallback_ntp_servers=$(awk -F= '$1 ~ /FallbackNTP/ { print $2; exit(0) }' \
                             /etc/systemd/timesyncd.conf)
    for ntp_server in $fallback_ntp_servers; do
      if ntpdig -Ss -M 10000 "$ntp_server"; then
        break
      fi
    done
  fi

  # this triggers a dbus call to systemd-timesyncd and also activates it
  timedatectl timesync-status
) &
