#!/bin/sh
#
# chkconfig: 345 85 15
# description: SSH Secure Shell daemon
#
# Author: Sami Lehtinen <sjl@ssh.com>
#
# sshd2		This shell script takes care of starting and stopping
#               sshd2.
# 
# partly
#
# Copyright (C) 2000 SSH Communications Security Corp, Helsinki, Finland
#
# Most of the code taken from RedHat Linux /etc/rc.d/init.d/httpd, 
# so I guess
#
# Copyright (C) 1999 RedHat, Inc.

[ -f /usr/local/sbin/sshd2 ] || exit 0

PORT=
SSHDPIDFILE=
 
# Get port number
PORT=`grep Port /etc/ssh2/sshd2_config | awk '{ x = $2 } END {print x}' -`
if [ "X$PORT" = "X" ]
then
	PORT=22
fi

# Get sshd2 pid file & pid

if test -f /var/run/sshd2_$PORT.pid
then
  SSHDPIDFILE=/var/run/sshd2_$PORT.pid
elif test -f /etc/ssh2/sshd2_$PORT.pid
then
  SSHDPIDFILE=/etc/ssh2/sshd2_$PORT.pid
fi

if test -n "$SSHDPIDFILE"
then
  SSHD_PID=`cat $SSHDPIDFILE`
fi

# See how we were called.

case "$1" in
  start)
	# Start daemons.
	echo -n "Starting sshd2 in port $PORT: "
	/usr/local/sbin/sshd2
	echo "done."
	;;
  stop)
	# Stop daemons.
        if test -n "$SSHD_PID"
        then
          echo -n "Shutting down sshd2 in port $PORT: "
	  kill $SSHD_PID
          echo "done."
        else
          echo "No sshd2 process found"
        fi

        if test -f "$SSHDPIDFILE"
        then
          rm -f $SSHDPIDFILE
        fi
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
	echo "Usage: sshd2 {start|stop|restart}"
	exit 1
esac

exit 0
