#!/bin/sh

# A sample OpenVPN startup script
# for Linux.

# openvpn config file directory
dir=/etc/openvpn

# openvpn work directory
work=/etc/openvpn

# load the firewall
if [ -f "$dir/firewall.sh" ]; then
  $dir/firewall.sh
fi

# load TUN/TAP kernel module
/sbin/modprobe tun

case "$1" in
    start)
	echo -n $"Starting openvpn: "

	# enable IP forwarding
	echo 1 > /proc/sys/net/ipv4/ip_forward
	
	# Invoke openvpn for each VPN tunnel
	# in daemon mode.  Alternatively,
	# you could remove "--daemon" from
	# the command line and add "daemon"
	# to the config file.
	#
	# Each tunnel should run on a separate
	# UDP port.  Use the "port" option
	# to control this.  Like all of
	# OpenVPN's options, you can
	# specify "--port 8000" on the command
	# line or "port 8000" in the config
	# file.

	ERROR=0
	cd $dir
	for c in `/bin/ls *.conf 2>/dev/null`; do
	  echo "start OpenVPN server($c)"
	  /usr/sbin/openvpn --daemon --config $c --cd $work
	    if [ $? != 0 ]; then
		ERROR=$ERROR$0
	    fi

	done
	;;
    stop)
	echo "Shutting down openvpn"
	/usr/bin/killall -TERM openvpn
	ERROR=$?
	;;
  restart)
	$0 stop
	sleep 2
	$0 start
	;;
    *)
	echo "OpenVPN status"
	ps aux | grep openvpn | grep -v $0 | grep -v grep
	ERROR=$?
esac

exit $ERROR
