#!/bin/bash
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
#if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
#    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
#fi
### BEGIN INIT INFO
# Provides:          qibovpn
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: <Enter a short description of the software>
# Description:       <Enter a long description of the software>
#                    <...>
#                    <...>
### END INIT INFO

# Author: root <root@unknown>

DESC="qibovpn"
#DAEMON=/usr/bin/qibovpn

qbvpn=""
qbvpn_locations="/usr/sbin/qbvpn /usr/local/qibo/sbin/qbvpn"
for location in $qbvpn_locations
do
  if [ -f "$location" ]
  then
    qbvpn=$location
  fi
done

# Lockfile
lock="/var/lock/qbvpn"

# PID directory
piddir="/var/run/qbvpn"

# Our working directory
work=/etc/qibovpn

# Source function library.
#. /etc/rc.d/init.d/functions

# Source networking configuration.
#. /etc/sysconfig/network

# Check that networking is up.
if [ "${NETWORKING}" = "no" ]
then
  echo "Networking is down"
  exit 0
fi

# Check that binary exists
if ! [ -f  $qbvpn ] 
then
  echo "qibovpn binary not found"
  exit 0
fi

# See how we were called.
case "$1" in
  start)
	echo -n $"Starting qibovpn: "

	/sbin/modprobe tun >/dev/null 2>&1

	# From a security perspective, I think it makes
	# sense to remove this, and have users who need
	# it explictly enable in their --up scripts or
	# firewall setups.

	#echo 1 > /proc/sys/net/ipv4/ip_forward

	# Run startup script, if defined
	if [ -f $work/qbvpn-startup ]; then
	    $work/qbvpn-startup
	fi

	if [ ! -d  $piddir ]; then
	    mkdir $piddir
	fi

	if [ -f $lock ]; then
	    # we were not shut down correctly
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
	      if [ -s $pidf ]; then
		kill `cat $pidf` >/dev/null 2>&1
	      fi
	      rm -f $pidf
	    done
	    rm -f $lock
	    sleep 2
	fi

	rm -f $piddir/*.pid
	cd $work

	# Start every .conf in $work and run .sh if exists
	errors=0
	successes=0
	for c in `/bin/ls *.conf 2>/dev/null`; do
	    bn=${c%%.conf}
	    if [ -f "$bn.sh" ]; then
		. ./$bn.sh
	    fi
	    rm -f $piddir/$bn.pid
	    $qbvpn --daemon --writepid $piddir/$bn.pid --config $c --cd $work
	    if [ $? = 0 ]; then
		successes=1
	    else
		errors=1
	    fi
	done

	if [ $errors = 1 ]; then
	    echo failure;
	else
	    echo success;
	fi

	if [ $successes = 1 ]; then
	    touch $lock
	fi
	;;
  stop)
	echo -n $"Shutting down qibovpn: "
	for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
	  if [ -s $pidf ]; then
	    kill `cat $pidf` >/dev/null 2>&1
	  fi
	  rm -f $pidf
	done

	# Run shutdown script, if defined
	if [ -f $work/qbvpn-shutdown ]; then
	    $work/qbvpn-shutdown
	fi

	echo success; 
	rm -f $lock
	;;
  restart)
	$0 stop
	sleep 2
	$0 start
	;;
  reload)
	if [ -f $lock ]; then
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
		if [ -s $pidf ]; then
		    kill -HUP `cat $pidf` >/dev/null 2>&1
		fi
	    done
	else
	    echo "qibovpn: service not started"
	    exit 1
	fi
	;;
  reopen)
	if [ -f $lock ]; then
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
		if [ -s $pidf ]; then
		    kill -USR1 `cat $pidf` >/dev/null 2>&1
		fi
	    done
	else
	    echo "qibovpn: service not started"
	    exit 1
	fi
	;;
  condrestart)
	if [ -f $lock ]; then
	    $0 stop
	    # avoid race
	    sleep 2
	    $0 start
	fi
	;;
  status)
	if [ -f $lock ]; then
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
		if [ -s $pidf ]; then
		    kill -USR2 `cat $pidf` >/dev/null 2>&1
		fi
	    done
	    echo "Status written to /var/log/messages"
	else
	    echo "qibovpn: service not started"
	    exit 1
	fi
        ;;
  *)
	echo "Usage: qibovpn {start|stop|restart|condrestart|reload|reopen|status}"
	exit 1
	;;
esac
exit 0
