#!/bin/sh

VDE_SWITCH=/usr/bin/vde_switch
SLIRPVDE=/usr/bin/slirpvde

if [ ! -x $VDE_SWITCH ] || [ ! -x $SLIRPVDE ]; then
  exit 0
fi

case $IFACE in
    tun*|tap*)
	# Remainder of script.
    ;;
    *)
	# Not a tun/tap device.
	exit 0
    ;;
esac	

chown root:vde-net /dev/net/tun
chmod 660 /dev/net/tun

if [ -n "$IF_VDE_SWITCH" ]; then
    set $IF_VDE_SWITCH
    USER="$1"
    shift
    PIDFILE="/var/run/vde/$IFACE.pid"
    CTLFILE="/var/run/vde/$IFACE.ctl"
    start-stop-daemon --start --quiet --pidfile $PIDFILE \
	--chuid $USER \
        --exec $VDE_SWITCH -- \
	    -s $CTLFILE -p $PIDFILE -t $IFACE -d "$@"
    test -S $CTLFILE && chmod 770 $CTLFILE
fi

if [ -n "$IF_VDE_SLIRP" ]; then
    set $IF_VDE_SLIRP
    USER="$1"
    shift
    PIDFILE="/var/run/vde/$IFACE-slirp.pid"
    CTLFILE="/var/run/vde/$IFACE.ctl"
    start-stop-daemon --start --quiet --pidfile $PIDFILE \
	--make-pidfile --background --chuid $USER \
	--exec $SLIRPVDE -- -s $CTLFILE "$@"
fi
