#!/bin/bash
# afrihost - script to get and remove spammers that have been unlocked on spe 

# cpanel check
if ! [ -f "/usr/local/cpanel/cpanel" ]; then
       echo "No cpanel"
       exit
fi

# Prevent duplicate run of script
echo -e ${bldblu}"- Created lock file"
CONTROL="/tmp/spe_unlocked_ids"

single_run_check()
{
   if [[ -f $CONTROL ]] ; then
          echo -e ${bldred}"Script is already running or didn't exit correctly previously.${txtrst}
          Check ps log and kill already running script and/or remove file $CONTROL"
          exit 2
   fi 
touch $CONTROL
}
single_run_check

cd /root
 url="http://bouncer.ifarhost.co.za/clients/getlist"
 blocklists="exim_deny_auto_local  exim_deny_spe_sender  exim_deny_this_host"
 unblocked=$(curl $url)
 if [[ $unblocked = "" ]] ; then exit ; fi
 for file in $blocklists ; do
  for email in $unblocked ; do
   #remove each email from blocklist
   sed -i.blocklist_bak "/$email/d" /etc/$file
  done
 done

rm -fv $CONTROL
