#!/bin/bash2
# PhionVersionString 3.2.0.1

if [ -f /etc/phion/phion.sh ]; then
        . /etc/phion/phion.sh
fi

if [ -f /etc/phion/functions ]; then
        . /etc/phion/functions
fi

function ipAddRange {
	python2 - $1 $2 <<EOP
import sys

ipstart = sys.argv[1].split('.')
if len(ipstart) != 4: sys.exit(1)

ipint = 0
for net in ipstart:
	ipint = ipint * 256 + int(net)

ipint += int(sys.argv[2])
ipend = []

while ipint > 0:
	ipend.insert(0, str(ipint % 256))
	ipint /= 256

if len(ipend) != 4: sys.exit(1)
print('.'.join(ipend))
EOP
}

function readPHIONCONF {
        if [ ! -f "$1" ]; then
                echo "No pppvpn.conf file found!!! Mail to support@phion.com"
                $logerror "Fatal"  "No pppvpn.conf file found!!! This log message is harmless if it appears only at the introducing of a new service!"
                return 1
        fi

        if [ ! -x "$PHION_ROLLOUT" ]; then
                echo NO rollout script "$PHION_ROLLOUT" found!
                return 1
        fi

	conf=/etc/phion/dynconf/pppvpn.conf.rollout.verify.$$

        "$PHION_ROLLOUT" "$1" ${conf} 1>/dev/null

        [ -f ${conf} ] && {
                . ${conf}
                rm -f ${conf}
               }

        return 0

        }


readPHIONCONF $1 || {
	echo cannot read configuration $1.
	echo "STATUS 1"
	exit 1
}

rc=0

if [ no == "${box_STATICIP}" ]; then

	if [ _yes == _${box_STARTL2TPD} ]; then
		if [ -z "${box_STARTIP}" -o -z "${box_RANGESIZE}" ]; then
				echo "L2TP Pool IP-Begin/Pool Size is empty."
				rc=1
		else
			start=${box_STARTIP##*.}
			if (( box_RANGESIZE <= 0 )); then
				echo "L2TP IP-pool size is zero."
				rc=1
			fi
			if ! ipAddRange ${box_STARTIP} $(( box_RANGESIZE - 1 )); then
				echo "L2TP IP-pool size is invalid."
				rc=1
			fi
		fi
	fi

	if [ _yes == _${box_STARTPPTP} ]; then
		if [ -z "${box_STARTIP2}" -o -z "${box_RANGESIZE2}" ]; then
				echo "PPTP Pool IP-Begin/Pool Size is empty."
				rc=1
		else
			start=${box_STARTIP2##*.}
			end=${box_RANGESIZE2}
			if (( end <= 0 )); then
				echo "PPTP IP-pool size is zero."
				rc=1
			fi
			if (( end >= 100 )); then
				echo "PPTP IP-pool size exceeds 100."
				rc=1
			fi
			if (( end + start > 256 )); then
				echo "PPTP IP-pool exceeds class C subnet."
				rc=1
			fi
		fi
	fi

else

	if [ _yes == _${box_STARTL2TPD} ]; then
		if [ -n "${box_STARTIP}" -o -n "${box_RANGESIZE}" ]; then
				echo "L2TP Pool IP-Begin/Pool Size is set but is not applicable with static IP assignment."
				rc=1
		fi
	fi

	if [ _yes == _${box_STARTPPTP} ]; then
		if [ -n "${box_STARTIP2}" -o -n "${box_RANGESIZE2}" ]; then
				echo "PPTP Pool IP-Begin/Pool Size is set but is not applicable with static IP assignment."
				rc=1
		fi
	fi

fi

iMAX=${#box_NAM[@]}
for ((i=0; iMAX-i; i++)); do
	if [ yes == "${box_STATICIP}" ]; then
		if [ -z "${box_USERIP[$i]}" ]; then
			echo "Static IP assignment is enabled but user '${box_NAM[$i]}' does not have a static IP address"
			rc=1
		fi
	else
		if [ -n "${box_USERIP[$i]}" ]; then
			echo "Static IP assignment is disabled but user '${box_NAM[$i]}' does have a static IP address"
			rc=1
		fi
	fi
done

echo "STATUS ${rc}"

exit ${rc}
