#!/bin/bash
#
# Copyright (c) 2013 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

echo -n ' Starting timecheck.. '

ntpserver="ntp1"
maxdrift="0.005"

source checkutils.sh

initialize $@

# are we called from somewhere or are we standalone
x=$(caller)
[[ "${x/NULL}" = "$x" ]] && declare -ri timecheck_standalone=0 || declare -ri timecheck_standalone=1

declare failed=""

# check that external need program are installed
s=""
case $os in
    Linux | FreeBSD )
	progs="tr ntpdate host"
	;;
    * )
	failed="Unknown OS :$os: FAILED"
	echo "$failed" >> ${logfile}
	echo "$failed"
	(( $timecheck_standalone )) && exit 0 || return 0
	;;
esac
for i in $progs ; do
    type $i &>/dev/null && continue  || s="$s $i "
done
if [ -n "$s" ] ; then
    failed=" Unable to run need missing command(s) $s FAILED"
    echo "$failed" >> ${logfile}
    echo "$failed"
    (( $timecheck_standalone )) && exit 0 || return 0
fi

{
unset -v d ; declare -a d=()

save_e
set +e
dnsok=$(host $ntpserver) 2>/dev/null
if [ "${dnsok/NXDOMAIN}" != "$dnsok" ] ; then
    echo -n "WARNING ntpserver $ntpserver unknown. " >> ${logfile}
    x=$($BINDIR/tmcc ntpinfo)
    if [ -n "$x" ] ; then
	ntpserver=${x/SERVER=}
    else
	echo -n "tmcc ntpinfo also failed. Last try from resolv.conf" >> ${logfile}
	x=$(grep search /etc/resolv.conf)
	if [ -n "$x" ] ; then
	    ntpserver="${ntpserver}.${x/search }"
	fi
    fi
    echo -n "Trying ntpserver $ntpserver " >> ${logfile}
    dnsok=$(host $ntpserver) 2>/dev/null
    if [ "${dnsok/NXDOMAIN}" != "$dnsok" ] ; then
	echo "WARNING $ntpserver also unknown. " >> ${logfile}
	echo "Giving up">> ${logfile}
	(( $timecheck_standalone )) && exit 0 || return 0
    fi
fi

case $os in
    Linux | FreeBSD )
	d=($(ntpdate -q $ntpserver))
	(( $? )) && failed="No Response from ntpserver:$ntpserver"

	for ((idx=0; idx<${#d[*]}; idx++)) ; do
	    [[ "${d[$idx]}" == "offset" ]] && break
	done
	((++idx))
	z=${d[$idx]}
	offset=$(echo ${z} | tr -d ,) # remove comma
	[[ $offset < 0 ]] && a=$(echo $offset | tr -d '-') || a=$offset # make $a abs()
	[[ $a > $maxdrift ]] && failed="maxdrift"
	if  [ -z "${failed}" ]; then
	    echo "Time check passed offset $offset allowed $maxdrift"
	elif [ "${failed}" = "maxdrift" ] ; then
	    echo "Time check failed offset $offset greater then $maxdrift second "
	else
	    echo "${failed}"
	fi
	;;
    * )
	echo "os $os unknown"
	offset="-1"
	failed="os $os unknown"
	;;
esac
restore_e
} >> ${tmplog} 2>&1

echo -n "Timecheck `date`: " >> ${logfile}
cat ${tmplog} >> ${logfile} 

if [ -z "${failed}" ] ; then 
    echo "offset $offset < $maxdrift OK" 
elif [ "${failed}" == "maxdrift" ] ; then
    echo "offset $offset > $maxdrift FAILED"
else
    echo "${failed} FAILED"
fi

# Check to if called standalone or source from another shell
(( $timecheck_standalone )) && exit 0 || return 0

# return 0
