#!/bin/bash

#
# Build and install script for custom NVPN's openl2tp daemon
#
SYS_VERSION=`rpm -q --queryformat '%{VERSION}' centos-release`
L2TP_VIRTUAL_IP=192.168.26.1
BUILDROOT=/tmp
TARBALL=openl2tp-1.8.tar.gz
BUILDDIR=$BUILDROOT/`basename $TARBALL .tar.gz`
INSTALLDIR=/opt/openl2tp
PATCH=openl2tp.patch
PATCH2=openl2tp-1.8-no-Werror.patch
PATCH3=openl2tp_Dis_RPC.patch

# removing previous builddir
rm -Rf $BUILDDIR
rm -Rf $INSTALLDIR

# installing prerequisites
rpm -ivh http://repo.nikoforge.org/redhat/el6/nikoforge-release-latest
yum -y install ipsec-tools portmap nfs* readline readline-devel flex-devel --skip-broken

# copying fresh sources and patching them
/bin/cp $TARBALL $BUILDROOT
(cd $BUILDROOT ; tar -zxvf ./$TARBALL)
/bin/cp $PATCH $BUILDDIR
(cd $BUILDDIR ; patch -p2 < $PATCH)

if [ $SYS_VERSION -eq 7 ]; then
  /bin/cp $PATCH2 $BUILDDIR
  /bin/cp $PATCH3 $BUILDDIR
  (cd $BUILDDIR ; patch -p1 < $PATCH2 ; patch -p1 < $PATCH3)
fi

# building and installing openl2tp
(cd $BUILDDIR ; make && make install)
mkdir -p $INSTALLDIR/{etc,var/run}

rm -Rf /usr/lib64/openl2tp /usr/lib/openl2tp
ln -s /opt/openl2tp/openl2tp/ /usr/lib64/openl2tp
ln -s /opt/openl2tp/openl2tp/ /usr/lib/openl2tp

# generating config based on IP addresses available on the system
./confgen.sh

# copying configuration files
/bin/cp ./openl2tpd /etc/init.d/
/bin/cp ./rpcbind /etc/init.d/

test -d /etc/monit.d && /bin/cp ./monit-openl2tp /etc/monit.d
test -d /etc/monit.d && /bin/cp ./monit-rpcbind /etc/monit.d
/bin/cp ./moose_rpc_monit_script.sh /usr/local/bin/

l2tpko_loaded=`cat /etc/rc.d/rc.local | grep l2tp_ | wc -l`
if [ $l2tpko_loaded -eq 0 ]; then
       modprobe l2tp_core && (echo "modprobe l2tp_core" >> /etc/rc.d/rc.local)
       modprobe l2tp_ppp && (echo "modprobe l2tp_ppp" >> /etc/rc.d/rc.local)
fi

# adding virtual ip for L2TP
/sbin/ip addr add $L2TP_VIRTUAL_IP dev eth0
virtip_added=`cat /etc/rc.d/rc.local | grep $L2TP_VIRTUAL_IP | wc -l`
if [ $virtip_added -eq 0 ]; then
	echo "/sbin/ip addr add $L2TP_VIRTUAL_IP dev eth0" >> /etc/rc.d/rc.local
fi

if [ $SYS_VERSION -eq 6 ]; then
  ln -s /usr/lib/pppd/2.4.5/pppol2tp.so /usr/lib64/pppd/2.4.5/pppol2tp.so
  ln -s /usr/lib/pppd/2.4.5/openl2tp.so /usr/lib64/pppd/2.4.5/openl2tp.so
  chkconfig rpcbind on
  chkconfig nfs on
  service rpcbind restart
  service nfs restart
  chkconfig openl2tpd on
  service openl2tpd restart
  service ippoold restart
elif [ $SYS_VERSION -eq 7 ]; then
  mkdir -p /usr/lib/pppd/2.4.5
  ln -s /usr/lib64/pppd/2.4.5/pppol2tp.so /usr/lib/pppd/2.4.5/pppol2tp.so
  ln -s /usr/lib64/pppd/2.4.5/openl2tp.so /usr/lib/pppd/2.4.5/openl2tp.so
  /bin/cp ./openl2tpd.service /lib/systemd/system/
  /bin/cp -f ./monit-openl2tp-c7 /etc/monit.d/monit-openl2tp 
  systemctl daemon-reload
  /bin/rm -f /etc/monit.d/monit-rpcbind
  /bin/rm -f /etc/init.d/openl2tpd
  /bin/rm -f /etc/init.d/rpcbind
#  systemctl restart rpcbind
  systemctl enable nfs
  systemctl restart nfs
  systemctl enable openl2tpd
  systemctl restart openl2tpd
#  systemctl restart ippoold
  sleep 3
#  systemctl restart ippoolc
  systemctl status nfs openl2tpd
fi
