#!/bin/bash

SYS_VERSION=`rpm -q --queryformat '%{VERSION}' centos-release`

>/opt/openl2tp/etc/openl2tpd.conf

IPLIST=`ip addr show | egrep -v '\blo|inet6' | egrep ':[0-9]+$' | awk '{print $2}' | sed -e 's/\/.*//g' | sort -r`
CONFIGID=0

for ip in $IPLIST; do
cat >> /opt/openl2tp/etc/openl2tpd.conf << EOF
tunnel profile create profile_name=tp_$CONFIGID our_udp_port=1701 src_ipaddr=$ip
peer profile create peer_ipaddr=$ip tunnel_profile_name=tp_$CONFIGID profile_name=pp_$CONFIGID
EOF
CONFIGID=$((CONFIGID+1))
done

if [ $SYS_VERSION -eq 6 ]; then
cat >> /opt/openl2tp/etc/openl2tpd.conf << EOF
ppp profile modify profile_name=default \
ip_pool_name=default \
use_radius=yes \
radius_hint=/etc/radiusclient/radiusclient.conf \
auth_pap=yes \
auth_chap=yes \
auth_mschapv1=yes \
auth_mschapv2=yes \
auth_eap=yes \
auth_none=no \
local_ipaddr=192.168.26.1 \
dns_ipaddr_pri=192.168.26.1 \
dns_ipaddr_sec=1.0.0.1 \
ip_pool_name=default \
proxy_arp=yes \
max_connect_time=0 \
lcp_echo_failure_count=16 \
lcp_echo_interval=10 \
default_route=no \
multilink=no
EOF
elif [ $SYS_VERSION -eq 7 ]; then
cat >> /opt/openl2tp/etc/openl2tpd.conf << EOF
ppp profile modify profile_name=default \
use_radius=yes \
radius_hint=/etc/radiusclient/radiusclient.conf \
auth_pap=yes \
auth_chap=yes \
auth_mschapv1=yes \
auth_mschapv2=yes \
auth_eap=yes \
auth_none=no \
local_ipaddr=192.168.26.1 \
dns_ipaddr_pri=192.168.26.1 \
dns_ipaddr_sec=1.0.0.1 \
proxy_arp=yes \
max_connect_time=0 \
lcp_echo_failure_count=16 \
lcp_echo_interval=10 \
default_route=no \
multilink=no
EOF
cat >> /opt/openl2tp/etc/openl2tpd.conf << EOF
system modify \
deny_remote_tunnel_creates=no \
session_establish_timeout=0 \
tunnel_persist_pend_timeout=300 \
session_persist_pend_timeout=60
EOF
fi

function read_current_server_ip_and_interface
{
        echo "SERVER_IP: $SERVER_IP"
        SERVER_IP=""
        if [ -z "$SERVER_IP" ]; then
                SYSTEM_IP=`curl ipecho.net/plain ; echo`
                if [ -z "$SYSTEM_IP" ]; then
                        echo "ipecho.net/plain attempt failed, we try as alternative ifconfig.me/ip"
                        SYSTEM_IP=`curl --connect-timeout 2 ifconfig.me/ip`
                fi
                if [[ $(echo $SYSTEM_IP | grep -cE "([0-9]{1,3}[\.]){3}[0-9]{1,3}") -eq 0 ]]; then
                        yum install bind-utils -y
                        echo "dig +short myip.opendns.com @resolver1.opendns.com"
                        SYSTEM_IP=`dig +short myip.opendns.com @resolver1.opendns.com`
                fi
                else
                        SYSTEM_IP="$SERVER_IP"
        fi

        echo "SYSTEM_IP: $SYSTEM_IP"
        SERVER_IP=$SYSTEM_IP

        if [[ $SYSTEM_IP != `hostname -i` ]]; then
                echo "hostname IP: `hostname -i` is different than detected SYSTEM_IP: $SYSTEM_IP we will prefer the $SYSTEM_IP IP!"
                #SYSTEM_IP=`hostname -i`
        fi

        echo "SYSTEM_IP: $SYSTEM_IP"
        SERVER_IP=$SYSTEM_IP

        yum install net-tools -y

        if [ -z "$INTERFACE" ]; then
                        if [ $SYS_VERSION -eq 6 ]; then
                                SYSTEM_INTERFACE=`netstat -ie | grep -B1 $SYSTEM_IP | head -n1 | awk '{print $1}'`
                                else
                                SYSTEM_INTERFACE=`netstat -ie | grep -B1 $SYSTEM_IP | head -n1 | awk -F":" '{print $1}'`
                        fi
                        SYSTEM_INTERFACE=`netstat -ie | grep -B1 $SYSTEM_IP | head -n1 | awk '{print $1}'`
                else
                        SYSTEM_INTERFACE="$INTERFACE"
        fi
        SYSTEM_INTERFACE=${SYSTEM_INTERFACE/:}
        echo "SYSTEM_INTERFACE: $SYSTEM_INTERFACE"
        interface=$SYSTEM_INTERFACE
        INTERFACE=$SYSTEM_INTERFACE
}

read_current_server_ip_and_interface
sed -i -E "s/dns_ipaddr_pri=192.168.26.1/dns_ipaddr_pri=$SERVER_IP/g" /opt/openl2tp/etc/openl2tpd.conf
