#!/lib/init/init-d-script
### BEGIN INIT INFO
# Provides:          shellinabox
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Shell In A Box Daemon
# Description:       Daemon for publishing a login shell at
#                    http://localhost:PORT
#		     where default port number is 4200.
### END INIT INFO

# Authors: Markus Gutschke <markus@shellinabox.com>, Marc Singer <elf@buici.com>

DESC="Shell In A Box Daemon"
NAME="shellinabox"
DAEMON="/usr/bin/shellinaboxd"
COMMAND_NAME="shellinaboxd"
PIDFILE="/run/shellinaboxd.pid"

# Set some default values
CERTDIR="/var/lib/shellinabox"
PORT="4200"

do_start_prepare() {
  [ -d "$CERTDIR" ] || install -m 755 -o "$U" -g "$G" -d "$CERTDIR"
  DAEMON_ARGS="${DAEMON_ARGS:---background --pidfile $PIDFILE -u shellinabox -g shellinabox -c $CERTDIR -p $PORT --user-css-dir /etc/shellinabox/options-enabled $OPTS}"
}
do_restart_prepare() { do_start_prepare; }

#
#       Function that reloads the config file for the daemon/service.
#
do_reload_cmd() {
  # Only reload if there are no active sessions running
  [ -r "$PIDFILE" ] &&
    [ `ps o pid= --ppid "\`cat "$PIDFILE"\`\`ps o pid= --ppid \
                                                \\\`cat "$PIDFILE"\\\`|
                                             xargs -r -n 1 printf ',%s'\`" |
       wc -l` -gt 1 ] &&
    return 1

  do_stop
  do_start
}
