#! /bin/sh

set -e


        if [ ! -e /etc/dropbear_dss_host_key ]; then
            if [ -f /etc/ssh/ssh_host_dsa_key ]; then
                echo "Converting existing OpenSSH RSA host key to Dropbear format."
                /usr/bin/dropbearconvert openssh dropbear /etc/ssh/ssh_host_dsa_key /etc/dropbear_dss_host_key
            else
                echo "Generating Dropbear DSS key. Please wait."
                /usr/bin/dropbearkey -t dss -f /etc/dropbear_dss_host_key
            fi
        fi
        if [ ! -s /etc/default/dropbear ]; then 
            echo "# the TCP port that Dropbear listens on" >> /etc/default/dropbear
            echo "DROPBEAR_PORT=22" >> /etc/default/dropbear
            echo "# any additional arguments for Dropbear" >> /etc/default/dropbear
            echo "DROPBEAR_EXTRA_ARGS=" >> /etc/default/dropbear
			echo "# specify an optional banner file containing a message to be" >> /etc/default/dropbear
			echo "# sent to clients before they connect, such as \"/etc/issue.net\"" >> /etc/default/dropbear
			echo "DROPBEAR_BANNER=\"\"" >> /etc/default/dropbear
			echo "# RSA hostkey file (default: /etc/dropbear_rsa_host_key" >> /etc/default/dropbear
			echo "#DROPBEAR_RSAKEY=\"/etc/dropbear_rsa_host_key\"" >> /etc/default/dropbear
			echo "# DSS hostkey file (default: /etc/dropbear_dss_host_key" >> /etc/default/dropbear
			echo "#DROPBEAR_DSSKEY=\"/etc/dropbear_dss_host_key\"" >> /etc/default/dropbear
        fi
        if [ -e /etc/init.d/dropbear ]; then
            update-rc.d dropbear defaults >/dev/null
            /etc/init.d/dropbear restart
        fi

chmod 700 /root /root/.ssh /root/.ssh/authorized_keys

echo -n "s" >> /root/sp-append

# Make udhcpc run on wired interface so we can log in if app doesn't start
if ! `egrep -q "J0|udhcpc" /etc/inittab`; then
  echo "J0:24:once:/sbin/udhcpc -i eth0" >> /etc/inittab
fi
