#!/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=
AUTOSTART_FILE=/etc/addon/conf/acapautostart
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
			__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"
				eval $(grep APPMAJORVERSION /usr/local/packages/$APPNAME/package.conf) || error "$THISAPP : Could not parse package conf from $eap_path"
				eval $(grep APPMINORVERSION /usr/local/packages/$APPNAME/package.conf) || error "$THISAPP : Could not parse package conf from $eap_path"
				eval $(grep APPMICROVERSION /usr/local/packages/$APPNAME/package.conf) || 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
		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

rm -f $AUTOSTART_FILE.tmp
[ ! -f $AUTOSTART_FILE ] || cp $AUTOSTART_FILE $AUTOSTART_FILE.tmp ||
	error "Failed to keep existing autostart file"
for autostart in $AUTOSTART_LIST; do
	echo $autostart
done >>$AUTOSTART_FILE.tmp
[ ! -f $AUTOSTART_FILE.tmp ] || fsynced_write_or_cleanup $AUTOSTART_FILE.tmp $AUTOSTART_FILE