#!/bin/bash
# description: Start/stop Firewall
# chkconfig: 2345 13 90

# do not use systemctl on C7
SYSTEMCTL_SKIP_REDIRECT=1

. /etc/init.d/functions

case "$1" in
'start')
       sysctl -e -p
       echo -n "Starting firewall ... "
       /usr/sbin/firewall.pl
       RETVAL=$?
       if [[ $RETVAL -eq 0 ]]; then
         echo_success
         echo
       elif [[ $RETVAL -eq 1 ]]; then
         echo_warning
         echo
       else
         echo_failure
         echo
       fi

       service iptables save
       if [[ -f /usr/sbin/nginx ]]; then
        if [[ -f /opt/hgmods/proxy_cron.pl ]]; then
           /opt/hgmods/proxy_cron.pl
        elif [[ -f /opt/eig_linux/bin/proxy_cron.pl ]]; then
           /opt/eig_linux/bin/proxy_cron.pl
        fi
       else
        if [[ -f /opt/eig_linux/bin/proxy_cron2.pl ]]; then
          /opt/eig_linux/bin/proxy_cron2.pl &> /dev/null;
        fi
       fi
       if [[ -f /etc/init.d/bandmin ]]; then
         /etc/init.d/bandmin start
       fi
       ;;
'stop')
       iptables -F
       iptables -X
       RETVAL=$?
       ;;
'restart') 
       if [[ -f /etc/init.d/bandmin ]]; then
         /etc/init.d/bandmin stop
       fi
       sysctl -e -p
       echo -n "Restarting firewall ... "
       /usr/sbin/firewall.pl
       RETVAL=$?
       if [[ $RETVAL -eq 0 ]]; then
         echo_success
         echo
       elif [[ $RETVAL -eq 1 ]]; then
         echo_warning
         echo
       else
         echo_failure
         echo
       fi

       service iptables save
       if [[ -f /usr/sbin/nginx ]]; then
        if [[ -f /opt/hgmods/proxy_cron.pl ]]; then
           /opt/hgmods/proxy_cron.pl
        elif [[ -f /opt/eig_linux/bin/proxy_cron.pl ]]; then
           /opt/eig_linux/bin/proxy_cron.pl
        fi
       else
        if [[ -f /opt/eig_linux/bin/proxy_cron2.pl ]]; then
          /opt/eig_linux/bin/proxy_cron2.pl &> /dev/null;
        fi
       fi
       if [[ -f /etc/init.d/bandmin ]]; then
         /etc/init.d/bandmin start
       fi
       ;;
*)
       echo "Usage: $0 { start | stop | restart }"
       RETVAL=1
       ;;
esac
exit $RETVAL

