#!/bin/bash
#
###########################################################################
#                                                                         #
# dynip v2.4.0 for TNN                                                    #
#                                                                         #
# Copyright (C) 2003 - 2008 Andreas Stempfhuber <andi@afulinux.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 3 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, see <http://www.gnu.org/licenses/>.   #
#                                                                         #
###########################################################################

cfg="dynip.cfg"		# Konfigurationsdatei fr dynip
parm="../parms.tnb"	# TNNs parms.tnb
import="../now.tnb"	# TNNs now.tnb
axipr="AXIPR"		# TNNs "AXIPR" Befehl
saveparam="SPARAM"	# TNNs "SPARAM" Befehl


# Konfiguration einlesen, mit der TNN-Konfiguration vergleichen und ggf.
# Importdatei erstellen
function readcfg ()
{
  while read line; do
    if [ "$line" ]; then
      call=`echo $line | cut -d " " -f 1`
      fqdn=`echo $line | cut -d " " -f 2`
      protocol=`echo $line | cut -d " " -f 3 | tr [a-z] [A-Z]`
      port=`echo $line | cut -d " " -f 4`

      # haben wir alles was wir brauchen?
      if [ "$call" ] && [ "$fqdn" ]; then
        parmline=`grep -i "^$axipr ADD $call " $parm`
        if [ "$parmline" ]; then
	  oldip=`echo "$parmline" | cut -d " " -f 4`
	  oldprotocol=`echo "$parmline" | cut -d " " -f 5`
          oldport=`echo "$parmline" | cut -d " " -f 6`
        fi

        # neue IP ermitteln
        ip=`gethostip -d $fqdn 2>/dev/null`

	# Debug-Ausgabe
	if [ "$debug" ]; then
          echo "Debug: $call - $fqdn"
          echo "Debug old: $oldip $oldprotocol $oldport"
          echo "Debug new: $ip $protocol $port"
          echo
	fi

        # konnten wir eine IP ermitteln?
	if [ "$ip" ]; then
          # hat sich etwas gendert (IP, Protokoll oder Port)?
          if [ "$oldip" != "$ip" ] || [ "$oldprotocol" != "$protocol" ] \
             || [ "$oldport" != "$port" ]; then
	    echo "$axipr DEL $call" >> $import
	    echo "$axipr ADD $call $ip $protocol $port" >> $import
	  fi
        fi  
      fi
    fi
  done
}


# Main
# ins Arbeitsverzeichnis von dynip wechseln
cd "${0%/*}"

# Debug-Option abfragen
[ "$1" = "-d" ] || [ "$1" = "--debug" ] && debug=1 && shift

# rm-Option abfragen (lscht Import-Datei)
[ "$1" = "--rm" ] && rm -f $import

# wenn keine Konfigurationsdatei existiert hats keinen Sinn
[ ! -f $cfg ] && exit 1

# wenn bereits eine Importdatei existiert soll sie TNN erst abarbeiten
[ -f $import ] && exit 0

# wenn keine Parameterdatei existiert erzeugen wir eine
if [ ! -f $parm ]; then
  touch $import
elif ! grep -qi "^$axipr P$" $parm; then
  # alter AXIPR-Syntax, auf neuen umschalten
  echo "$axipr P" >> $import
  # Timeout auf 0 setzen, damit alle Eintrge gespeichert werden
  echo "$axipr TIMEOUT 0" >> $import
elif ! grep -qi "^$axipr TIMEOUT 0" $parm; then
  # Timeout auf 0 setzen, damit alle Eintrge gespeichert werden
  echo "$axipr TIMEOUT 0" >> $import
else
  # Konfiguration bearbeiten
  egrep -v "^(#|$)" $cfg | readcfg
fi

# wir haben eine Importdatei angelegt, also mssen wir speichern
[ -f $import ] && echo "$saveparam" >> $import

# Rechte setzen
[ -f $import ] && chmod g+rw $import

# Debug-Ausgabe
[ "$debug" ] && [ -f $import ] && cat $import
