#!/bin/sh
# ver 0.9.3  Apr 14, 2008, hernad@bring.out.ba

HOSTNAME=router-wan-sa-1
NAMESERVER=128.177.28.71

TMP_DIR=/tmp/refresh_ip

if [ ! -d $TMP_DIR ] 
then
 mkdir $TMP_DIR
fi

cd $TMP_DIR

IP=`ifconfig ppp0 | grep -o  "inet addr:\(.*\) P-t-P" | sed -e "s/inet addr://" | sed -e "s/ P-t-P//" | sed -e "s/ //g"`

LAST_IP=`cat last_ip.txt`

mv last_call.txt old_call.txt
if [ "${LAST_IP}" == "${IP}" ]
then
        echo "`date` :  no ip change ${IP}">last_call.txt
	exit 0
else
        echo "`date` : IP CHANGE has happened - old ${LAST_IP}  new ${IP} !">last_call.txt
fi


(cat <<TEXT
\$TTL 60   
@               IN SOA          @       root (
                                200705251      ; serial
                                60              ; refresh
                                20              ; retry - update retry
                                3W12h           ; expiry - 3 weeks + 12 hours
                                10 )            ; minimum - 2 hours
                MX              10 mail.sigma-com.net.
                MX              50 mail-50.sigma-com.net.
                IN NS           ns.sigma-com.net.


ns              IN A            128.177.28.71
vps-oh1         IN A            128.177.28.71

officesa        IN A           ${IP} 
officeze        CNAME           officeze.dyndns.org.


mail-50         CNAME           vps-oh1
mail            CNAME           mail-10

openvpn		CNAME		officesa
masine          CNAME           web-1
www.masine      CNAME           web-1

trac            CNAME           web-1
repos           CNAME           web-1
ifolder         CNAME           web-1

jabber          CNAME           java-infra-2

svn            CNAME            web-1

dev-infra-2    CNAME            officesa
java-infra-2   CNAME            officesa

zimbra         CNAME            mail-10
blogs          CNAME            web-1
forums         CNAME            web-1
www             CNAME           web-1

web-1	       CNAME		dev-infra-2
mail-10        CNAME            dev-infra-2

web-146		CNAME		officesa
.               CNAME 		officesa

TEXT
) > tmp.zone


echo "ip sigma-com.net =  $IP"

FEEDBACK="0"                           
reload_nameserver() {           

echo "restarting name server"
scp -o -y -i /etc/dropbear/${HOSTNAME}.key tmp.zone root@${NAMESERVER}:/var/named/sigma-com.net.zone
FEEDBACK=`ssh -y -i /etc/dropbear/${HOSTNAME}.key root@${NAMESERVER} service named restart | grep -c "\[.*OK.*\]"`
# 2 => 2  x [ OK ]                                   
}                                                           
                                                           
                                                           
while [ "$FEEDBACK" != "2" ]                             
do                                   
   reload_nameserver         
   echo "feedback=_${FEEDBACK}_"                                                          
   if [ "$FEEDBACK" == "2" ]
   then
      echo $IP>last_ip.txt  
      echo "ip change has finished">>last_call.txt
      exit 0
   else
     echo "error sleep 10sec"      
     echo "error10 ">>last_call.txt
     sleep 10                        
   fi                             
done                                                   
                                                       
echo "ip change unsucessfull (after while) ?!?">>last_call.txt

