#!/bin/sh

if [ ! -x /usr/sbin/dropbear ]
then
    exit 0
fi

if [ "$1" = "stop" -o "$1" = "restart" ]
then                                                                            
    echo "Stopping the dropbear ssh server: "
    killall dropbear
fi

if [ "$1" = "start" -o "$1" = "restart" ]                                       
then
    # This generates the persistent host key in writeable NAND flash area
    if [ ! -s /opt/etc/dropbear/dropbear_rsa_host_key ]; then
       /bin/echo "Generating keys for the dropbear ssh server: "
       /bin/mkdir -p /opt/etc/dropbear
       /usr/bin/dropbearkey -t rsa -f /opt/etc/dropbear/dropbear_rsa_host_key
    fi
    
    # rootfs is ro > copy host key to temp. writeable /etc dir. at every start
    /bin/mkdir -p /etc/dropbear
    /bin/cp /opt/etc/dropbear/dropbear_rsa_host_key /etc/dropbear/
    
    echo "Starting the dropbear ssh server: "
    /usr/sbin/dropbear $DROPBEAR_ARGS
fi
