#! /bin/sh
### BEGIN INIT INFO
# Provides:          3dprinteros
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: 3DPrinterOS Client
# Description: a client for 3DPrinterOS - cloud 3D printing software
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

#. /lib/lsb/init-functions
_3DPRINTEROS_PID=/tmp/3dprinteros.pid
# this path is used for Creality K1Max
_3DPRINTEROS_DIR=/usr/data/3dprinteros-client
_3DPRINTEROS_PY=$_3DPRINTEROS_DIR/launcher.py
_3DPRINTEROS_PYTHON=python3

case "$1" in
  start)
        start-stop-daemon -S -q -b -m -p $_3DPRINTEROS_PID -d $_3DPRINTEROS_DIR $_3DPRINTEROS_PYTHON $_3DPRINTEROS_PY
        ;;
  stop)
        if [ -f $_3DPRINTEROS_PID ]; then
            kill `cat $_3DPRINTEROS_PID`
        fi
        ;;

  restart|reload|force-reload)
        kill `cat $_3DPRINTEROS_PID`
        while [ -f $_3DPRINTEROS_PID ] && [ -n "$(ps ax | grep -e "^\s*"`cat $_3DPRINTEROS_PID` )" ]; do sleep 1s; echo "waiting for 3DPrinterOS to stop"; done  
        start-stop-daemon -S -q -b -m -p $_3DPRINTEROS_PID -d $_3DPRINTEROS_DIR $_3DPRINTEROS_PYTHON $_3DPRINTEROS_PY
        ;;
  *)
        # echo "Usage: $0 start|stop" >&2
        # exit 3
        ;;
esac
