#!/bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin

cd /etc/sysconfig/network-scripts
. network-functions

IP=/sbin/ip

CONFIG=$1
[ -f "$CONFIG" ] || CONFIG="ifcfg-$1"
source_config

if [ -z "$TUNLOCAL" ]; then
  echo "missing TUNLOCAL"; 
	exit 1
fi

if [ -z "$TUNREMOTE" ]; then
  echo "missing TUNREMOTE"; 
	exit 1
fi

if [ -z "$PHYSLOCAL" ]; then
  echo "missing PHYSLOCAL"; 
	exit 1
fi

if [ -z "$PHYSREMOTE" ]; then
  echo "missing PHYSREMOTE"; 
	exit 1
fi

if [ -z "$TTL" ]; then
	TTL="inherit"
fi

if [ -z "$TYPE" ]; then
	TYPE="ipip"
fi

if [ -z "$REMOTENETMASKLEN" ]; then
	REMOTENETMASKLEN=32
fi

if [ "$2" = "boot" -a "$ONBOOT" = "no" ]; then
  exit
fi

[ -x "$IP" ] || {
  echo "$IP does not exist or is not executable"
  echo "ifup-tun for $DEVICE exiting"
  logger -p daemon.info -t ifup-iptun \
    "$IP does not exist or is not executable for $DEVICE"
  exit 1
}
case "$TYPE" in
	ipip)
		MODULE=ipip
		;;
	gre)
		MODULE=ip_gre
		;;
	*)
		echo "tunnel TYPE for $DEVICE must be either 'ipip' or 'gre'"
		exit 1
esac
modprobe $MODULE

$IP tunnel add $DEVICE mode $TYPE local $PHYSLOCAL remote $PHYSREMOTE ttl $TTL $OPTIONS
$IP address add $TUNLOCAL peer $TUNREMOTE/$REMOTENETMASKLEN dev $DEVICE
$IP link set dev $DEVICE up
exec /etc/sysconfig/network-scripts/ifup-post "ifcfg-$DEVICE" "$2"

