#! /bin/sh

# Script to invoke the Linux Jnos.  Usage: dojnos [optional_args]
# User should edit definitions for jnosroot and autoexec, and edit the
# $local and $remote addresses and $mtu value in the DIP script below:

jnosroot=~jpd/jnos
autoexec=autoexec.nos

if [ $TERM = xterm ]; then
	TERM=xterm-color	# xterms is another possibility
fi
tset 2> /dev/null
cd $jnosroot
rm spool/mqueue/*.lck 2>/dev/null

QRZDRV=/cdrom
MSGUSER=n5knx
USER=n5knx
export QRZDRV MSGUSER USER

# See if we already have a DIP process running for ptye
DODIP=1
ps ax|grep -e "-dip (" > /tmp/dip.$$
if [ -s /tmp/dip.$$ ]; then
	grep -q " pe " /tmp/dip.$$
DODIP=$?   # 0 => dip already running
fi

# Establish a slip link from Linux via pseudo-tty "e" to a Jnos task
# (use 'dip -k' to kill dip daemon)
if [ $DODIP -ne 0 ]; then
dip -t << \=EOF=
port ptype
get $local p-n5knx.aara.org
get $remote p-n5knx.ampr.org
get $netmask 255.255.255.0
get $mtu 1500
# was 236
mode slip
proxyarp
exit
=EOF=
fi

rm /tmp/dip.$$
# It seems that the proxyarp DIP cmd is failing...so we do it ourselves.
# We want the Linux ip stack to respond to ARPs for the Jnos IP addr,
# for those packets received via eth0 (ethernet).
arp -Ds p-n5knx.ampr.org eth0 pub


# Corresponding to the above slip setup, are matching Jnos attachments:
# attach asy ttype 0 slip linux 3000 1500 9600   # was 1536 236 9600
# asyconfig linux txq 15
# asyconfig linux rxq 15

# Rename any existing core file so we can use GDB later to debug it
if [ -f core ]; then
	mv core core.`date "+%y%m%d-%T"`
fi

exec ./jnos -g2 $* -d $jnosroot $autoexec

echo "error: exec of jnos returned ... error code is $?"
