# -*- mode: shell-script; -*-

start on stopped configurator

script
    IPK_DIR=/usr/palm/ipkgs
    TMP_DIR=/media/internal/downloads
    # create $TMP_DIR in case there was a hard reset
    mkdir -p $TMP_DIR
    cd $IPK_DIR
    for package in *.ipk
    do
        echo "$UPSTART_JOB: attempt to install $package" | logger

        # package must be installed from writable location because the
        # installer creates a tmp directory in the same location (NOV-116838)
        cp $package $TMP_DIR
        
        # use luna-send so that app shows up in launcher when LunaSysMgr
        # is not in minimal mode
        # Comment out for now since we can scan newly installed pkgs
        # using the applicationManager.
        #luna-send -n 1 palm://com.palm.appinstaller/installNoVerify \
        #    '{"systemMode":true, "target": "'$TMP_DIR/$package'"}'
        
        ipkg -o /media/cryptofs/apps install $package | logger

        # Get the pkg name from the file name.
        local bname=${package##*/}
        local rname=${bname%.ipk}
        local pkg=${rname%%_*}

        echo "$UPSTART_JOB: attempt to scan $pkg" | logger
        
        # Have the applicationManager rescan the pkg so that it will 
        # show up in the launcher.        
        returnVal=$(luna-send -n 1 palm://com.palm.applicationManager/forceSingleAppScan \
            '{"id":"'$pkg'"}' 2>&1)                                                                                                            
                                                                                          
        logger "$UPSTART_JOB: returnVal is $returnVal"         
    done
end script
