#!/bin/bash

#
# Build and install script for custom NVPN's xl2tp daemon
# xl2tp installed to /opt/xl2tp, however config files should still go to /etc/xl2tpd
#

BUILDROOT=/tmp
BUILDDIR=$BUILDROOT/xl2tpd-src
INSTALLDIR=/opt/xl2tpd
PATCH=xl2tpd-myip.patch

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

# installing prerequisites
yum -y install libpcap libpcap-devel

# copying fresh sources and patching them
/bin/cp -Rp ./xl2tpd-src $BUILDROOT
/bin/cp $PATCH $BUILDDIR
(cd $BUILDDIR ; patch -p2 < $PATCH)

# building and installing xl2tp
(cd $BUILDDIR ; make && make install)

# copying configuration files
/bin/cp ./xl2tpd-conf/{xl2tpd.conf,l2tp-secrets} /opt/xl2tpd/etc
/bin/cp ./xl2tpd-conf/options.xl2tpd /etc/ppp
/bin/cp ./xl2tpd /etc/init.d/

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

test -d /etc/monit.d && /bin/cp ./monit-xl2tp /etc/monit.d

service xl2tpd restart
chkconfig xl2tpd on
