#!/bin/sh -e

# Configure networking for autostart
IF=`route -n | grep ^0.0.0.0 | awk ' { print $8 ; } ' | tail -1`
if [ -z $IF ] ; then
 echo No default route, not configuring ncad network
 exit 0
fi

IP=`ifconfig $IF | grep inet.addr | sed 's/:/ /g' | awk ' { print $3 ; } '`
BC=`ifconfig $IF | grep inet.addr | sed 's/:/ /g' | awk ' { print $5 ; } '`
NM=`ifconfig $IF | grep inet.addr | sed 's/:/ /g' | awk ' { print $7 ; } '`
GW=`route -n | grep ^0.0.0.0 | awk ' { print $2 ; } ' | tail -1`

if [ ! -s /etc/default/ncad ] ; then
cat <<EOF >/etc/default/ncad
# Settings for networking to be brought up
# ahead of time
# Interface
IF=$IF
# IP address
IP=$IP
# Broadcast
BC=$BC
# Netmask
NM=$NM
# Default Gateway
GW=$GW
EOF
fi


if [ -f /etc/ssh/ssh_host_dsa_key ] && [ ! -f /etc/ncad/ncad_host_dss_key ] ; then
  /usr/lib/ncad/dropbearconvert openssh dropbear /etc/ssh/ssh_host_dsa_key /etc/ncad/ncad_host_dss_key
 chmod 0600 /etc/ncad/ncad_host_dss_key
fi

if [ -f /etc/ssh/ssh_host_rsa_key ] && [ ! -f /etc/ncad/ncad_host_rsa_key ]; then
  /usr/lib/ncad/dropbearconvert openssh dropbear /etc/ssh/ssh_host_rsa_key /etc/ncad/ncad_host_rsa_key
  chmod 0600 /etc/ncad/ncad_host_rsa_key
fi

update-rc.d ncad start 01 S 1 .

