#!/bin/bash

SYS_VERSION=`rpm -q --queryformat '%{VERSION}' centos-release`
#
# Build and install script for custom NVPN's PPP daemon
#

# installing sources to the build root
if [ $SYS_VERSION -eq 6 ]; then
    rpm -ivh ppp-2.4.5-5.el6.src.rpm
    /bin/cp ./centos6/*.patch /root/rpmbuild/SOURCES 
    /bin/cp ./centos6/ppp.spec /root/rpmbuild/SPECS
elif [ $SYS_VERSION -eq 7 ]; then
	#if [[ "$*" == update ]]; then
		# copying old rpmbuild just incase to avoid conflicts errror when installing newest version and some older wasnt fully removed beforehand
		#/bin/cp -Rp /root/rpmbuild /tmp
		rm -rf /root/rpmbuild/SPECS/ppp.spec /root/rpmbuild/SOURCES/ppp* /root/rpmbuild/RPMS/x86_64/ppp-* /root/rpmbuild/BUILD/ppp-*
		#rm -rf /root/rpmbuild
	#fi
	sed -i -e 's/ppp //g' /etc/yum.conf
    	rpm -ivh ppp-2.4.5-33.el7.src.rpm

    	# adding custom patch and spec file
    	/bin/cp *.patch /root/rpmbuild/SOURCES
    	/bin/cp ppp.spec /root/rpmbuild/SPECS
fi

# setting up build dependencies`
yum -y install libpcap libpcap-devel

# building ppp
rpmbuild -bb /root/rpmbuild/SPECS/ppp.spec || (echo "Error building ppp"; exit 1)

# removing current ppp
rpm -e ppp --nodeps
yum erase ppp -y

# instaling new ppp
rpm -ivh /root/rpmbuild/RPMS/`uname -i`/ppp-2.4.5-*.rpm

# disabling ppp update via yum
ppp_excluded=`grep -E '^exclude.*ppp' /etc/yum.conf | wc -l`
if [ $ppp_excluded -eq 0 ]; then
	sed -i /etc/yum.conf -e 's/^\(exclude=.*\)/\1 ppp/g'
fi
