#!/bin/sh

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars

# Mono processes started by the devicestack or any of its childs should only use 16M heap
export MONO_GC_PARAMS=max-heap-size=16M

case "$1" in
  start)
    echo -n "Starting devicestack ... "
    # Do not use the ${DS_LOGFILE} to log the output b/c it is not logrotated.
    # This can lead to a system that does not have free space in the tmpfs /var partition.
    # Proper operation is not guaranteed in that case and logfiles cannot be downloaded anymore.
    # The log file can be used again if there is a means to logrotate at a maximum size.
    #${DEVICESTACK} > ${DS_LOGFILE} 2>&1 &
    #echo $! > ${DS_PIDFILE}

    # We use the system logger until there is a better way to logrotate ${DS_LOGFILE}
    # To have the content in /var/log/devicestack.log, we log to local0.notice facility
    # and configure a rule for that to the log file in /etc/syslog.conf.
    ${DEVICESTACK} 2>&1 | logger -t devicestack -p local0.notice &
    echo $(($! - 1)) > ${DS_PIDFILE}
    echo $!
    echo 10 > /proc/$(cat ${DS_PIDFILE})/oom_adj
    ;;
  stop)
    echo -n "Stopping DeviceStack ... "
    if start-stop-daemon --stop --quiet --pidfile ${DS_PIDFILE}
    then
      echo "done."
    else
      echo "failed."
    fi
    ;;
  *)
    echo "Usage: $0 (start|stop)"
    exit 1
esac

exit 0
