#!/bin/sh

# Debian Linux system service run script (init file) for Synchronet
#
# This file normally goes in your /etc/init.d directory
#
# $Id: sbbs.debian,v 1.4 2011/10/17 09:00:36 sbbs Exp $

### BEGIN INIT INFO
# Provides:          sbbs
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop Synchronet BBS services
# X-Interactive:     false
### END INIT INFO

########################################
# Synchronet BBS Debian startup script #
########################################
# by Karloch <karloch@hispamsx.org> 8 Dec 2005

# You can edit these vars to meet your system configuration

PATH=/bin:/sbin:/usr/bin:/usr/bin
SBBSROOT=/sbbs
DAEMON=$SBBSROOT/exec/sbbs

# Command Line options (e.g. daemonize)
OPTIONS="-d"

export SBBSCTRL=$SBBSROOT/ctrl
export SHELL=/bin/bash
# Uncomment if you want core files created with an unlimited size:
#ulimit -c unlimited

# DO NOT edit anything below this line unless you know what you doing

test -x $DAEMON || exit 0

case "$1" in
  start)
      echo -n "Starting Synchronet BBS services... "
      if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
      then
          echo "[already running]"
      else
          start-stop-daemon --quiet --start --exec $DAEMON -- $OPTIONS
      fi
      ;;
  stop)
      echo -n "Stopping Synchronet BBS services... "
      if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
      then
          if start-stop-daemon --quiet --stop --exec $DAEMON --retry 300
	  then
	  	echo "[OK]"
	  else
		echo "[timeout]"
	  fi
      else
          echo "[not running]"
      fi
      ;;
  restart|force-reload)
      $0 stop
      $0 start
      ;;
  status)
      echo -n "Synchronet BBS services status: "
      if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
      then
          echo "[running]"
	  echo -n "PID(s): "
	  pidof sbbs
      else
          echo "[not running]"
      fi
      ;;
  recycle)
      echo -n "Putting recycle server mark on Synchronet: "
      if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
      then
          touch $SBBSCTRL/recycle
	  echo "[OK]"
      else
          echo "[FAILED]"
	  echo -n "Synchronet server doesn't seem to be running"
      fi
      ;;
  *)
      echo "Usage: /etc/init.d/sbbs {start|stop|restart|force-reload|status|recycle}"
      exit 1

esac

exit 0