#!/bin/sh -e

port=830
ncxserver_sockname=/tmp/ncxserver.sock

args=$@

while [ $# -gt 0 ]; do
  case "$1" in
    --port=*)
      port="${1#*=}"
      ;;
    --ncxserver-sockname=*)
      ncxserver_sockname="${1#*=}"
      ;;
  esac
  shift
done

sshd_cfg_filename=${ncxserver_sockname}.sshd_config

kill -KILL `fuser $ncxserver_sockname 2>/dev/null` || true
kill -KILL `fuser --ipv4 -v -n tcp $port 2>/dev/null` || true
rm $ncxserver_sockname $sshd_cfg_filename || true

#run dedicated sshd
./run-sshd-for-netconfd $port $ncxserver_sockname $sshd_cfg_filename

/usr/sbin/netconfd $args
