#!/bin/bash
#
# ssh	Secure Shell Daemon
#
# chkconfig: 345 80 20
# description: SSH daemon
# processname: sshd
# config:      /etc/ssh/sshd_config

stop () {
	# okay this pisses me off, but it has to be done:
	LOGINS=$(fuser -n tcp 22 | awk '{print $3}')
	if [ -z "$LOGINS" ]; then
	  default_stop
	else
	  echo -e "Cannot stop, there are users logged in!$RESULT_FAIL"
	fi
}

force-restart () {
	$0 stop
	if [ -n "`pidof sshd`" ] ; then
	  sleep 1
	  echo "Sending TERM signal to all ssh daemons..."
	  killall -TERM sshd
	  sleep 2
	  if [ -n "`pidof sshd`" ] ; then
	    echo "Sending KILL signal to all ssh daemons..."
	    killall -KILL sshd
	    sleep 2
	    if [ -n "`pidof sshd`" ] ; then
	      echo -e "Failed to stop all ssh daemons!$RESULT_FAIL"
	      exit 1
	    fi
	  fi
	fi
	$0 start
}

remote-restart () {
	echo "Remote restart of sshd in progress."
	echo "YOU WILL BE DISCONNECTED !!!"
	echo "Please log in again after 5 seconds"
	/bin/bash <<EOF
/etc/init.d/ssh force-restart &
EOF
}

usage () {
	echo "Usage: $0 {start|stop|restart|reload|probe|force-restart|remote-restart|status}"
}

# make stops safe
# [ "$1" == "stop" ] && sleep 1

. /lib/lsb/init-functions

