#!/bin/sh

SSH_KEYGEN=/usr/sbin/ssh-keygen
SSHD=/usr/sbin/sshd.me

DSA_KEY=/storage/ssh.me/ssh_host_dsa_key
RSA_KEY=/storage/ssh.me/ssh_host_rsa_key
ECDSA_KEY=/storage/ssh.me/ssh_host_ecdsa_key

if ! [ -x ${SSHD} ]; then
 exit 0
fi
if [ ! -d /storage/ssh.me ]; then
  mkdir -p /storage/ssh.me
fi
# Check for keys in /storage/ssh.me
if ! [ -f ${DSA_KEY} ] ; then
   # Generate DSA key
   echo "Generating Secure Shell DSA Host Key ..."
   ssh-keygen -q -t dsa -f ${DSA_KEY} -C Airespace_Switch -N ""
fi
if ! [ -f ${RSA_KEY} ] ; then
   # Generate RSA key
   echo "Generating Secure Shell RSA Host Key ..."
   ssh-keygen -q -t rsa -f ${RSA_KEY} -C Airespace_Switch -N ""
fi
if ! [ -f ${ECDSA_KEY} ] ; then
   # Generate sshv1 key
   echo "Generating Secure Shell version 2 ECDSA Host Key ..."
   ssh-keygen -q -t ecdsa -f ${ECDSA_KEY} -C Airespace_Switch -N ""
fi

case "$1" in
 start)
    # Enable syslog debug in mwar/utils/openssh/sshd_config
    # and uncomment the lines below to log sshd debug messages
    # rm -f /mnt/application/syslog*
    # ln -s /bin/busybox /usr/sbin/syslogd
    # /usr/sbin/syslogd -O /mnt/application/syslog.log
    ${SSHD} -f /etc/ssh/sshd_config.me
  ;;
 stop)
  killall sshd.me 2>/dev/null
  ;;
 restart)
  $0 stop
  $0 start
  ;;
esac
