#!/bin/sh

. /etc/default/lnsa.sh

SERVICE_BASE=$LNSA_SERVICES_DIR
ROLE_BASE=$LNSA_ROLES_DIR
JOB_BASE=$LNSA_JOBS_DIR
APP_ROOT=/media/cryptofs/apps
APP_BASE=$APP_ROOT/usr/palm/applications

find_appbase() {
    appid="$1"
    appbase="$APP_BASE/$appid"
}


if [ "$#" = 1 -a "$1" = "--root-dir" ] ; then
    echo $APP_ROOT
elif [ "$#" = 2 -a "$1" = "--app-dir" ] ; then
    find_appbase "$2"
    echo "$appbase"
elif [ "$#" -ge 3 -a "$1" = "--app-run" ] ; then
    find_appbase "$2"
    execfile="$appbase/$3"
    shift
    shift
    shift
    exec $execfile "$@"
elif [ "$#" = 3 -a "$1" = "--add-job-file" ] ; then
    find_appbase "$2"
    jobfile="$appbase/$3"
    jobname="`basename $3`"
    mkdir -p "$JOB_BASE/"
    cp "$jobfile" "$JOB_BASE/$jobname"
elif [ "$#" = 3 -a "$1" = "--remove-job-file" ] ; then
    find_appbase "$2"
    jobfile="$appbase/$3"
    jobname="`basename $3`"
    stop "$jobname" 2> /dev/null || false # stop job if it is running
    rm -f "$JOB_BASE/$jobname"
elif [ "$#" = 3 -a "$1" = "--add-service-file" ] ; then
    find_appbase "$2"
    servicefile="$appbase/$3"
    servicename="`basename $3`"
    cp "$servicefile" "$SERVICE_BASE/$servicename"
    ls-control scan-services #ls2: rescan service and role directories
elif [ "$#" = 3 -a "$1" = "--remove-service-file" ] ; then
    find_appbase "$2"
    servicefile="$appbase/$3"
    servicename="`basename $3`"
    rm -f "$SERVICE_BASE/$servicename"
    ls-control scan-services #ls2: rescan service and role directories
elif [ "$#" = 3 -a "$1" = "--add-role-file" ] ; then
    find_appbase "$2"
    rolefile="$appbase/$3"
    rolename="`basename $3`"
    cp "$rolefile" "$ROLE_BASE/$rolename"
    ls-control scan-services #ls2: rescan service and role directories
elif [ "$#" = 3 -a "$1" = "--remove-role-file" ] ; then
    find_appbase "$2"
    rolefile="$appbase/$3"
    rolename="`basename $3`"
    rm -f "$SERVICE_BASE/$rolename"
    ls-control scan-services #ls2: rescan service and role directories
elif [ "$#" -ge 2 -a "$1" = "--version-check" ] ; then
    BUILDNUM=$(lunaprop com.palm.properties.buildNumber -m)
    BUILDNAME=$(lunaprop com.palm.properties.buildName -m)
    BUILDVERS=$(lunaprop com.palm.properties.version -m | sed 's/^.* //')
    match=0
    shift
    while [ -n "$1" ]; do
        if [ "x$1" = "x--version" ] ; then
            shift
            if [ "x$1" = "xinternal" ] ; then
                case "$BUILDVERS" in
                    [a-zA-Z]* ) match=1 ;;
                esac
            else
                case "$BUILDVERS" in
                    "$1" | "$1".* ) match=1 ;;
                esac
            fi
            shift
        elif [ "x$1" = "x--number" ] ; then
        	shift
        	ARG="$1"
        	if [ "x${ARG/*:*/hasname}" = "xhasname" ] ; then
    	    	NAME=${ARG/:*/}
    	    	RANGE=${ARG/*:/}
    	    else
    	    	NAME=""
    	    	RANGE=${ARG}
    	    fi
        	MIN=${RANGE/-*/}
        	MAX=${RANGE/*-/}
        	if [ -z "$NAME" -o "x$NAME" == "x$BUILDNAME" ] && [ -z "$MIN" -o "$BUILDNUM" -ge "$MIN" ] && [ -z "$MAX" -o "$BUILDNUM" -le "$MAX" ] ; then
        		match=1
        	fi
        	shift
        else
        	break
        fi
    done
    if [ $match -eq 0 ] ; then
    	# failure, no match
    	exit 1
    else
    	# success: run command, if supplied
    	if [ "$#" -gt 0 ] ; then
    		exec "$@"
    	fi	    
    	exit 0
    fi
else 
    echo "Usage: $0 ...any of..."
    echo "          --root-dir"
    echo "                       Prints to stdout the root path for installed application packages"
    echo ""
    echo "          --app-dir <appid>"
    echo "                       Prints to stdout the base path for a particular installed application"
    echo ""
    echo "          --app-run <appid> <app-relative-cmd> [args...]"
    echo "                       Executes a program that is stored within an application package."
    echo ""
    echo "          --add-job-file <appid> <app-relative-jobfile-path>"
    echo "                       Installs an upstart job file that is stored within an application"
    echo "                       package."
    echo ""
    echo "          --remove-job-file <appid> <app-relative-jobfile-path>"
    echo "                       Removes an upstart job file that was installed from an application"
    echo "                       package. The passed parameters should be identical to --add-job-file."
    echo ""
    echo "          --add-service-file <appid> <app-relative-servicefile-path>"
    echo "                       Installs a service launcher file that is stored within an application"
    echo "                       package."
    echo ""
    echo "          --remove-service-file <appid> <app-relative-servicefile-path>"
    echo "                       Removes a service launcher file that was installed from an application"
    echo "                       package. The passed parameters should be identical to --add-service-file."
    echo ""
    echo "          --add-role-file <appid> <app-relative-servicefile-path>"
    echo "                       Installs a service role file that is stored within an application"
    echo "                       package."
    echo ""
    echo "          --remove-role-file <appid> <app-relative-servicefile-path>"
    echo "                       Removes a service role file that was installed from an application"
    echo "                       package. The passed parameters should be identical to --add-role-file."
    echo ""
    echo "          --version-check [--number [<name>:][<value>[-<max>]]|--version <name>|internal]] [cmd args...]"
    echo "                       Checks system version for a match to specified ranges. If there is a"
    echo "                       match to any supplied version, then the command succeeds, and executes"
    echo "                       a command, if that is supplied. If there is no match, the command returns"
    echo "                       failure, and does not execute any supplied command."
    echo "                       The --number argument takes an optional build name, and a single build number"
    echo "                       or range, and matches any build number in that range. The name must match, if"
    echo "                       if supplied."
    echo "                       The --version argument takes a single build version, and matches that exact"
    echo "                       build version, or a build version that is the same plus additional dot"
    echo "                       revisions. (I.e., '--version 1.3.5' will match 1.3.5, 1.3.5.0, or 1.3.5.2.6)."
    echo "                       Other than dot revisions, version matches are exact, and will not accept"
    echo "                       higher or lower."
    echo "                       If 'internal' is passed to --version it matches any internal build, but will"
    echo "                       not match on any product build: an explicit --version argument is needed to"
    echo "                       match those."
    echo ""
fi
