#!/bin/ksh
#
# Component: utils
#
# Description:
#  Control script of component utils
#

#--- INIT ------------------------------
#needs $HOME
. $HOME/etc/ctrl.main.shi

[ -f $HOME/etc/setenv.sh ] && . $HOME/etc/setenv.sh

#--- FUNC ------------------------------
do_shell_()
{
	RefCountValue=Database
	
	for refCountDir in $VAR/refcount/*/ ; do
	    rmdir $refCountDir >/dev/null 2>&1 
	done
	rmdir $VAR/refcount >/dev/null 2>&1
	
	[ -d $VAR/refcount ] && RefCountValue=Hybrid

	cat <<-EndOfInlineData
		UTILS_HOME=$HOME/component/utils
		REFCOUNT_VAR=\$VAR/refcount
		REFCOUNTMODE=$RefCountValue
		REFCOUNTTBL=RefCounter
		QUERY_GUARD_PID=\$RUN/queryGuard.pid

	EndOfInlineData

} #do_shell_()


#
# check if process is a queryGuard
#
# $1 - process id
#
# return
#    0 - process is a queryGuard
#    1 - process does not exist or is not a queryGuard
#    2 - process id is empty
#
query_guard_check_process_()
{
	local Pid="$1"
	local Cmd

	[ -z "${Pid}" ] && return 2
	Cmd=$( ps -p "${Pid}" -o comm h )
	[ "${Cmd}" = "queryGuard" ] && return 0

	return 1
} #query_guard_check_process_


#
# Starts the queryGuard
#
query_guard_start_()
{
	local Pid
	local ProcList

	if [ -f "${QUERY_GUARD_PID}" ] ; then
		# should not happen check if the PID is valid
		Pid=$( cat "${QUERY_GUARD_PID}" )
		if query_guard_check_process_ $( cat "${QUERY_GUARD_PID}" ) ; then
			echo "queryGuard is already running. Pid: ${Pid}"
			return 0
		fi
	else
		ProcList=$( ps -C queryGuard h -o pid )
		if [ -n "${ProcList}" ] ; then
			echo "An orphaned queryGuard process is already running (${ProcList})" >&2
			return 1
		fi
	fi

	$UTILS_HOME/bin/queryGuard -c 1 >"$LOG/queryGuard.log" 2>&1 &
	Pid=$!
	echo $Pid >"$QUERY_GUARD_PID"
	echo "queryGuard started. Pid: ${Pid}"
	return 0
} #query_guard_start_


#
# Stop the queryGuard
#
query_guard_stop_()
{
	local Pid
	local ProcList

	if [ -f "${QUERY_GUARD_PID}" ] ; then
		Pid=$( cat "${QUERY_GUARD_PID}" )
		if query_guard_check_process_ $( cat "${QUERY_GUARD_PID}" ) ; then
			echo "killing queryGuard (${Pid})"
			kill "${Pid}"
		else
			echo "queryGuard process (${Pid}) does not exist"
		fi

		rm "${QUERY_GUARD_PID}"
	fi

	ProcList=$( ps -C queryGuard h -o pid )
	[ -z "${ProcList}" ] && return 0

	# queryGuard processes are still running wait a millisec before start killall
	sleep 0.01
	ProcList=$( ps -C queryGuard h -o pid )
	[ -z "${ProcList}" ] && return 0

	echo "queryGuard process is still running (${ProcList}); killall" >&2
	killall queryGuard
	sleep 0.01

	ProcList=$( ps -C queryGuard h -o pid )
	[ -z "${ProcList}" ] && return 0

	echo "queryGuard process is still running (${ProcList}); killall -9" >&2
	killall -9 queryGuard

	ProcList=$( ps -C queryGuard h -o pid )
	[ -z "${ProcList}" ] && return 0
	echo "queryGuard process could not be stopped" >&2
	return 1
} #query_guard_stop_


#
# Checks the status of the queryGuard process
#
# return
#   0 - OK
#   1 - Stopped
#   2 - Error
#
query_guard_status_()
{
	local Pid
	local ProcList

	if [ -f "${QUERY_GUARD_PID}" ] ; then
		Pid=$( cat "${QUERY_GUARD_PID}" )
		query_guard_check_process_ $( cat "${QUERY_GUARD_PID}" ) && return 0
		return 2
	fi

	return 1
}



do_status_()
{
	query_guard_status_
	case $? in
		0)
			echo "OK"
			;;
		1)
			echo "STOPPED"
			;;
		*)
			echo "ERROR"
			;;
	esac
	exit

} #do_status_()


convert_jpeg2000_conf_()
{
	# check if legacy config file exists and return if there is no legacy config file
	[ ! -f "$CONF/lossyjpeg2000params.conf" ] && return 0 

	# check if config file exists
	if [ ! -f "$CONF/jpeg2000lossy.conf" ] ; then
		# if no config file exists - convert legacy config file
		awk -vFS="[ =]" -vQ='"' ' \
			{ \
				gsub( Q, "" ); \
				for (i = 1; i <= NF; i++ ) { \
					if ( $i == "rate" ) { \
						rate = $(i+1); \
						if ( rate == "" ) { \
							next; \
						} \
						printf "%s=%s%g%s\n", $1, Q, rate, Q; \
					} \
				} \
			} ' "$CONF/lossyjpeg2000params.conf" > "$CONF/jpeg2000lossy.conf"
	fi
	
	# remove legacy config file
	rm -f "$CONF/lossyjpeg2000params.conf"
	return 0
} # convert_jpeg2000_conf_


do_start_()
{
	# check and create $REFCOUNT_VAR
	[ ! -d "$REFCOUNT_VAR" ] && mkdir $REFCOUNT_VAR

	convert_jpeg2000_conf_

	query_guard_start_
} #do_start_()


do_stop_()
{
	query_guard_stop_
} #do_stop_()

do_crontab_()
{
    if [ "$REFCOUNTMODE" == "Hybrid" ]; then
cat <<-EOI
0 6 * * * $UTILS_HOME/bin/checkRefcountMode.sh >/dev/null 2>&1
EOI
    fi
} #do_crontab_()


#
# List files/tables to be backed up grouped by categories
#
# output lines should be in '<category> <type> <names>' format
#
do_listbackup_()
{
	cat <<-EOFLISTBACKUP
		misc.refcount FILE ${REFCOUNT_VAR#$HOME/}
		config.system FILE ${CONF#$HOME/}/jpeg2000lossy.conf
	EOFLISTBACKUP
}
#--- MAIN ------------------------------

check_action_ $1
if [ 0 = $? ]; then
	do_$1_ $2

else
	echo "--- Component utils [ctrl $$]: Bad command: $1" >&2
	exit 1

fi
