#!/bin/bash

CURRENT_FOLDER_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
SYS_VERSION=`rpm -q --queryformat '%{VERSION}' centos-release`

STUNNEL_VERSION="5.55"
#STUNNEL_URL="https://www.stunnel.org/downloads/stunnel"
STUNNEL_URL="http://www.usenix.org.uk/mirrors/stunnel/archive/5.x/stunnel"
yum -y remove stunnel

if [ ! -d "/root/rpmbuild/SOURCES" ]; then
	mkdir -p /root/rpmbuild/{SPECS,SOURCES,SPECS,BUILD}
fi

sed -i -e 's/exclude/#&/' /etc/yum.conf

if [ $SYS_VERSION -eq 6 ]; then
  yum -y install redhat-lsb-core-4.0-7.el6.centos.x86_64 tcp_wrappers-devel
elif [ $SYS_VERSION -eq 7 ]; then
  yum -y install redhat-lsb-core-4.1-27.el7.centos.1.x86_64 tcp_wrappers-devel
fi

sed -i -e 's/#exclude/exclude/' /etc/yum.conf

if [[ `grep -c '^stunnel:' /etc/passwd` == 1 ]]; then
	
	echo "user 'stunnel' already exists, lets delete him"
	userdel stunnel
	
	if [[ `grep -cE "^stunnel:" /etc/group` == 0 ]]; then
		echo "group 'stunnel' doesnt exist, lets add it"
		groupadd -g 91 stunnel
		else
			echo "group 'stunnel' already exists, lets delete then re-add it"
			groupdel stunnel
			groupadd -g 91 stunnel
	fi
	
	useradd -c "stunnel Daemon" -d /home/stunnel -g stunnel -s /bin/false -u 91 stunnel
	echo "stunnel added to group now with: "
	cat /etc/group | grep stunnel
	
	else
		if [[ `grep -cE "^stunnel:" /etc/group` == 0 ]]; then
                	echo "group 'stunnel' doesnt exist, lets add it"
	                groupadd -g 91 stunnel
		fi
		useradd -c "stunnel Daemon" -d /home/stunnel -g stunnel -s /bin/false -u 91 stunnel
fi

if [ ! -d "/etc/stunnel" ]; then
	mkdir /etc/stunnel
fi

cd /root/rpmbuild/SOURCES

wget $STUNNEL_URL-$STUNNEL_VERSION.tar.gz
tar -zxvf stunnel-$STUNNEL_VERSION.tar.gz
cd stunnel-$STUNNEL_VERSION

if [ $SYS_VERSION -eq 6 ]; then
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-systemd && make
elif [ $SYS_VERSION -eq 7 ]; then
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var && make
fi

#./configure --prefix=/usr/sbin --sysconfdir=/etc --localstatedir=/var --disable-systemd && make
make docdir=/usr/share/doc/stunnel-$STUNNEL_VERSION install
#install -v -m750 -o stunnel -g stunnel -d /var/lib/stunnel/run && chown stunnel:stunnel /var/lib/stunnel
install -v -m750 -o stunnel -g stunnel -d /var/run/stunnel && chown stunnel:stunnel /var/run/stunnel


cd $CURRENT_FOLDER_PATH
tar -xf blfs-bootscripts-20170225.tar.xz
if [ $SYS_VERSION -eq 6 ]; then
  cd blfs-bootscripts-20170225
  make install-stunnel
  cd $CURRENT_FOLDER_PATH
  /bin/cp -Rp stunnel /etc/init.d/
fi
./confgen.sh

echo "ENABLED=1" > /etc/default/stunnel

if [ $SYS_VERSION -eq 6 ]; then
 chkconfig --add stunnel
 chkconfig stunnel on
 /etc/init.d/stunnel start

 if [[ "$*" == update ]]; then
  service stunnel restart
  netstat -tulpn | grep stunnel
  echo "Stunnel successfully updated to version $STUNNEL_VERSION"
 fi
elif [ $SYS_VERSION -eq 7 ]; then
 /bin/cp -f ./stunnel.service /lib/systemd/system/stunnel@.service
 /bin/rm -f /etc/init.d/stunnel
 /bin/rm -f /etc/stunnel/stunnel.conf-sample
 systemctl daemon-reload
if [ ! -d "/var/run/stunnel" ]; then
	mkdir /var/run/stunnel
fi
TUNNELS=`ls /etc/stunnel | grep conf | awk -F. '{print $1}' | awk -F- '{print $2}'`
for item in $TUNNELS
do
systemctl enable stunnel@$item
systemctl start stunnel@$item
systemctl status stunnel@$item
 if [[ "$*" == update ]]; then
  systemctl restart stunnel@$item
  netstat -tulpn | grep stunnel
  echo "Stunnel successfully updated to version $STUNNEL_VERSION"
 fi
done
fi
