#!/bin/sh
# $Id$
#
# Join a network, see kipd-control from more comments.
#

PATH=/usr/sbin:/sbin:/usr/bin:/bin:%bindir%

endpointhost=130.237.43.201
thispointhost=130.237.43.17
fakepoint=10.0.0.1
dev=tun0

case $# in
    0)
        modprobe tun
	def=$(route -n | awk '$1 ~ /0.0.0.0/ && $3 ~ /0.0.0.0/ { print $2 }')

	if test "X$def" = "X" ; then
		echo "missing default route"
		exit 1
	fi

	exec kip -c $0 -a $def $endpointhost
	;;
    *)
	state=$1
	dev=$2
	host=$3
	arg=$4
	case $state in
	up)
		ifconfig $dev $thispointhost pointopoint $fakepoint
		route delete default

		route add -host $endpointhost gw $arg
		route add default gw $fakepoint
		;;
	down)

		echo $dev $arg > /tmp/kip-down

		ifconfig $dev down

		route delete default
		route delete $endpointhost
		route add default gw $arg
		;;
	*)
		exit 17
	;;
	esac
esac
