#!/usr/bin/env bash

# refactor nginx config files in order to support new features
# 21/08/16 Andrew Checkov <andrew@thirdlane.com>

yum -y install thirdlane-nginx thirdlane-nginx-config --enablerepo=epel

mkdir -p /etc/nginx/conf.d/https/

#echo Refactor Ngnix config files

# adjust usermenus
#cd /etc/asterisk/
#sed -i -e "s/127.0.0.2/127.0.0.1/g" usermenus.txt 

# prepare PHPFCGI
#cp -f /var/thirdlane_load/updates/phpfcgi.init /etc/init.d/phpfcgi
#chmod +x /etc/init.d/phpfcgi
#/etc/init.d/phpfcgi start
#chkconfig phpfcgi on

# disable httpd
#/etc/init.d/httpd stop
#chkconfig httpd off
#/usr/local/sbin/pbxprotect-client service enable http

# add nginx to bin group
/usr/sbin/usermod -a -G bin nginx 

echo Adjust /etc/webmin/miniserv.conf
cp -f /etc/webmin/miniserv.conf /etc/webmin/miniserv.conf.`date +%F`
sed /etc/webmin/miniserv.conf -i.bak -e "s/^port=.*$/port=22000/"
if [ "`grep -e "^ssl=" -c /etc/webmin/miniserv.conf`" = "1" ]; then
    sed /etc/webmin/miniserv.conf -i.bak -e "s/^ssl=.*$/ssl=0/"
else
    echo "ssl=0" >> /etc/webmin/miniserv.conf
fi

sed -i.backup -e /inetd_ssl/d -e /trust_real_ip/d /etc/webmin/miniserv.conf
echo -e "\ninetd_ssl=1\ntrust_real_ip=1\n" >> /etc/webmin/miniserv.conf

echo Adjust /etc/webmin/config
cp -f /etc/webmin/config /etc/webmin/config.`date +%F`
if [ "`grep -e "^referer=" -c /etc/webmin/config`" = "1" ]; then
    sed /etc/webmin/config -i.bak -e "s/^referer=.*$/referer=localhost/"
else
    echo "referer=localhost" >> /etc/webmin/config
fi

systemctl enable  nginx
systemctl restart nginx