#!/bin/bash
#
###########################################################################
#                                                                         #
# dynip v2.3.3 for TNN                                                    #
#                                                                         #
# Copyright (C) 2003 - 2004 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 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.          #
#                                                                         #
###########################################################################

cfg="dynip.cfg"		# dynip configuration file
parm="../parms.tnb"	# TNN's params.tnb
import="../now.tnb"	# TNN's now.tnb

axipr="AXIPR R"		# TNN's "AXIPR R" command
saveparm="SPARAM"	# TNN's "SPARAM" command

defaultprotocol="U"	# default protocol
defaultport="93"	# default port


function readcfg ()
{
  while read line; do
    if [ "$line" ]; then
      comment=`echo $line | cut -b 1-1`
      call=`echo $line | cut -d " " -f 1`
      fqdn=`echo $line | cut -d " " -f 2`
      protocol=`echo $line | cut -d " " -f 3 | cut -b 1-1 | tr [a-z] [A-Z]`
      port=`echo $line | cut -d " " -f 4`

      # falls wir kein Protokoll haben verwenden wir den Defaultwert...
      [ -z "$protocol" ] && protocol=$defaultprotocol

      # falls wir keinen Port haben verwenden wir den Defaultwert...
      [ -z "$port" ] && port=$defaultport

      #echo "$comment - $call - $fqdn - $protocol - $port"

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

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

	# Debug Ausgabe
	if [ "$debug" ]; then
          echo "$call - $fqdn"
          echo "old: $oldip $oldprotocol $oldport"
          echo "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 - $call" >> $import
	    echo "$axipr + $call $fqdn $protocol $port" >> $import
	    #echo "$axipr + $call $ip $protocol $port" >> $import
	    echo "$saveparm" >> $import
	  fi
        fi  
      fi
    fi
  done
}


# go to the working directory of dynip
base=${0%/*}
cd $base

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

# 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
  echo "$saveparm" >> $import
else
  # Konfiguration einlesen
  readcfg < $cfg
fi

[ -f $import ] && chmod g+rw $import

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

