#!/bin/bash
#
# Start the service coolwsd
start() {
	if ps -C coolwsd >/dev/null
	  then
	    echo "Coolwsd server is running"
	else
          echo -n "Starting coolwsd server: "
	  if [ ! -e /var/log/coolwsd.log ]
            then
              touch /var/log/coolwsd.log
          fi
          chgrp cool /var/log/coolwsd.log
          chmod 664 /var/log/coolwsd.log
          su cool -c "/usr/bin/coolwsd --version --o:sys_template_path=/opt/cool/systemplate --o:child_root_path=/opt/cool/child-roots --o:file_server_root_path=/usr/share/coolwsd --o:logging.file[@enable]=true" >/dev/null 2>&1 &
          ### Create the lock file ###
          touch /var/lock/coolwsd
          echo "success"
	fi
}

# Stop the service coolwsd
stop() {
        echo -n "Stopping coolwsd server: "
        killall coolwsd && echo "stopped"
        ### Now, delete the lock file ###
        rm -f /var/lock/coolwsd
}

### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        ps -C coolwsd >/dev/null && echo "Coolwsd server is running" || echo "Coolwsd server NOT running"
        ;;
  restart|reload)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0
