#!/bin/sh
### BEGIN INIT INFO
# Provides:          images
# Required-Start:    $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       ImageButler
### END INIT INFO

EXEC=/opt/images/bin/flask
EXEC_OPTS="run -h web-vi01 -p 5000"
EXEC_STOP=/opt/images/bin/python3

RUNAS=images
PIDFILE=/var/run/images.pid

start() {
  echo 'Starting service...' >&2
  FLASK_APP=imagebutler IMAGEBUTLER_CONFIGS=/opt/images/etc/config.cnf start-stop-daemon \
    --start --chuid "$RUNAS" --background --make-pidfile --pidfile $PIDFILE \
    --exec $EXEC -- $EXEC_OPTS
}

stop() {
  echo 'Stopping service...' >&2
  start-stop-daemon --stop --user "$RUNAS" --pidfile $PIDFILE --exec $EXEC_STOP
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
esac
