#!/bin/sh

set -u

. /etc/default/lnsa.sh

SERVICE_BASE=$LNSA_SERVICES_DIR
ROLE_BASE_PUB=$LNSA_ROLES_DIR/pub
ROLE_BASE_PRV=$LNSA_ROLES_DIR/prv
JOB_BASE=$LNSA_JOBS_DIR
APP_ROOT=/media/cryptofs/apps
APP_BASE=$APP_ROOT/usr/palm/applications

usage() {
    [ $# -eq 1 ] && echo "$0: error: $1"
    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-public-role-file <appid> <app-relative-servicefile-path>"
    echo "                       Installs a public service role file that is stored within an application"
    echo "                       package."
    echo ""
    echo "          --add-private-role-file <appid> <app-relative-servicefile-path>"
    echo "                       Installs a private service role file that is stored within an application"
    echo "                       package."
    echo ""
    echo "          --remove-public-role-file <appid> <app-relative-servicefile-path>"
    echo "                       Removes a public service role file that was installed from an application"
    echo "                       package. The passed parameters should be identical to --add-public-role-file."
    echo ""
    echo "          --remove-private-role-file <appid> <app-relative-servicefile-path>"
    echo "                       Removes a private service role file that was installed from an application"
    echo "                       package. The passed parameters should be identical to --add-private-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 ""
    exit 1
}

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

[ "$#" -lt 1 ] && usage
CMD=$1
shift

case "$CMD" in
    --root-dir)
        [ "$#" -gt 0 ] && usage
        echo $APP_ROOT
        ;;
    --app-dir)
        [ "$#" -ne 1 ] && usage
        find_appbase "$1"
        echo "$appbase"
        ;;
    --app-run)
        [ "$#" -lt 2 ] && usage
        find_appbase "$1"
        execfile="$appbase/$2"
        shift 2
        exec $execfile "$@"
        ;;
    --add-job-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        jobfile="$appbase/$2"
        jobname="`basename $2`"
        mkdir -p "$JOB_BASE/"
        cp "$jobfile" "$JOB_BASE/$jobname"
        ;;
    --remove-job-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        jobfile="$appbase/$2"
        jobname="`basename $2`"
        stop "$jobname" 2> /dev/null || false # stop job if it is running
        rm "$JOB_BASE/$jobname" || echo "jobfile \"$jobname\" not found"
        ;;
    --add-service-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        servicefile="$appbase/$2"
        servicename="`basename $2`"
        cp "$servicefile" "$SERVICE_BASE/$servicename"
        ls-control scan-services #ls2: rescan service and role directories
        ;;
    --remove-service-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        servicefile="$appbase/$2"
        servicename="`basename $2`"
        rm -f "$SERVICE_BASE/$servicename"
        ls-control scan-services #ls2: rescan service and role directories
        ;;
    --add-public-role-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        rolefile="$appbase/$2"
        rolename="`basename $2`"
        cp "$rolefile" "$ROLE_BASE_PUB/$rolename"
        ls-control scan-services #ls2: rescan service and role directories
        ;;
    --add-private-role-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        rolefile="$appbase/$2"
        rolename="`basename $2`"
        cp "$rolefile" "$ROLE_BASE_PRV/$rolename"
        ls-control scan-services #ls2: rescan service and role directories
        ;;
    --remove-public-role-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        rolefile="$appbase/$2"
        rolename="`basename $2`"
        rm -f "$ROLE_BASE_PUB/$rolename"
        ls-control scan-services #ls2: rescan service and role directories
        ;;
    --remove-private-role-file)
        [ "$#" -ne 2 ] && usage
        find_appbase "$1"
        rolefile="$appbase/$2"
        rolename="`basename $2`"
        rm -f "$ROLE_BASE_PRV/$rolename"
        ls-control scan-services #ls2: rescan service and role directories
        ;;
    --version-check)
        [ "$#" -lt 1 ] && usage
        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
        while [ "$#" -gt 0 ]; do
            if [ "$1" = "--version" ] ; then
                shift
                [ $# -lt 1 ] && usage "--version requires an argument"
                if [ "$1" = "internal" ] ; then
                    case "$BUILDVERS" in
                        [a-zA-Z]* ) match=1 ;;
                    esac
                else
                    case "$BUILDVERS" in
                        "$1" | "$1".* ) match=1 ;;
                    esac
                fi
                shift
            elif [ "$1" = "--number" ] ; then
        	    shift
                [ $# -lt 1 ] && usage "--number requires an argument"
        	    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
        ;;
    *)
        usage
        ;;
esac