#!/bin/bash

# Insert the account filenames to be checked below
# Remember to increment the account[#] for new entries

#account[0]=larry.iptables
#account[1]=curly.iptables
#account[2]=moe.iptables

#     ipchecker (c) Copyright 2012, Ward Mundy & Associates LLC. All Rights Reserved.
#
#                    ipchecker 1.3U is licensed under the GPL2 license
#
#  For a copy of license, visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
#   For additional information, contact us: http://pbxinaflash.com/about/comment.php



# Don't make changes below this line
cd /root
element_count=${#account[@]}
restartflag=0
index=0
while [ "$index" -lt "$element_count" ]
do
 if [ ! -s "${account[$index]}" ]; then
  echo "Ooops. ${account[$index]} not found."
  ip="0.0.0.0"
  test=$ip
  fqdn=$ip
 else
 fqdn=`cat ${account[$index]} | cut -f 1 -d " "`
   ip=`cat ${account[$index]} | cut -f 2 -d " "`
 test=`dig +short $fqdn`
 LEN=${#test}
 if [ $LEN -gt 15 ]; then
  echo "Ooops. We gotta a DIG overage problem. Forcing a match on IP to avoid disaster."
  test=$ip
 fi
 if [ $LEN -lt 7 ]; then
  echo "Ooops. We gotta a DIG fail problem. Forcing a match on IP to avoid disaster."
  test=$ip
 fi
 if [ "$ip" = "$fqdn" ]; then
  test=$ip
  echo "Ooops. Missing FQDN. Entry ignored."
 fi
fi
if [ "$ip" != "$test" ]; then
 echo "Account ${account[$index]} CHANGED"
 restartflag=1
 echo "  FQDN: $fqdn"
 echo "OLD IP: $ip"
 echo "NEW IP: $test"
 echo "$fqdn $test" > /root/${account[$index]}
#else
# echo "Account ${account[$index]} OK"
fi
((index++))
done
if [ $restartflag -eq 1 ]; then
 /usr/local/sbin/iptables-restart
fi

exit 0

#eof
