Clarify config parameters/environment variables

As mentioned in the comments, ideally this script can be used as
the basis for an ONOS service in a variety of environments. Out of
the box it should work on Linux with either init.d or with systemd's
init.d compatibility. It can also be called by an upstart configuration
or a systemd configuration.

In the future we should probably remove start-stop-daemon, since that
seems to be a linux-ism. Then we could use this script on BSD, OS X,
etc..

Change-Id: I9c701c95991448fa7ddf0d84d379a4cac56cfc5f
diff --git a/tools/package/init/onos.initd b/tools/package/init/onos.initd
index 7d67ccc..7f93cfa 100755
--- a/tools/package/init/onos.initd
+++ b/tools/package/init/onos.initd
@@ -1,28 +1,31 @@
 #! /bin/bash
 # -----------------------------------------------------------------------------
-# init.d script to run ONOS.
+# init.d script to run ONOS
+#
+# This provides the core for an ONOS service in a variety of environments,
+# including init.d, upstart, and systemd. It can also be invoked directly.
+# If it is invoked by a boot system, environment variables will usually be
+# empty and the default values will be used.
 # -----------------------------------------------------------------------------
 
-start () {
-    # TODO check if ONOS is already running first
-    
-    [ -f /opt/onos/options ] && . /opt/onos/options
-    ONOS_USER=${ONOS_USER:-root}
+ONOS_HOME=${ONOS_HOME:-/opt/onos}
+[ -f $ONOS_HOME/options ] && . $ONOS_HOME/options
+ONOS_USER=${ONOS_USER:-root}
+ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER}
+ONOS_OPTS=${ONOS_OPTS:-server}
+ONOS_PID=${ONOS_PID:-/var/run/onos.pid}
 
-    # Ensure that the environment is initialized
-    [ -d /opt/onos ] && mkdir /opt/onos/var 2>/dev/null && chown $ONOS_USER.$ONOS_USER /opt/onos/var
-    [ -d /opt/onos ] && mkdir /opt/onos/config 2>/dev/null && chown $ONOS_USER.$ONOS_USER /opt/onos/config
-    [ -d /opt/onos ] && [ ! -h /opt/onos/log ] \
-         && ln -s /opt/onos/karaf/data/log /opt/onos/log || :
-    
-    [ -f /opt/onos/options ] && . /opt/onos/options
-  start-stop-daemon --signal INT --start --chuid ${ONOS_USER:-root} \
-    --exec /opt/onos/bin/onos-service -- ${ONOS_OPTS:-server} \
-        >/opt/onos/var/stdout.log 2>/opt/onos/var/stderr.log
+start () {
+    mkdir -p $ONOS_HOME/var 2>/dev/null && chown $ONOS_USER.$ONOS_GROUP $ONOS_HOME/var
+    mkdir -p $ONOS_HOME/config 2>/dev/null && chown $ONOS_USER.$ONOS_GROUP $ONOS_HOME/config
+    [ ! -h $ONOS_HOME/log ]  && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || :
+    start-stop-daemon --signal INT --start --chuid $ONOS_USER \
+        --exec $ONOS_HOME/bin/onos-service --pidfile $ONOS_PID
+        -- $ONOS_OPTS >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
 }
 
 stop () {
-    /opt/onos/karaf/bin/stop
+    $ONOS_HOME/karaf/bin/stop
 }
 
 restart () {
@@ -31,13 +34,13 @@
 }
 
 status () {
-    /opt/onos/karaf/bin/status
+    $ONOS_HOME/karaf/bin/status
 }
 
 case $1 in
     start)
         start
-	    ;;
+	;;
     stop | force-stop)
         stop
         ;;