#!/bin/bash

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

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

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

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

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

/bin/cp ./ippool.conf $INSTALLDIR/etc/

# copying configuration files

#if [ $SYS_VERSION -eq 6 ]; then
/bin/cp ./ippoold /etc/init.d/
test -d /etc/monit.d && /bin/cp ./monit-ippool /etc/monit.d
chkconfig ippoold on
#elif [ $SYS_VERSION -eq 7 ]; then
#  /bin/cp ./ippool*.service /lib/systemd/system/
#  systemctl daemon-reload
#  systemctl enable ippoold
#  test -d /etc/monit.d && /bin/cp ./monit-ippool-systemd /etc/monit.d/monit-ippool
#fi
  