#!/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
# We use nursery-size=1M and soft-heap-limit=4M to ensure that the GC is run regularly.
export MONO_GC_PARAMS=max-heap-size=16M,nursery-size=1M,soft-heap-limit=4M

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 $!
	# Set oom_score_adj for GDS PID: This value is added to the computed oom_score to determine, if this process is killed in case of OOM.
	# Allowed ranges [-1000, 1000].
    echo 700 > /proc/$(cat ${DS_PIDFILE})/oom_score_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
