#!/bin/bash



#################################################################
## Functions 
tempfiles=( )

cleanup() {
  rm -f "${tempfiles[@]}"
}

endOfProgram() {
  set +x
  tracef "== End of execution (command:$SSH_ORIGINAL_COMMAND) [$(/bin/date)] (err=$1) == "
  tracef ""
  exit $1

}

SigExit() {
	tracef "Terminated by signal."
	cleanup 
}


#######################################################
#
# MAIN
#
#######################################################
# import common variables and functions
. /opt/rcon/bin/sshr-base

trap 'SigExit' 1 2 3 15

#check if SSH_ORIGINAL_COMMAND contains command to be executed
#-if it is empty, process parameters of script
if [ -z "$SSH_ORIGINAL_COMMAND" ];then
  SSH_ORIGINAL_COMMAND=$@
fi

cutLog
tracef "== Start of execution (command:$SSH_ORIGINAL_COMMAND) [$(/bin/date)] =="

# get command without specified paramaters
SHORTCOMMAND=$(echo $SSH_ORIGINAL_COMMAND | cut -d" " -f1)
tracef "param:whole command=\"$SSH_ORIGINAL_COMMAND\""
tracef "param:command without params=\"$SHORTCOMMAND\""
tracef "param:user=\"$USER\", sudo user=\"$SUDO_USER\""


#read XX
case "$SHORTCOMMAND" in
 *dbaccess*)
   . /opt/informix/*var
   tracef "dbaccess execution, environment for Informix is set."
   eval $SSH_ORIGINAL_COMMAND
   RES=$?
   ;;
 getMAC)
   PARAMC=$(echo $SSH_ORIGINAL_COMMAND | wc -w)
   if [ $PARAMC -gt 1 ]; then
     DEVICE=$(echo $SSH_ORIGINAL_COMMAND | cut -d" " -f2)
   else
     #print MAC address of eth0
     DEVICE=eth0
   fi
   if [ "$DEVICE" = "-l" ]; then
     #list eth0, eth1, eth2 devices
     cat /sys/class/net/eth0/address
     cat /sys/class/net/eth1/address
     cat /sys/class/net/eth2/address
   else
     cat /sys/class/net/$DEVICE/address
   fi

   RES=$?
   ;;
 getpubIP)
   /opt/rcon/bin/getlocpubIP
   RES=$?
   ;;

 getHAipInfo)
   PARAMC=$(echo $SSH_ORIGINAL_COMMAND | wc -w)
   if [ $PARAMC -gt 1 ]; then
        WHAT=$(echo $SSH_ORIGINAL_COMMAND | cut -d" " -f2)
		$GETHAIPSETUPENTRY $WHAT
        RES=$?
   else
        #no parameter - exit
        RES=1
   fi
	;; 

 configureCLAN)
	#distinguish Ass from CAP
	[ $CURRENTSYSTEM = "assistant_intl" ] && CONFIGURE_CLAN=$ASCONFIGURECLAN || CONFIGURE_CLAN=$CAPCONFIGURECLAN
	#parse original command line parameters
	PARAMS=`echo "$SSH_ORIGINAL_COMMAND" | sed -e 's/configureCLAN//'`
	#call the configure clan script
	$CONFIGURE_CLAN $PARAMS
	RES=$?
	;;
 getTimezone)
	#TIMEZONE=`sed -n 's/^TIMEZONE=\(.*\)/\1/p' /etc/sysconfig/clock | sed -n 's/"//gp'`
	#RES=$?
	TIMEZONE=$(readlink -f /etc/localtime)
	[[ "x$TIMEZONE" =~ /usr/share/zoneinfo ]]
	RES=$?
	TIMEZONE=${TIMEZONE#/usr/share/zoneinfo/}
	echo $TIMEZONE
	tracef "getTimezone = \"$TIMEZONE\""
	;;

 setTimezone)
	if [ $CURRENTSYSTEM = "host_intl" ]
	then
		PARAMC=$(echo $SSH_ORIGINAL_COMMAND | wc -w)
		if [ $PARAMC -gt 1 ]; then
			TZONE=$(echo $SSH_ORIGINAL_COMMAND | cut -d" " -f2)
			#set current TZ
			ret_code=`/opt/webservice/scripts/swupdated set_timezone "$TZONE" 2>&1 | sed -n -r -e "s~<rc>([0-9]+)</rc>~\1~p"`
			if [ "$ret_code" -ne 0 ]; then
				echo "swupdated set_timezone failed, rc=$ret_code" >&2
				exit 1
			fi 
			RES=$?
		fi
	elif [ $CURRENTSYSTEM = "cap_intl" ]
	then
		PARAMC=$(echo $SSH_ORIGINAL_COMMAND | wc -w)
		if [ $PARAMC -gt 1 ]; then
			TZONE=$(echo $SSH_ORIGINAL_COMMAND | cut -d" " -f2)
			#set current TZ and permament also if succ
			/usr/sbin/zic -l $TZONE && ed /etc/sysconfig/clock <<GOGO
H
g/^TIMEZONE/c\\
TIMEZONE="$TZONE"\\
.
w
q
GOGO
			RES=$?
		fi
	fi
	;;
	
 resetPassword)
	if [ $CURRENTSYSTEM = "host_intl" ]
	then
		PARAMC=$(echo $SSH_ORIGINAL_COMMAND | wc -w)
		if [ $PARAMC -le 1 ]; then
			echo "Usage: resetPassword <username>; password on stdin"
			exit 1
		fi
		USER=$(echo $SSH_ORIGINAL_COMMAND | cut -d" " -f2)
		TMP=`/bin/mktemp /dev/shm/resetPassword-XXXXXXXX`
		tempfiles+=( "$TMP" )
		echo -n "$USER:" > $TMP
		cat - >> $TMP
		tracef "Password file: $TMP"
		RES=0
		if ! /opt/webservice/scripts/has_quorum >/dev/null
		then
			#start with the quorum node first = 103700
			/opt/inventory/inventory put 103700 $TMP $TMP
			if [ $? -eq 0 ]
			then
				/opt/inventory/inventory command1 103700 "/usr/sbin/chpasswd <$TMP; rm -f $TMP"
				if [ $? -eq 0 ]; then 
					tracef "Quorum's password for user $USER has been reset."
				else 
					tracef "Error resetting Quorum's password for user $USER."
					RES=1
				fi
			else
				tracef "Error transfer password file to Quorum node."
				RES=1
			fi
		fi

		if ! /opt/webservice/scripts/is_duplex >/dev/null
		then
			#standby node = 102700
			/opt/inventory/inventory put 102700 $TMP $TMP
			if [ $? -eq 0 ]
			then
				/opt/inventory/inventory command1 102700 "/usr/sbin/chpasswd <$TMP; rm -f $TMP"
				if [ $? -eq 0 ]; then
					tracef "Standby node's password for user $USER has been reset."
				else
					tracef "Error resetting Standby node's password for user $USER."
					RES=1
				fi
			else
				tracef "Error transfer password file to Standby node."
				RES=1
			fi
		fi
		/usr/sbin/chpasswd <$TMP
		if [ $? -eq 0 ]; then
			tracef "Active node's password for user $USER has been reset."
		else
			tracef "Error resetting Active node's password for user $USER."
			RES=1
		fi
		rm -f $TMP
		
	elif [ $CURRENTSYSTEM = "cap_intl" ]
	then
		PARAMC=$(echo $SSH_ORIGINAL_COMMAND | wc -w)
		if [ $PARAMC -le 1 ]; then
			echo "Usage: resetPassword <username>; password on stdin"
			exit 1
		fi
		USER=$(echo $SSH_ORIGINAL_COMMAND | cut -d" " -f2)
		TMP=`/bin/mktemp /dev/shm/resetPassword-XXXXXXXX`
		tempfiles+=( "$TMP" )
		echo -n "$USER:" > $TMP
		cat - >> $TMP
		tracef "Password file: $TMP"
		RES=0
		/usr/sbin/chpasswd <$TMP
		if [ $? -eq 0 ]; then
			tracef "CSTA's password for user $USER has been reset."
		else
			tracef "Error resetting CSTA's password for user $USER."
			RES=1
		fi
		rm -f $TMP
	fi
	;;

 createLock)
   . /opt/rcon/bin/sshr-lock
   tracef "Locking ..."
   parseArgs
   RES=$?
   if [ $RES -eq 0 ]; then
     createLock
     RES=$?
   fi
   ;;
 checkLock)
   . /opt/rcon/bin/sshr-lock
   tracef "Checking lock ..."
   checkLock
   RES=$?
   ;;
 deleteLock)
   . /opt/rcon/bin/sshr-lock
   tracef "Deleting lock ..."
   parseArgs
   RES=$?
   if [ $RES -eq 0 ]; then
     deleteLock
     RES=$?
   fi
   ;;
 *)
   tracef "execution of command started  at [$(/bin/date)]"
   eval $SSH_ORIGINAL_COMMAND
   RES=$?
   tracef "execution of command finished at [$(/bin/date)]"
   ;;

esac 

tracef "== End of execution ($SSH_ORIGINAL_COMMAND) [$(/bin/date)] (err=$RES) =="
tracef ""

exit $RES



