#!/bin/sh
#
# Copyright (c) 2010 Werner Jaeger <werner.jaeger@t-systems.com>
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL;  if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
### BEGIN INIT INFO
# Provides:          l2tp-ipsec-vpn-daemon
# Required-Start:    $remote_fs $network $syslog
# Required-Stop:     $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Daemon used to start/stop L2TP over IPsec VPN connections by non privileged users
# Description:       Daemon used to start/stop L2TP over IPsec VPN connections by non privileged users
### END INIT INFO

NAME=L2tpIPsecVpnControlDaemon
DESC=L2tpIPsecVpnControlDaemon
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/lib/l2tp-ipsec-vpn-daemon/$NAME

test -x $DAEMON || exit 0

# Include L2tpIPsecVpnControlDaemon defaults if available
if [ -f /etc/default/l2tp-ipsec-vpn-daemon ] ; then
	. /etc/default/l2tp-ipsec-vpn-daemon
fi

PIDFILE=/var/run/$NAME.pid

# set the environment vaiables needed by the daemon
export HOME=/root

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
	echo -n "Starting $DESC: "
	test -d ${L2TP_IPSEC_VPN_CONTROLD_RUN_DIR:-/var/run/$NAME} || mkdir -p ${L2TP_IPSEC_VPN_CONTROLD_RUN_DIR:-/var/run/$NAME}
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE --exec $DAEMON
	echo "$NAME."
	;;
  force-reload)
	test -d ${L2TP_IPSEC_VPN_CONTROLD_RUN_DIR:-/var/run/$NAME} || mkdir -p ${XL2TP_IPSEC_VPN_CONTROLD_RUN_DIR:-/var/run/$NAME}
	# check whether $DAEMON is running. If so, restart
	start-stop-daemon --stop --test --quiet --pidfile $PIDFILE --exec $DAEMON && $0 restart || exit 0
	;;
  restart)
	test -d ${L2TP_IPSEC_VPN_CONTROLD_RUN_DIR:-/var/run/$NAME} || mkdir -p ${L2TP_IPSEC_VPN_CONTROLD_RUN_DIR:-/var/run/$NAME}
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  status)
   status_of_proc -p $PIDFILE $DAEMON $NAME || true
   ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|status|force-reload}" >&2
	exit 1
	;;
esac

exit 0
