#!/bin/bash
# Orig by Ceasar Sun. Modified by Steven Shiau.

WHOAMI="$SUDO_USER"
VBOX_GROUP="vboxusers"
TAP_NETWORK="192.168.125"
TAP_DEV_NUM=0
DESC="TAP config"

do_start() {
  if [ ! -x /usr/sbin/tunctl ]; then
    echo "/usr/sbin/tunctl was NOT found!"
    exit 1
  fi
  tunctl -t tap$TAP_DEV_NUM -u $WHOAMI
  chown :$VBOX_GROUP /dev/net/tun
  ifconfig tap$TAP_DEV_NUM &>/dev/null
  ifconfig tap$TAP_DEV_NUM ${TAP_NETWORK}.254  netmask 255.255.255.0  broadcast ${TAP_NETWORK}.255 promisc
  ifconfig tap$TAP_DEV_NUM
}

do_stop() {
  ifconfig tap$TAP_DEV_NUM down 
}
do_restart() {
  do_stop
  do_start
}
check_status() {
  ifconfig tap$TAP_DEV_NUM 
}

case $1 in 
  start)    do_start;;
  stop)     do_stop;;
  restart)  do_restart;;
  status)
            echo "Status of $DESC: "
            check_status
            exit "$?"
            ;;
  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1 
esac
