#!/bin/ksh
#
# Component: utils
#
# Description:
#  Control script of component utils
#
# Last Updated     : $Author$
# Update Date Time : $Date$
# RCS File Name    : $RCSfile$
# RCS Revision     : $Revision$
# Locked by        : $Locker$
# Symbolic name    : $Name$
# State            : $State$

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

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

#--- FUNC ------------------------------
do_shell_()
{
	cat <<-EndOfInlineData
		UTILS_HOME=$HOME/component/utils
		REFCOUNT_VAR=\$VAR/refcount
		REFCOUNTMODE=Database
		REFCOUNTTBL=RefCounter

	EndOfInlineData

} #do_shell_()

do_status_()
{
	echo "OK"
	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
}

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

	convert_jpeg2000_conf_
} #do_start_()

                                                                        
#
# 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
