#!/usr/bin/bash

# Main, main
cmd=`basename $0`
basedir=`dirname $0`
# Check if it is a soft link, and adjust the basedir
if [ -h $0 ]; then
  link_cmd=`readlink $0`
  link_basedir=`dirname $link_cmd`
  basedir=$basedir/$link_basedir 
fi

usage="Usage: $cmd <sync-method> [--ptp-if <if>] [--ptpmon-host] [--ptpmon-period] [--ntp-servers <servers>] [--help|-h] [--verbose|-v] [--debug|-d]"

if [ `whoami` != "root" ]; then
  echo "Error: You need to be root to run $cmd"
  echo $usage
  exit 1
fi

ptpmon_period=20
ntpmon_enable="false"
ntpmon_period=20

log_file="/var/log/puppet-timesync.log"
all_args=$*

POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -h|--help)
    echo $usage
    echo "    <sync-method> ntp|ptp"
    echo "    --help|-h       prints this help"
    echo "    --debug|-d      Puppet with --debug option"
    echo "    --noop          Dry-run - don't change anything"
    echo "    --ptp-if        PTP network interface"
    echo "    --ptpmon-host   PTP monitoring server"
    echo "    --ptpmon-period PTP monitoring period"
    echo "    --ntp-servers   NTP servers"
    echo "                    'HQ'  -> use default Garching NTP servers"
    echo "                    'PUB' -> use public NTP pool"
    echo "                    <servers>  -> comma separated list of NTP servers"
    echo ""
    echo "Examples:"
    echo ""
    echo "Configure PTP synchronization with network interface enp3s0f1"
    echo "$cmd ptp --ptp-if enp3s0f1"
    echo ""
    echo "Configure PTP synchronization with monitoring"
    echo "$cmd ptp --ptp-if ptp --ptpmon-host trshks-cnd.ecm.hq.eso.org --ptpmon-period 20"
    echo ""
    echo "Configure NTP synchronization for use at HQ"
    echo "$cmd ntp --ntp-servers HQ"
    echo ""
    echo "Configure NTP synchronization for use at HQ with monitoring"
    echo "$cmd ntp --ntp-servers HQ --ntpmon-enable --ntpmon-period 20"
    echo ""
    echo "Configure NTP synchronization with default options"
    echo "$cmd ntp --ntp-servers 'server1.somedomain,server2.somedomain'"
    echo ""
    echo "Configure NTP synchronization with custom options"
    echo "$cmd ntp --ntp-servers 'server1.somedomain iburst,server2.somedomain minpoll 5 maxpoll 9'"
    shift # past argument
    exit 0
    ;;
  --debug|-d)
    debug="--debug"
    shift
    ;;
  --ptp-if)
  ptp_interface=$2
  shift
  shift
  ;;
  --ptpmon-host)
  ptpmon_host=$2
  shift
  shift
  ;;
  --ptpmon-period)
  ptpmon_period=$2
  shift
  shift
  ;;
  --ntp-servers)
  ntp_servers=$2
  shift
  shift
  ;;
  --ntpmon-enable)
  ntpmon_enable="true"
  shift
  ;;
  --ntpmon-period)
  ntpmon_period=$2
  shift
  shift
  ;;
  --noop)
  noop="--noop"
  shift
  ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

if [ $# -ne 1 ]; then
  echo "Need exactly one positional argument <sync-method>"
  echo $usage
  exit 1
fi

sync_method=$1

# Additional checks for 'ntp' case
if [ $sync_method = 'ntp' ] && [ -z "$ntp_servers" ]; then
  echo "Error: --ntp-servers must be specified"
  exit 1
fi

# Additional checks for 'ptp' case
if [ $sync_method = 'ptp' ] && [ -z $ptp_interface ]; then
  echo "Error: --ptp-if must be specified"
  exit 1
fi

export FACTER_TIME_SYNC_METHOD=$sync_method
export FACTER_TIME_PTP_INTERFACE=$ptp_interface
export FACTER_TIME_PTP_MONITOR_HOST=$ptpmon_host
export FACTER_TIME_PTP_MONITOR_PERIOD=$ptpmon_period
export FACTER_TIME_NTP_SERVERS=$ntp_servers
export FACTER_TIME_NTP_MONITOR_ENABLE=$ntpmon_enable
export FACTER_TIME_NTP_MONITOR_PERIOD=$ntpmon_period

cd $basedir

puppet_bin=`which puppet`
if [ $? -ne 0 ]; then
  echo "ERROR: puppet binary not found"
  exit 1
fi

start_date=`date`
echo "${start_date} - running ${cmd} ${all_args}" >> $log_file
sudo -E $puppet_bin apply --verbose $debug --config "$basedir/puppet/puppet.conf" $noop "$basedir/puppet/manifest/timesync.pp" 2>&1 | tee -a $log_file
echo "puppet return status: ${?}" 2>&1 | tee -a $log_file
printf "\n\n\n" >> $log_file
