#!/bin/sh
# sshd/rc.main
# perp runscript for sshd service
# wcm, 2009.10.07 - 2011.02.02
# ===

exec 2>&1

TARGET=${1}
SVNAME=${2:-sshd}

## generate keys:
make_keys() {
  if test ! -f /etc/ssh/ssh_host_key ; then
     echo "*** ${SVNAME}: generating /etc/ssh/ssh_host_key ..."
     /usr/bin/ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N ''
  fi
  if test ! -f /etc/ssh/ssh_host_dsa_key ; then
     echo "*** ${SVNAME}: generating /etc/ssh/ssh_host_dsa_key ..."
     /usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
  fi
  if test ! -f /etc/ssh/ssh_host_rsa_key ; then
     echo "*** ${SVNAME}: generating /etc/ssh/ssh_host_rsa_key ..."
     /usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  fi
}


## start target:
start() {
  echo "*** ${SVNAME}: starting sshd..."
  make_keys
  ## sshd options required for perp service:
  ##   -D  no detach, run in foreground
  ##   -e  log to stderr (for tinylog) instead of syslog
  ##
  exec \
    /usr/sbin/sshd -D -e -f /etc/ssh/sshd_config
}


## reset target:
reset() {
  case $3 in
    'exit' )
        echo "*** ${SVNAME}: exited status $4" ;;
    'signal' )
        echo "*** ${SVNAME}: killed on signal $5" ;;
    * ) 
        echo "*** ${SVNAME}: stopped ($3)" ;;
  esac 
  exit 0
}


## branch to target:
eval ${TARGET} "$@"

### EOF
