#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61ntp428 src/tcpip/usr/sbin/ntp4/ntp_ssw/ntp_ssw 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2021 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)65        1.4  src/tcpip/usr/sbin/ntp4/ntp_ssw/ntp_ssw, ntp, 61ntp428, 2127A_61ntp428 7/8/21 08:13:40
#
#
#

L_FLAG=     # link flag
R_FLAG=	    # xntpd daemon running flag 
DAEMON=xntpd
IS_XNTPD_RUNNING=0

NTPV3_FILES=6
V3_FILE[0]="/usr/sbin/ntp3/xntpd"
V3_FILE[1]="/usr/sbin/ntp3/xntpdc"
V3_FILE[2]="/usr/sbin/ntp3/ntpq"
V3_FILE[3]="/usr/sbin/ntp3/ntptrace"
V3_FILE[4]="/usr/sbin/ntp3/sntp"
V3_FILE[5]="/usr/sbin/ntp3/ntpdate"

NTPV4_FILES=7
V4_FILE[0]="/usr/sbin/ntp4/ntpd4"
V4_FILE[1]="/usr/sbin/ntp4/ntpdc4"
V4_FILE[2]="/usr/sbin/ntp4/ntpq4"
V4_FILE[3]="/usr/sbin/ntp4/ntptrace4"
V4_FILE[4]="/usr/sbin/ntp4/sntp4"
V4_FILE[5]="/usr/sbin/ntp4/ntpdate4"
V4_FILE[6]="/usr/sbin/ntp4/ntp-keygen4"

# NTP Softlink files 
SLINK_FILES[0]="/usr/sbin/xntpd" 
SLINK_FILES[1]="/usr/sbin/xntpdc"
SLINK_FILES[2]="/usr/sbin/ntpq" 
SLINK_FILES[3]="/usr/sbin/ntptrace"
SLINK_FILES[4]="/usr/sbin/sntp" 
SLINK_FILES[5]="/usr/sbin/ntpdate" 
SLINK_FILES[6]="/usr/sbin/ntp-keygen"

################################# usage #########################################
#
# NAME: usage
#
# DESCRIPTION: Display the usage and exit
#
usage(){
	/usr/bin/echo "Usage:  /usr/sbin/ntp_ssw [-v4|-v3]"
	/usr/bin/echo "-v4     Switch soft links to NTPv4 binaries."
	/usr/bin/echo "-v3     Switch soft links to NTPv3 binaries."
}


############################## checkexistence ###################################
#
# NAME: checkexistence
#
# DESCRIPTION: Test if the NTP executable already exists.
#
# INPUT:
#        $1   Name of NTP binary file
#
checkexistence() {

	NTP_BINARY=$1

	if [ ! -f $NTP_BINARY ]; then
		/usr/bin/echo "$NTP_BINARY doesn't exist." >&2
		exit 1
	fi
}

############################## check_and_relink ########################################
#
# NAME: check_and_relink
#
# DESCRIPTION: Test if the symbolic link already exists. 
#
# INPUT:
#	$1  NTP v3/v4 binary file name
#	$2  NTP target file name
#
check_and_relink() {

	NTP_BINARY_FILE=$1
	NTP_SLINK_FILE=$2

	L_FLAG=`/usr/bin/ls -al $NTP_SLINK_FILE 2>/dev/null | /usr/bin/awk '{print substr($1,1,1)}'`

	if [ "$L_FLAG" = "l" ]; then
		OLD_BINARY_FILE=`/usr/bin/ls -al $NTP_SLINK_FILE 2>/dev/null | /usr/bin/awk '{print $NF}'`

		if [ "$OLD_BINARY_FILE" = $NTP_BINARY_FILE ]; then
			/usr/bin/echo "$NTP_SLINK_FILE is already linked to $NTP_BINARY_FILE" >&2
		else
			stopdaemon 
        		relink $NTP_BINARY_FILE $NTP_SLINK_FILE
			update_trustchk $NTP_BINARY_FILE $NTP_SLINK_FILE $OLD_BINARY_FILE
		fi
	else
		stopdaemon 
		relink $NTP_BINARY_FILE $NTP_SLINK_FILE 
		update_trustchk $NTP_BINARY_FILE $NTP_SLINK_FILE $OLD_BINARY_FILE
	fi
}


############################## stopdaemon #############################
#
# NAME: stopdaemon 
#
# DESCRIPTION: Check if xntpd daemon is running. 
#	       If so, 
#		   set the R_FLAG.
#	           stop the running xntpd daemon.
#
#
stopdaemon() {

	R_FLAG="$( /usr/bin/lssrc -s $DAEMON 2>/dev/null | /usr/bin/tail -1 | /usr/bin/awk '{print $NF}' )"
	if [ $R_FLAG = "active" ]; then
		# Save the status of daemon, 
		# which is used to start the deamon after the version switching 
		IS_XNTPD_RUNNING=1
		/usr/bin/stopsrc -s $DAEMON >/dev/null 2>&1
		RTNCODE=$?
		if [ $RTNCODE -ne 0 ]; then
			/usr/bin/echo "stopsrc failed to stop $DAEMON"
			exit 2
		fi
	fi
}


############################## startdaemon #############################
#
# NAME: startdaemon
#
# DESCRIPTION: Check if xntpd daemon is running.
#          If so,
#          set the R_FLAG.
#              start the running xntpd daemon.
#
# INPUT:
#      NONE 
#

startdaemon() {

	R_FLAG="$( /usr/bin/lssrc -s $DAEMON 2>/dev/null | /usr/bin/tail -1 | /usr/bin/awk '{print $NF}' )"
	if [ $R_FLAG = "inoperative" ] && [ $IS_XNTPD_RUNNING -ne 0 ]; then
		/usr/bin/startsrc -s $DAEMON
		RTNCODE=$? 
		if [ $RTNCODE -ne 0 ]; then
			/usr/bin/echo "startsrc failed to start $DAEMON."
			exit 3
		fi
	fi
}


################################## relink ###################################
# NAME:  relink 
#
# DESCRIPTION: Relink the NTP binaries to the intended executable
#
# INPUT:
#	$1- NTPV3/NTPV4 files , $2 target file name

relink() {
	NTP_BINARY_FILE=$1
	NTP_SLINK_FILE=$2

	if [[ -L $NTP_SLINK_FILE ]]; then
		/usr/bin/rm -f $NTP_SLINK_FILE > /dev/null 2>&1
		RTNCODE=$?
		if [ $RTNCODE -ne 0 ]; then
			/usr/bin/echo "Failed to remove the file $NTP_SLINK_FILE"
			exit 4
		fi
	fi

	/usr/bin/echo "Creating the symbolic link $NTP_SLINK_FILE to $NTP_BINARY_FILE"
	/usr/bin/ln -s $NTP_BINARY_FILE $NTP_SLINK_FILE > /dev/null 2>&1
	RTNCODE=$?
	if [ $RTNCODE -ne 0 ]; then
		/usr/bin/echo "Failed to link the file $NTP_SLINK_FILE"
		exit 5
	fi
}

################################## update_trustchk###############################
#NAME: update_trustchk
#
# DESCRIPTION:  update trustchk database
#
# INPUT:
#	$1 New NTP binary file linked aftre switching
#	$2 old NTP binary file before switching 
#	$3 NTP target file name
#
update_trustchk() {

	NEW_BINARY_FILE=$1   	
	OLD_BINARY_FILE=$3
	SLINK_FILE=$2

	# update symlinks attribute value, for old binary set it as blank
	# for new binary it should be symlink file name
	/usr/sbin/trustchk -u $OLD_BINARY_FILE symlinks=''
	/usr/sbin/trustchk -u $NEW_BINARY_FILE symlinks=$SLINK_FILE

}

################################## ssw_ntpv3 ###################################
# NAME: ssw_v3e 
#
# DESCRIPTION:	Switch the symbolic link to NTP v3 files ..
#
# INPUT:
#	NONE
#

ssw_ntpv3() {
 	# Check if all the NTPv3 binaries exist. If they don't 
	# exist, checkexistence would exit printing a error message.
	(( i=0 ))
	while ((i< NTPV3_FILES)); do
		checkexistence ${V3_FILE[i]} 
		(( i= i+1 )) 
	done

	# Check the softlinks and relink if necessary.
	(( i=0 ))
	while (( i< NTPV3_FILES )); do
		check_and_relink ${V3_FILE[i]} ${SLINK_FILES[i]}
		(( i=i+1 ))
	done 
	
	# Start the ntp daemon if it was stopped.
	startdaemon
}

################################## ssw_ntpv4 ###################################
# NAME: ssw_v4
#
# DESCRIPTION:  Switch the symbolic link to NTP v4 files .
#
# INPUT:
#       NONE
#
ssw_ntpv4() {

 	# Check if all the NTPv4 binaries exist. If they don't 
	# exist, checkexistence would exit printing a error message.
	(( i =0 ))
	while ((i< NTPV4_FILES)); do
		checkexistence ${V4_FILE[i]}
		(( i= i+1 ))
	done

	# Check the softlinks and relink if necessary.
	(( i=0 ))
	while (( i< NTPV4_FILES )); do
		check_and_relink ${V4_FILE[i]} ${SLINK_FILES[i]}
		(( i= i+1 ))
	done

	# Start the ntp daemon if it was stopped.
	startdaemon 
}


############################## main ############################################

if [ "$1" == "-v3" ]; then
	ssw_ntpv3
elif [ "$1" == "-v4" ]; then
	ssw_ntpv4
else
	usage
	exit 6
fi

exit 0