#!/bin/sh

! touch /dev/console 2>/dev/null || exec 2> /dev/console

DEBUG=0

LICENSEFILE=lic.xml

. /usr/html/axis-cgi/lib/adp.sh

TAG=${0##*/}
ACTION=
PACKAGE=
DBUS_REQ_GET_APP_STATUS="dbus-send --system --print-reply --dest=com.axis.RuleEngine /com/axis/RuleEngine com.axis.RuleEngine.GetApplicationStatus"

croak() {
	logger -t $TAG -p INFO "$1"
	[ "$DEBUG" = 0 ] || echo "$TAG: $1"
	exit 1
}

echo_ssivar_licensekey() {
	echo -n "<!--#set var=\"license_key\" value=\""
	cat $PACKAGE_DIRECTORY/$1/$LICENSEFILE  2>/dev/null || echo -n "none"
	echo "\" -->"
}

status() {
	local dbus_resp st_resp="Not Found" _cfg="$PACKAGE_DIRECTORY/$1/$ADPPACKCFG"

	if [ -r "$_cfg" ]; then
		if ! __source_if_sourceable "$_cfg"; then
			echo "$ADP_LISTCMD_ERROR_MSG_CONFIG"
			exit 1
		fi
		__get_application_status st_resp
	fi

	echo $st_resp
}

list() {
	local listpack= found= p= status_resp= licensed= dbus_resp= _licstatus= _expdate=

	if [ -d "$PACKAGE_DIRECTORY" ]; then
		cd $PACKAGE_DIRECTORY
		listpack=$(echo *)
		[ "$listpack" != '*' ] || listpack=
		found=0
		for p in $listpack; do
			if [ -r "$p/$ADPPACKCFG" ]; then
				found=1
				if ! __source_if_sourceable "$p/$ADPPACKCFG" ; then
					echo "$ADP_LISTCMD_ERROR_MSG_CONFIG"
					exit 1
				fi

				__get_application_status status_resp

				__get_license_status "$p/$LICENSEFILE" _licstatus _expdate
				licensed="$_licstatus $_expdate"

				cat <<EOF
document.write('<option value="$APPNAME">' + getPaddedString("$PACKAGENAME", width1, true) + '&nbsp;&nbsp;' +
getPaddedString("$APPMAJORVERSION.$APPMINORVERSION", width2, true) + '&nbsp;&nbsp;' +
getPaddedString("$VENDOR", width3, true) + '&nbsp;&nbsp;' +
getPaddedString("$status_resp", width4, true) + '&nbsp;&nbsp;' +
getPaddedString("$licensed", width5, true) +
'&nbsp;</option>\n');
EOF
			fi
		done
		if [ $found -eq 0 ]; then
			cat <<EOF
document.write('<option>' + getPaddedString("&nbsp;", 22, true) + '&nbsp;&nbsp;' +
 getPaddedString("&nbsp;", 22, true) + '&nbsp;&nbsp;' +
 getPaddedString("&nbsp;", 22, true) + '&nbsp;</option>\n');
EOF
		fi
	fi
}

menulist() {
	local listpack= p= name=

	if [ -d $PACKAGE_DIRECTORY ]; then
		cd $PACKAGE_DIRECTORY
		listpack=$(echo *)
		[ "$listpack" != '*' ] || listpack=
		for p in $listpack; do
			if [ -r "$p/$ADPPACKCFG" ]; then
				if ! __source_if_sourceable "$p/$ADPPACKCFG" ; then
					echo "$ADP_LISTCMD_ERROR_MSG_CONFIG"
					exit 1
				fi

				[ "$APPTYPE" != lua ] || APPNAME=${APPNAME%.*}
				if [ "$MENUNAME" ]; then
					name=$MENUNAME
				else
					name=$PACKAGENAME
				fi
				cat <<-EOF
					["$name", "/app_params.shtml", "app=$APPNAME&", hostA, <!--#if expr="\$activeMenu1 = $APPNAME" -->true<!--#else -->false<!--#endif -->, null,
						[
						["Settings", "/app_params.shtml", "app=$APPNAME&", hostA, <!--#if expr="\$activePage = param_$APPNAME" -->true<!--#else -->false<!--#endif -->, null, []],
				EOF
				LICENSEPAGE=$(echo $LICENSEPAGE |tr '[:upper:]' '[:lower:]')
				if [ -z "$LICENSEPAGE" ] || [ "$LICENSEPAGE" = axis ]; then
					cat <<-EOF
						["License", "/app_license.shtml", "app=$APPNAME&", hostA, <!--#if expr="\$activePage = license_$APPNAME" -->true<!--#else -->false<!--#endif -->, null, []],
					EOF
				fi
				if [ "$LICENSEPAGE" = custom ] && [ -r "/usr/html/local/$APPNAME/license.inc" ]; then
					cat <<-EOF
						["License", "/app_license_custom.shtml", "app=$APPNAME&", hostA, <!--#if expr="\$activePage = custom_$APPNAME" -->true<!--#else -->false<!--#endif -->, null, []],
					EOF
				fi
				if [ -r "/usr/html/local/$APPNAME/about.inc" ]; then
					cat <<-EOF
						["About", "/app_index.shtml", "app=$APPNAME&", hostA, <!--#if expr="\$activePage = $APPNAME" -->true<!--#else -->false<!--#endif -->, null, []],
					EOF
				fi
				cat <<-EOF
					[]]],
				EOF
				# Clear variables that may not exist
				# in the next package listed.
				MENUNAME=
			fi
		done
	fi

}

mainpagelink() {
	local indexpage="/local/$1/index.html"

	[ ! -r "/usr/html$indexpage" ] ||
		echo "<a align=\"left\" href=\"#\" onclick=\"openPopUp('$indexpage', 'AppPopUp', 635, 580)\">Main page</a>"
}

settingslink() {
	local settingspagepath=/local/$1

	if [ -r "$PACKAGE_DIRECTORY/$1/$ADPPACKCFG" ]; then
		. "$PACKAGE_DIRECTORY/$1/$ADPPACKCFG" || :
		if [ "$SETTINGSPAGETEXT" ] && [ "$SETTINGSPAGEFILE" ] &&
			 [ -e "/usr/html$settingspagepath" ]; then
			echo "<a align=\"left\" href=\"$settingspagepath/$SETTINGSPAGEFILE\" target=\"_blank\">$SETTINGSPAGETEXT</a>"
		fi
	fi
}

confvariable() {
	local val=

	if [ -r "$PACKAGE_DIRECTORY/$1/$ADPPACKCFG" ]; then
		. "$PACKAGE_DIRECTORY/$1/$ADPPACKCFG" || :
		eval val=\$$2
		echo $val
	fi
}

load_auto_inst_form() {
	local response=

	response=$(curl -s -m 60 http://www.axis.com/techsup/compatible_applications/cam_form.php)

	if [ "$response" ]; then
		echo '<script type="text/javascript">'
		echo "$response"
		echo '</script>'
	else
		echo '<tr><td colspan="4"><div id="internetConnectionStatus">'
		echo '<script type="text/javascript">print_no_conn_text()</script>'
		echo '</div></td></tr>'
	fi
}

get_pre_inst_list() {
	echo '""'
}

ACTION=$1
PACKAGE=$2
VARIABLE=

[ "$ACTION" ]  || croak "No ACTION provided"

case $ACTION in
	list)
		list
		;
	status)
		[ "$PACKAGE" ] || croak "No PACKAGE provided"
		status "$PACKAGE"
		;
	menulist)
		menulist
		;
	mainpagelink)
		[ "$PACKAGE" ] || croak "No PACKAGE provided"
		mainpagelink "$PACKAGE"
		;
	settingslink)
		[ "$PACKAGE" ] || croak "No PACKAGE provided"
		settingslink "$PACKAGE"
		;
	confvariable)
		[ "$PACKAGE" ] || croak "No PACKAGE provided"
		VARIABLE=$3
		[ "$VARIABLE" ] || croak "No variable name provided"
		confvariable "$PACKAGE" "$VARIABLE"
		;
	echo_ssivar_licensekey)
		[ "$PACKAGE" ] || croak "No PACKAGE provided"
		echo_ssivar_licensekey "$PACKAGE"
		;
	load_auto_inst_form)
		load_auto_inst_form
		;
	get_pre_inst_list)
		get_pre_inst_list
		;
	*)
		croak "Action $ACTION not recognized"
		;
esac

exit 0