#!/bin/bash
# #################################################################################
# Exceed TurboX Connection Node
# © Rocket Software, Inc. or its affiliates. All Rights Reserved.
# ROCKET SOFTWARE, INC. CONFIDENTIAL
# #################################################################################
# patch
# #################################################################################

# patch script for Exceed TurboX Connection Node 



test_ECHO()
{
	${ECHO} -e "test\c" | grep "\-e" > /dev/null
	if [ $? -gt 0 ] ; then
		# -e is supported so add it
		ECHO="${ECHO} -e"
	fi
}

find_good_ECHO()
{
	ECHO=`which echo`
	test_ECHO
	export ECHO
}

test_TR()
{
	echo "aaAA" | ${TR} [:upper:] [:lower:]  > /dev/null 2>&1
	if [ $? -gt 0 ] ; then
		return 1
	else
		return 0
	fi
}

find_good_TR()
{
	TR=`which tr`
	test_TR
	if [ $? -gt 0 ] ; then
		TR=/usr/ucb/tr
		test_TR
		if [ $? -gt 0 ] ; then
			${ECHO} "FATAL ERROR: The tr version on this machine is too old.  It needs to be equivalent to GNU tr."
			cd_and_exit 1
		fi
	fi
	export TR
	return
}

strlen()
{
	# `expr length "$*"` is not supported in older systems
	strlenresult=`expr "$*" : '.*'`
	return ${strlenresult}
}

printsectmessage()
{

	if [ "${bSilent}" = "1" ] ; then
		return
	fi
	
	# calculate proper # of = for this header
	strlen $*
	slen=$?
	lpos=0
	suffix=
	
	while [ ${lpos} -lt  ${slen} ]
	do
		suffix="${suffix}="
		lpos=`expr ${lpos} + 1`
	done
	
	${ECHO}
	${ECHO} $*
	${ECHO} ${suffix}
	${ECHO}
}

test_GREP()
{
	${ECHO} ${GREP} -e GREP "$0" > /dev/null 2>&1
	${GREP} -e GREP "$0" > /dev/null 2>&1
	if [ $? -gt 0 ] ; then
		return 1
	else
		return 0
	fi
}

find_good_grep()
{
	GREP=`which grep`
	test_GREP
	if [ $? -gt 0 ] ; then
		GREP=`which egrep`
		test_GREP
		if [ $? -gt 0 ] ; then
			GREP=/usr/xpg4/bin/grep
			test_GREP
			if [ $? -gt 0 ] ; then
				${ECHO} "FATAL ERROR: The grep version on this machine is too old.  It needs to be equivalent to GNU grep."
				exit 1
			fi
		fi
	fi
	export GREP
	return
}


usage()
{
	${ECHO} "Usage: `basename $0` [-s responsefile]"
	exit
}


readThisSectionKeyValueForThisVariable()
{
	thisPropFile=$1
	thisSection=$2
	thisKey=$3
	thisKeyFullName=${thisSection}.${thisKey}
	
	tmp_value=`${GREP} "^${thisKeyFullName}[ \t]*=" "${thisPropFile}"  2> /dev/null | cut -f2 -d=`
	ret_value=`echo ${tmp_value}|${TR} -d '\011\012\015'`
	# because of possible !! in password string we need to handle this specially without use of eval
	if [ "${thisKey}" = "DBPassword" ] ; then
		DBPassword="${ret_value}"
	else
		eval ${thisKey}="${ret_value}"
	fi
}

readThisSectionKeys()
{
	thisPropFile="$1"
	thisSection="$2"
	theseKeys="$3"
	
	for thisKey in ${theseKeys} ; do
		readThisSectionKeyValueForThisVariable "${thisPropFile}" "${thisSection}" ${thisKey}
	done
}

readResponseFile()
{
	thisResponseFile="$1"
	if [ ! -f "${thisResponseFile}" ] ; then
		${ECHO} "FATAL ERROR: Silent mode response file '${thisResponseFile}' does not exist!"
		exit 1
	fi
#	${ECHO} Silent mode active.  Current ResponseFile values:
	theseKeys="SkipNodeChange" 
	readThisSectionKeys "${thisResponseFile}" "node.patch" "${theseKeys}"	
}


resolveDir()
{
	# resolve links - $0 may be a softlink
	thisPRG="${1}"
	
	while [ -h "$thisPRG" ]; do
		ls_ld=`ls -ld "$thisPRG"`
		this_link=`expr "${ls_ld}" : '.*-> \(.*\)$'`
		
		if expr "${this_link}" : '/.*' > /dev/null; then
			thisPRG="${this_link}"
		else
			thisPRG=`dirname "${thisPRG}"`/"${this_link}"
		fi
	done

	# -- we only want to check fully resolved path
	if [ "${3}" = "CD" ] ; then
		eval ${2}="${thisPRG}"
		return
	fi
	
	CDir=`pwd`
	
	thisPrgDirName="`dirname ${thisPRG}`"
	
	cd "${thisPrgDirName}" > /dev/null 2>&1
	cd .. > /dev/null 2>&1 
	
	Here=`pwd`
	eval ${2}="${Here}"

	cd ${CDir}

}

getInstallDir()
{
	resolveDir $0 InstallDir
}

resetCDir()
{
	cd "${CDir}" > /dev/null 2>&1
}

cd_and_exit()
{
	resetCDir
	exit $1
}

init_patch()
{
	find_good_ECHO
	find_good_grep
	find_good_TR
	getInstallDir
	bFromInstall="0"
	
	while [ ! -z "${1}" ] 
	do
		case "${1}" in

			-s )
				if [ -z "$2" ] ; then
					usage
				fi
				
				shift
				bSilent="1"
				readResponseFile "$1"
			;;


			-install )
				bFromInstall="1"
			;;
			
			* )
				${ECHO} "Invalid option: ${1}"
				exit 1
			;;
		esac
		
		shift
	done
	
	
	binDir=${InstallDir}/bin
	copyssrtosyslib="${binDir}/copyssrtosyslib"
	binnodecmds=${binDir}/nodecmds
	binotetxcn=${binDir}/otetxcn

	conf_dir=${InstallDir}/conf

}

always_update_nodepackage_version()
{
	if [ ! -f "${binnodecmds}" ] ; then
		${ECHO} "FATAL ERROR: ${binnodecmds} does not exist.  The installation cannot continue."
		cd_and_exit 1
	fi

	cd ${binDir}

	cnpkgversion=`cat ../installlogs/.cnpkg-version`
	if [ ! -n "${cnpkgversion}" ] ; then
		${ECHO} "FATAL ERROR: Connection Node version value contained in .cnpkg-version file is missing.  The patch cannot continue."
		cd_and_exit 1
	fi

	cd ${InstallDir}
}

bootstart_msg()
{
	local bootstatus="$("${binotetxcn}" bootstatus)"
	if [ -d /run/systemd/system \
	     -a ! -f "${InstallDir}/installlogs/systemd.unit.name" \
	     -a "$bootstatus" = "on" \
	]; then
		${ECHO} ""
		${ECHO} "To upgrade bootstart from SysV to native systemd please run: ${binotetxcn} bootstart enable"
	fi
}

checkinstall()
{
	${binnodecmds} checkinstall
	retVal=$?

	if [ "${retVal}" -eq 0 ] ; then
		return 0
	fi
	${ECHO} Please run the install script before attempting to run patch.
}

do_patch()
{
	if [ "${bFromInstall}" -eq 0 ] ; then
		# As long as we are not called from install itself 
		# from 10.5.1 and on we always should update this node's 
		# package version after a patch has been applied
		always_update_nodepackage_version
		
		# If already installed, update SSR libs in system folder
		if [ -f "${copyssrtosyslib}" ]; then
			${copyssrtosyslib} updateonly
		fi
		
		${ECHO} "The patch script has completed successfully."
		bootstart_msg
	else
		${ECHO} "No patch changes are required."
	fi
	
	cd_and_exit 0
}


init_patch $*
checkinstall
do_patch

