#! /bin/bash
# Afrihost - complete restart of web service for emergency situations.
 
# cpanel check
if ! [ -f "/usr/local/cpanel/cpanel" ]; then
       exit
fi

if egrep -l  ".*-.*-.*-" /root/.config/.litespeed_ls 2>/dev/null ; then 
 /usr/local/afribin/configs/cpanel_nginx_disable
 /usr/local/lsws/admin/misc/cp_switch_ws.sh lsws
 killall -9 lsphp ;  service lsws restart ;  touch /usr/local/lsws/admin/tmp/.lsphp_restart.txt
 nlist| egrep  'http|ngin|lite'
 exit   
fi

# Check if both nginx and apache running as normal and restart
if netstat -ntulp |  egrep  'http'| grep  ":8080" &&  netstat -ntulp | egrep  'http' | grep  ":8443" &&
 netstat -ntulp |  egrep  'nginx'| grep  ":80" && netstat -ntulp |  egrep  'nginx'| grep  ":443"  ; then
 echo "== apache and nginx running as expected [ Restarting ]=="
 killall -9 nginx httpd ; service nginx start ; service httpd start
 echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_web_services ::::" | tee -a /var/log/ips.log
 nlist| egrep  'http|ngin|lite'
 exit
fi 

# Checks if nginx running on port 80 and 443 but apache not listening on proxy ports - quick-fix
if netstat -ntulp |  egrep  'nginx'| grep  ":80" && netstat -ntulp |  egrep  'nginx'| grep  ":443"  ; then
  if ! netstat -ntulp |  egrep  'http'| grep  ":8080" ||  netstat -ntulp | egrep  'http' | grep  ":8443" ; then
   echo "nginx running on port 80 and 443 but apache isn't running on 8080 8443"
   killall -9 nginx httpd 
   whmapi1 set_tweaksetting key=apache_port value=0.0.0.0:80
   whmapi1 set_tweaksetting key=apache_ssl_port value=0.0.0.0:443
   frob rebuild 
#   if timeout 5  dig @localhost google.com +short  | ipgrep | grep [0-9] >/dev/null  ; then echo -e  "nameserver 127.0.0.1\nnameserver 197.242.155.155\nnameserver 197.242.144.144"  > /etc/resolv.conf  ; else /scripts/setupnameserver --force bind  ; if timeout 5  dig @localhost google.com +short  | ipgrep | grep [0-9] >/dev/null  ; then echo -e  "nameserver 127.0.0.1\nnameserver 197.242.155.155\nnameserver 197.242.144.144"  > /etc/resolv.conf ; fi  ; fi; /usr/local/afribin/configs/engintron_install.sh ; sh  /usr/local/afribin/configs/cpanel_nginx_custom_rules_generate.sh
    echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_web_services ::::" | tee -a /var/log/ips.log
    nlist| egrep  'http|ngin|lite'
   exit
  fi
fi

# Checks if http running on proxyport but nginx not running.
if  netstat -ntulp |  egrep  'http'| grep  ":8080" &&  netstat -ntulp | egrep  'http' | grep  ":8443" ; then
 if ! netstat -ntulp |  egrep  'nginx'| grep  ":80" || netstat -ntulp |  egrep  'nginx'| grep  ":443" ; then
  echo "http running on proxy but nginx not on port 80/443"
   nginx  -t  > /dev/null 2>&1   ; [ $? -eq 1 ] && 
   sed -i   's#\.\*\.#\.#' /etc/nginx/conf.d/default_https.conf
   rm -fv /etc/nginx/custom_rules
   sh  /usr/local/afribin/configs/cpanel_nginx_custom_rules_generate.sh
   if ! [ -f "/etc/nginx/custom_rules" ]; then
        touch /etc/nginx/custom_rules
   fi
   killall -9 nginx httpd ; service nginx start ; service httpd start
   echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_web_services ::::" | tee -a /var/log/ips.log
   nlist| egrep  'http|ngin|lite'
   exit
 fi
fi
