Added support to pre-stage apps as part of onos-setup-karaf.
Fixed onos-app reinstall command usage.

Change-Id: I1f5eb084a06fb16f7b2db7b61376211c25c4327a
diff --git a/tools/test/bin/onos-app b/tools/test/bin/onos-app
index 0515e83..268fb1b 100755
--- a/tools/test/bin/onos-app
+++ b/tools/test/bin/onos-app
@@ -11,16 +11,40 @@
 export HDR="-HContent-Type:application/octet-stream"
 export curl="curl -sS"
 
+function usage {
+    echo "usage: onos-app {install|install!} <app-file>" >&2
+    echo "       onos-app {reinstall|reinstall!} <app-name> <app-file>" >&2
+    echo "       onos-app {activate|deactivate|uninstall} <app-name>" >&2
+    exit 1
+}
+
 case $cmd in
     list) $curl -X GET $URL;;
-    install) $curl -X POST $HDR $URL --data-binary @$app;;
-    install!) $curl -X POST $HDR $URL?activate=true --data-binary @$app;;
-    reinstall) $curl -X DELETE $URL/$app && $curl -X POST $HDR $URL --data-binary @$app;;
-    reinstall!) $curl -X DELETE $URL/$app && $curl -X POST $HDR $URL?activate=true --data-binary @$app;;
-    uninstall) $curl -X DELETE $URL/$app;;
-    activate) $curl -X POST $URL/$app/active;;
-    deactivate) $curl -X DELETE $URL/$app/active;;
-    *) echo "usage: onos-app {install|install!|reinstall|reinstall!} <app-file>" >&2
-       echo "       onos-app {activate|deactivate|uninstall} <app-name>" >&2
-       exit 1;;
+    install)
+        [ $# -lt 3 -o ! -f $app ] && usage
+        $curl -X POST $HDR $URL --data-binary @$app;;
+    install!)
+        [ $# -lt 3 -o ! -f $app ] && usage
+        $curl -X POST $HDR $URL?activate=true --data-binary @$app;;
+
+    reinstall)
+        [ $# -lt 4  -o ! -f $4 ] && usage
+        $curl -X DELETE $URL/$app
+        $curl -X POST $HDR $URL --data-binary @$4;;
+    reinstall!)
+        [ $# -lt 4  -o ! -f $4 ] && usage
+        $curl -X DELETE $URL/$app
+        $curl -X POST $HDR $URL?activate=true --data-binary @$4;;
+
+    uninstall)
+        [ $# -lt 3 ] && usage
+        $curl -X DELETE $URL/$app;;
+    activate)
+        [ $# -lt 3 ] && usage
+        $curl -X POST $URL/$app/active;;
+    deactivate)
+        [ $# -lt 3 ] && usage
+        $curl -X DELETE $URL/$app/active;;
+
+    *) usage;;
 esac
diff --git a/tools/test/bin/onos-setup-karaf b/tools/test/bin/onos-setup-karaf
index 7cd75a9..930fdf7 100755
--- a/tools/test/bin/onos-setup-karaf
+++ b/tools/test/bin/onos-setup-karaf
@@ -70,9 +70,9 @@
         $KARAF_ROOT/lib
 fi
 
-if [ ! -d $STAGE/apps -o ! -d $STAGE/config ]; then
+if [ ! -d $STAGE/config ]; then
     echo "Creating local cluster configs for IP $IP..."
-    mkdir -p $STAGE/apps $STAGE/config
+    mkdir -p $STAGE/config
     cat > $STAGE/config/cluster.json <<EOF
     { "ipPrefix": "$SUBNET.*",
       "nodes":[ { "id": "$IP", "ip": "$IP", "tcpPort": 9876 }]}
@@ -88,3 +88,20 @@
 cp $ONOS_ROOT/tools/package/etc/hazelcast.xml $KARAF_ROOT/etc/hazelcast.xml
 perl -pi.old -e "s/192.168.56/$SUBNET/" $KARAF_ROOT/etc/hazelcast.xml
 perl -pi.old -e "s/        <name>onos</        <name>$IP</" $KARAF_ROOT/etc/hazelcast.xml
+
+
+echo "Staging builtin apps..."
+rm -fr $STAGE/apps
+mkdir -p $STAGE/apps
+find $ONOS_ROOT -name 'app.xml' | egrep -v '/src/test/|/target/|org\.foo\.' | \
+    xargs grep 'name=' | sed 's/<app name="//g;s/".*//g' | while read line; do
+        appxml=${line%:*}
+        app=${line#*:}
+        mkdir $STAGE/apps/$app
+        cp $appxml $STAGE/apps/$app/app.xml
+    done
+
+echo "Customizing apps to be auto-activated..."
+for app in $(echo $ONOS_APPS | tr ',' ' '); do
+    touch $STAGE/apps/org.onosproject.$app/active
+done