#!/bin/sh
##############################################################################
#
# FILE NAME  : acap_handle_persistent
#
# DESCRIPTION: Manage pre-installed acap applications
#
# ----------------------------------------------------------------------------
# Copyright (C) 2014, Axis Communications AB, LUND, SWEDEN
##############################################################################

THISAPP=${0##*/}


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

INSTALL_PATH=$PACKAGE_DIRECTORY
WS_NEED_RELOAD=
EAP_WANT_WS_RELOAD=

PERSISTENT_STORAGE=/usr/share/packages
AUTOSTART_LIST=
TMP_ADDON_DIR=/tmp/addon
FINALIZE_FILE=finalize

eap_pattern="$PERSISTENT_STORAGE/*.eap"
APPLIST_2=$(echo $eap_pattern)
[ -z "$APPLIST_2" ] || [ "$APPLIST_2" = "$eap_pattern" ] || {
	APPNAME= PACKAGENAME=
	for eap_path in $APPLIST_2; do
		if __pkgconf_get_var "$eap_path" APPNAME; then
			if ! acapctl --syslog verify-signature "$eap_path" >/dev/null 2>&; then
				error "Failed to verify integrity of acap application $APPNAME"
			else
				__pkgconf_get_var "$eap_path" PACKAGENAME || PACKAGENAME=$APPNAME
				if [ ! -d $INSTALL_PATH/$APPNAME ]; then
					name=$APPNAME

					information "$THISAPP : Installing application: $PACKAGENAME"
					__install_eap_file $eap_path EAP_WANT_WS_RELOAD
					result_code=$?
					if [ $result_code -eq 0 ]; then
						__pkgconf_get_var "$eap_path" AUTOSTART || AUTOSTART=n
						if [ "$AUTOSTART" = y ] || [ "$AUTOSTART" = Y ]; then
							AUTOSTART_LIST="$AUTOSTART_LIST $name"
						fi
					else
						error "$THISAPP : $PACKAGENAME installation failed"
					fi
				else
					information "$THISAPP : Checking if there is a newer version of" \
						"application: $PACKAGENAME"
					package_path="/usr/local/packages/${APPNAME}/package.conf"
					eval $(grep APPMAJORVERSION $package_path) ||
						error "$THISAPP : Could not parse package conf from $eap_path"
					[ "$APPMAJORVERSION" ] || APPMAJORVERSION=0
					eval $(grep APPMINORVERSION $package_path) ||
						error "$THISAPP : Could not parse package conf from $eap_path"
					[ "$APPMINORVERSION" ] || APPMINORVERSION=0
					eval $(grep APPMICROVERSION $package_path) ||
						error "$THISAPP : Could not parse package conf from $eap_path"
					[ "$APPMICROVERSION" ] || APPMICROVERSION=0
					installed_major=$APPMAJORVERSION
					installed_minor=$APPMINORVERSION
					installed_micro=$APPMICROVERSION
					__pkgconf_get_var "$eap_path" APPMAJORVERSION ||
						error "$THISAPP : Could not parse package conf from $eap_path"
					__pkgconf_get_var "$eap_path" APPMINORVERSION ||
						error "$THISAPP : Could not parse package conf from $eap_path"
					__pkgconf_get_var "$eap_path" APPMICROVERSION ||
						error "$THISAPP : Could not parse package conf from $eap_path"
					[ "$APPMICROVERSION" ] || APPMICROVERSION=0
					__pkgconf_compare_versions $APPMAJORVERSION $APPMINORVERSION \
						$APPMICROVERSION $installed_major $installed_minor \
						$installed_micro || {
						information "$THISAPP : Installing newer version of application:" \
							"$PACKAGENAME"
						__install_eap_file "$eap_path" EAP_WANT_WS_RELOAD || :
					}
				fi
				if [ $EAP_WANT_WS_RELOAD ] && [ -z $WS_NEED_RELOAD ]; then
					WS_NEED_RELOAD=y
				fi
			fi
		else
			error "$THISAPP : Could not read configuration for pre-installed ACAP application"
		fi
		__pkgconf_unset
		unset eap_path APPNAME AUTOSTART name EAP_WANT_WS_RELOAD
	done
}

if [ "$WS_NEED_RELOAD" = y ]; then
	[ -d $TMP_ADDON_DIR ] || mkdir $TMP_ADDON_DIR || warning "$THISAPP : Failed create $TMP_ADDON_DIR"
	>$TMP_ADDON_DIR/$FINALIZE_FILE || warning "$THISAPP : Failed create $TMP_ADDON_DIR/$FINALIZE_FILE"
fi

echo "$AUTOSTART_LIST"