#!/bin/sh
#
#    Copyright (c) 1996-2020 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Initialization script to start/stop ntpd.
#

# Source common library
. /etc/init.d/functions

NTPD=/usr/sbin/ntpd
NTPQ=/usr/sbin/ntpq
AWK="/bin/awk"
GREP="/bin/grep"
WC="/usr/bin/wc"
CAT="/bin/cat"
CUT="/usr/bin/cut"
ECHO="/bin/echo"
KILL="/bin/kill"
MV="/bin/mv"
RM="/bin/rm"
PS="/bin/ps"
SED="/bin/sed"
NULL="/dev/null"
SWITCHSHOW="/fabos/bin/switchshow"
CONFIG_GET="/fabos/cliexec/config get"
ACTIVE_SERVER_FILE="/tmp/ts.ntpq.active.log"
STATS_FILE="/tmp/ts.ntpq.stats.log"

case "$1" in
    
'start')    
    pid=`$PS -ef | $GREP "ntpd" | $GREP -v grep | $GREP -v start | $AWK '{print $2}'`
    $KILL $pid > ${NULL} 2>&1
	if test -x $NTPD; then
	    # Query for NTP server and update ntp.conf
	    # Delete old servers
	    $SED '/^server/d' /etc/ntp.conf > /etc/ntp.conf.tmp
	    $MV /etc/ntp.conf.tmp /etc/ntp.conf
	    
	    # Delete old trustedkeys
	    $SED '/^trustedkey/d' /etc/ntp.conf > /etc/ntp.conf.tmp
	    $MV /etc/ntp.conf.tmp /etc/ntp.conf
	    
	    # Delete old trustedkeys
	    $SED '/^controlkey/d' /etc/ntp.conf > /etc/ntp.conf.tmp
	    $MV /etc/ntp.conf.tmp /etc/ntp.conf
	    
	    # Add new servers
	    active_server=`$CONFIG_GET ts.clockServer 5`
	    active_index=`$CONFIG_GET ts.clockServerIndex 5`
	    ntp_servers=`$CONFIG_GET ts.clockServerList 5`
	    ntp_key_indexes=`$CONFIG_GET ts.clockServerIndexList 5`
	    authspec_mode=`$CONFIG_GET ts.clockServerAuthSpec 2`
	    legacy_mode=`$CONFIG_GET ts.clockServerLegacyMode 2`
	    
	    IFS=';' read -r -a array <<< "$ntp_servers"
	    IFS=';' read -r -a array2 <<< "$ntp_key_indexes"
	    
	    # if legacy mode enabled, only add active server to list
	    if [ $legacy_mode -eq 1 ]; then
	    	if [ "$active_server" == "LOCL" ]; then
			exit 
		fi	
		if [[ $active_index -ne -1 && $authspec_mode -eq 1 ]]; then
			$ECHO "server $active_server minpoll 6 maxpoll 10 key $active_index" >> /etc/ntp.conf
		else
			$ECHO "server $active_server minpoll 6 maxpoll 10" >> /etc/ntp.conf
		fi	
	    else
	    	i=0
	    	for element in "${array[@]}"
			do
				if [ "$element" == "LOCL" ]; then
					exit 
				fi			
				if [[ -n "${array2[i]}" && ${array2[i]} -ne -1 && $authspec_mode -eq 1 ]]; then
					$ECHO "server $element minpoll 6 maxpoll 10 key ${array2[i]}" >> /etc/ntp.conf
				else
					$ECHO "server $element minpoll 6 maxpoll 10" >> /etc/ntp.conf
				fi			
				i=$((i+1))
			done
	    fi

		trustedkeys=`$CONFIG_GET ts.clockServerIndexList 5`
		$ECHO "trustedkey $trustedkeys" | $SED 's/;/ /g' >> /etc/ntp.conf
		
		#controlkeys=`$CONFIG_GET ts.clockServerIndexList 5`
		#$ECHO "controlkey $controlkeys" | $SED 's/;/ /g' >> /etc/ntp.conf
			
	    # Start ntpd
	    $NTPD -g -a -c /etc/ntp.conf > ${NULL} 2>&1
	fi

    ;;

'stop')
    ${KILLALL} ntpd > ${NULL} 2>&1
    ;;    
    
'query')
    $NTPQ -pn > $ACTIVE_SERVER_FILE.tmp 2>&1
    $CAT $ACTIVE_SERVER_FILE.tmp | $AWK '{print $1}' | $AWK 'NR>2' > $ACTIVE_SERVER_FILE 
    ;;  
    
'stats')
    $NTPQ -pn > $STATS_FILE 2>&1
    $ECHO "" >> $STATS_FILE

    $NTPQ -c rv > $STATS_FILE.tmp 2>&1
    #Skip first 8 lines
    cnt=8
    reftimeflag=0
    clockflag=0
	IFS=$',' read -d '' -r -a array < $STATS_FILE.tmp
	for i in "${array[@]}"; do
		if [ $cnt -ge 0 ]; then
			cnt=$((cnt-1))
		else
			$ECHO $i | $GREP 'reftime' > ${NULL} 2>&1
			reftime=$?
			$ECHO $i | $GREP 'clock' > ${NULL} 2>&1
			clock=$?
			if [[ $reftime -eq 0 ]]; then
				reftimeflag=1
			elif [[ $clock -eq 0 ]]; then
				clockflag=1
			else
				if [[ $reftimeflag -eq 1 ]]; then
					$ECHO "reftime=$i" >> $STATS_FILE
					reftimeflag=0	  
				elif [[ $clockflag -eq 1 ]]; then
					$ECHO "clock=$i" >> $STATS_FILE
					clockflag=0	  
				else
					$ECHO $i >> $STATS_FILE
				fi	    
			fi
		fi
	done
	
	# Replace "remote" with "address"
	$SED -i 's/remote/address/g' $STATS_FILE 
	# Remove 'system' information
	$SED -i '/system/d' $STATS_FILE 
	# Remove 'processor' information
	$SED -i '/processor/d' $STATS_FILE 
	# Remove 'version' information
	$SED -i '/version/d' $STATS_FILE 
	
	# Remove 'peer' information
	$SED -i '/peer/d' $STATS_FILE 
	# Remove 'tc' information
	$SED -i '/tc/d' $STATS_FILE 
	# Remove 'mintc' information
	$SED -i '/mintc/d' $STATS_FILE 
	
	$ECHO "" >> $STATS_FILE
	$NTPQ -c as >> $STATS_FILE 2>&1

	# Remove 'ntpq' errors
	$SED -i '/ntpq/d' $STATS_FILE 
	
	$CAT $STATS_FILE 
    ;;  

*)
    echo "usage: $0 {start | stop | query | stats}"
    ;;

esac
