#!/bin/sh

DEV="$1"
DEVQE="$2"

TMPFILE="`mktemp /tmp/$0.XXXXXX`"
export TMPFILE

SIMH="./vax"

HOSTIP=`/sbin/ifconfig eth0 | grep inet | gawk -- '{ print $2 }' | gawk -F : -- '{ print $2 }'`
HOSTNETMASK=`/sbin/ifconfig eth0 | grep inet | gawk -- '{ print $4 }' | gawk -F : -- '{ print $2 }'`
HOSTDEFAULTGATEWAY=`/sbin/route | grep default | gawk -- '{ print $2 }'`

sighandler() {
	rm $TMPFILE
	exit
}

trap sighandler 1 3 9  15

cat <<_EOF > $TMPFILE
load -r ka655.bin
set cpu 64m
set cpu conhalt
set rl disab
set dz disab
set ts disab
set tq disab
set telnet 193
;
set rq0 ra82
at rq0 VMS055.DSK
;
set xq mac=08-00-2b-11-22-44
at xq0 $DEVQE
;
_EOF

/sbin/ifconfig $DEVQE up

# Now convert eth0 to a bridge and bridge it with the TAP interface
/usr/sbin/brctl addbr br0
/usr/sbin/brctl addif br0 eth0
/sbin/ifconfig eth0 0.0.0.0
/sbin/ifconfig br0 $HOSTIP netmask $HOSTNETMASK up
# restore the default route to the br0 interface
/sbin/route add -net 0.0.0.0/0 gw $HOSTDEFAULTGATEWAY
# bridge in the tap device
/usr/sbin/brctl addif br0 $DEV
/sbin/ifconfig $DEV 0.0.0.0

nice $SIMH -v $TMPFILE

rm $TMPFILE

# Revert to the original eth0 network configuration
/sbin/ifconfig br0 down
/usr/sbin/brctl delbr br0
/sbin/ifconfig eth0 $HOSTIP netmask $HOSTNETMASK up

# restore the default route to the eth0 interface
/sbin/route add -net 0.0.0.0/0 gw $HOSTDEFAULTGATEWAY

