#!/bin/sh
# chkconfig: 345 77 15
# description: Plesk VPN

### BEGIN INIT INFO
# Provides:          psa-vpn
# Required-Start:    $syslog $remote_fs $network
# Required-Stop:     $syslog $remote_fs $network
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Plesk VPN module
# Description:       Plesk VPN module management
### END INIT INFO

SERVICE_NAME="psa-vpn"
MODULE="vpn"
PRODUCT_ROOT_D="/usr/local/psa"

action="$1"
conf="$PRODUCT_ROOT_D/var/modules/$MODULE/openvpn.conf"
pid="$PRODUCT_ROOT_D/var/modules/$MODULE/openvpn.pid"
name="$PRODUCT_ROOT_D/admin/sbin/modules/$MODULE/openvpn"

# function is_running tries to find running process with name $1 and pid $2

is_running()
{
	if [ -f "$2" ]; then
		local exec_name="$1"
		local pid_value=`cat "$2"`
		which pidoff >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			local pid_value_pidof=`pidof $exec_name`
			if [ "$pid_value" = "$pid_value_pidof" ]; then
				return 0
			else
				return 1
			fi
		fi
		# there is no pidof in system
		# get name of exec_file from /proc 
		which readlink >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			if  [ "$exec_name" = "`readlink -e /proc/${pid_value}/exe`" ]; then
				return 0
			else
				return 1
			fi
		fi
	fi
	return 1
}

case "$action" in

	start)
		if [ -f "$conf" ]; then
			if is_running "$name" "$pid"; then
				echo "$SERVICE_NAME: OpenVPN is already running with pid $(cat $pid)"
				exit 0
			fi
			rm -f "$pid" >/dev/null 2>&1
			if $name --config "$conf"; then
				echo "$SERVICE_NAME: OpenVPN has been started"
				exit 0
			else
				echo "$SERVICE_NAME: OpenVPN failed to start"
				exit 1
			fi
		else
			echo "$SERVICE_NAME: OpenVPN is disabled"
			exit 0
		fi
		;;

	stop)
		if is_running "$name" "$pid"; then		
			kill `cat "$pid"` 
			rm -f "$pid" >/dev/null 2>&1
			echo "$SERVICE_NAME: OpenVPN has been stopped"
			exit 0
		else
			echo "$SERVICE_NAME: No OpenVPN found running"
			exit 0
		fi
		;;

	restart)
		"$0" stop
		"$0" start
		exit
		;;

	reconfigure|reload|force-reload)
		if [ ! -f "$conf" ]; then
			echo "$SERVICE_NAME: OpenVPN is disabled, stopping"
			"$0" stop
			exit
		elif is_running "$name" "$pid"; then
			kill -HUP `cat "$pid"`
			echo "$SERVICE_NAME: Sent a signal to OpenVPN to reread the configuration file"
			exit
		else
			"$0" start
			exit
		fi
		;;

	rekey)
		if [ ! -f "$conf" ]; then
			echo "$SERVICE_NAME: OpenVPN is disabled, stopping"
			"$0" stop
			exit
		elif is_running "$name" "$pid"; then
			kill -USR1 `cat "$pid"`
			echo "$SERVICE_NAME: Sent a signal to OpenVPN to reread the key file"
			exit
		else
			"$0" start
			exit
		fi
		;;

	status)
		if is_running "$name" "$pid"; then
			pid_value=`cat "$pid"`
			echo "OpenVPN is running with pid $pid_value"
			exit 0
		else
			echo "OpenVPN is stopped"
			exit 1
		fi
		;;

	help)
		echo "usage: $0 <arg>"
		echo "where argument is one from the list:"
		cat << EOF
	start       -- start ${SERVICE_NAME}
	stop        -- stop ${SERVICE_NAME}
	restart     -- restart ${SERVICE_NAME} if running or start if not running
	reconfigure -- make ${SERVICE_NAME} re-read the configuration file if running or start if not running
	rekey       -- make ${SERVICE_NAME} re-read the authentication key if running or start if not running
	status      -- show ${SERVICE_NAME} status
	help        -- this screen

EOF
		exit 1
		;;

	*)
		echo "usage: $0 (start|stop|restart|reconfigure|rekey|status|help)"
		exit 1
		;;
esac
