#!/bin/sh
#
# Author deelkar@gmx.de
# 
# /usr/local/bin/ip-update
# 
# Version 7.3.2
#
# Requires: yi.bs, ddclient
#
# Public:   yes

# Copyright (C) 2002 "Dirk-Lueder Kreie" <deelkar@gmx.de>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the
#
# Free Software Foundation, Inc.
# 59 Temple Place, Suite 330
# Boston, MA 02111-1307
# USA.

lckfile="/var/sysflags/ip-update.lock"
trap "rm $lckfile; exit" SIGHUP SIGINT SIGTERM

#EXT_IF="ppp0"
EXT_IF="remote"
export LC_ALL=C

if [ "`find $lckfile -cmin +15 2>&1`" = "$lckfile" ]; then
  test -e $lckfile && rm $lckfile
  echo `date`" - Warning: old lockfile \"$lckfile\" exists"
  echo `date`" - Warning: old lockfile \"$lckfile\" exists" > /dev/stderr
fi

test -e $lckfile && echo `date`" - Fatal: lockfile \"$lckfile\" exists"
test -e $lckfile && exit 1
touch $lckfile
chmod 666 $lckfile


zonefiledir="/var/bind/pri"
zonefilename="deelkar.de.zone"

oldip=`cat /var/log/oldip`
oldserial=`cat /var/log/oldserial`
curip=`/usr/local/bin/getip $EXT_IF`
curserial=`date +%Y%m%d%H`
if [ -z "$curip" ]; then
  curip=0.0.0.0
fi

if [ "$oldip" != "$curip" ]; then
  echo "old \"$oldip\" != \"$curip\" new"
  /usr/local/bin/rinker.sh&
  /usr/local/bin/ipv6tunnel&
  rndc freeze
  cp ${zonefiledir}/${zonefilename} /tmp/${zonefilename}.backup-$curserial
  sed -e "s/.*\(; __MARKER__AUTO__IP__.*\)/\t\t\tA\t$curip  \1/" -e 's/.*\(; serial : YYYYMMDDhh\)/\t\t\t\t'"$curserial"' \1/' < /tmp/${zonefilename}.backup-$curserial > ${zonefiledir}/${zonefilename} || cp /tmp/${zonefilename}.backup-$curserial ${zonefiledir}/${zonefilename}
  echo "; $oldserial > $curserial : $oldip > $curip" >> ${zonefiledir}/${zonefilename}
  rndc thaw
  /etc/init.d/named reload
  /usr/local/bin/inwx.sh&
#  /etc/init.d/prosody restart
#  /etc/init.d/apache2 start
#  /etc/init.d/radvd start
#  /usr/local/bin/yi.bs&
fi

echo "$curip" > /var/log/oldip
echo "$curserial" > /var/log/oldserial

rm -f $lckfile

