#!/bin/bash 
#
#
# fixing script up to be called as an option from all the build-ovs scripts
# script to add static ip for client and setup shorewall params


#setting the COMMONNAME to $1
COMMONNAME=$1

. /usr/share/openvpn-server/functions.sh
. /etc/openvpn-server/config.sh

IPPREFIX=`cat /etc/openvpn/$OVPN_ORGNICK-server.conf |grep 'server '|cut -f2 -d' '|cut -f1,2,3 -d.`.

IP=`cat /etc/openvpn/$OVPN_ORGNICK-server.conf | grep '^server '| cut -f2 -d' '`
SUBNET=`cat /etc/openvpn/$OVPN_ORGNICK-server.conf | grep '^server '| cut -f3 -d' '`

#initialise in case this is the first time

case $FIREWALLED in
        yes)
        ;;
        *)
        mkdir -p $CCDDIR > /dev/null
mkdir -p /etc/shorewall/
sed -ibackup -e "/openvpn\.vars/d" /etc/shorewall/params
sed -ibackup -e "/#LAST/i\
\.\ $SHOREWALLVARS" /etc/shorewall/params
sed -ibackup -e "s/FIREWALLED=no/FIREWALLED=yes/" /etc/openvpn-server/config.sh
        ;;
esac



function get_new_ip() {
    local newip=""
    local s_ip=$1
    local s_subnet=$2
    local skip_first_zero=1
    local skip_first_one=1

    for ip in `/usr/share/openvpn-server/calc-ip-range.sh -i $s_ip -s $s_subnet`; do
        if ! grep -q $ip $CCDDIR/*; then
            IFS=. read -r a b c d <<< "$ip"
            if [ $d -eq 0 ] && [ $skip_first_zero -eq 1 ]; then
                skip_first_zero=0
            elif [ $d -eq 1 ] && [ $skip_first_one -eq 1 ]; then
                skip_first_one=0
            else
                newip="$ip"
                break
            fi
        fi
    done

    if [ -z "$newip" ]; then
        echo "No available IP addresses found in the range."
        return 1
    else
        echo "$newip"
    fi
}



# Return the openvpn server endpoint for an IP
function get_server_endpoint_ip {

    echo "$1" | awk -F"." '{$4++;print $1"."$2"."$3"."$4}'

}

# with topology subnet, we don't need this stuff
#Each pair of ifconfig-push addresses represent the virtual client and server IP endpoints.
#They must be taken from successive /30 subnets in order to be compatible with Windows clients
#and the TAP-Win32 driver.
#Specifically, the last octet in the IP address of each endpoint pair must be taken from this set:
#
#    [  1,  2] [  5,  6] [  9, 10] [ 13, 14] [ 17, 18]
#    [ 21, 22] [ 25, 26] [ 29, 30] [ 33, 34] [ 37, 38]
#    [ 41, 42] [ 45, 46] [ 49, 50] [ 53, 54] [ 57, 58]
#    [ 61, 62] [ 65, 66] [ 69, 70] [ 73, 74] [ 77, 78]
#    [ 81, 82] [ 85, 86] [ 89, 90] [ 93, 94] [ 97, 98]
#    [101,102] [105,106] [109,110] [113,114] [117,118]
#    [121,122] [125,126] [129,130] [133,134] [137,138]
#    [141,142] [145,146] [149,150] [153,154] [157,158]
#    [161,162] [165,166] [169,170] [173,174] [177,178]
#    [181,182] [185,186] [189,190] [193,194] [197,198]
#    [201,202] [205,206] [209,210] [213,214] [217,218]
#    [221,222] [225,226] [229,230] [233,234] [237,238]
#    [241,242] [245,246] [249,250] [253,254]
#

ip=$(get_new_ip $IP $SUBNET);

if [ -x $ip ] ; then
	echo "we appear to have run out of ip's, fix it"
	echo "then run \$ $0 $COMMONNAME"
else
	endpointip=$(get_server_endpoint_ip $ip)
        echo "your new ip will be $ip"


        if [ $COMMONNAME != "" ] ; then
                echo "writing new ccd $COMMONNAME"
                echo ifconfig-push $ip $SUBNET > $CCDDIR$COMMONNAME #topology subnet now!

                echo "writing new shorewall vars"
                create_shorewall_vars
                
        else
                echo "no command name supplied so nothing will be written out"
                echo "to write the results run \$ $0 <COMMONNAME>"
        fi

fi

#now sync the ccds to the servers if we have them
if [ "$CAONLY" == "yes" ]
        then for server in `cat /etc/openvpn-server/servers.conf`
                do echo "syncing CCDs to the $server"
			rsync -a --delete -e 'ssh -i /var/lib/openvpn-server/openssl/ssh-keys/id_rsa' /etc/openvpn/ccd/ $server:/etc/openvpn/ccd/
			rsync -a --delete -e 'ssh -i /var/lib/openvpn-server/openssl/ssh-keys/id_rsa' $SHOREWALLVARS $server:$SHOREWALLVARS
                done

fi

exit 0
