#!/bin/bash
# 
# Author: deelkar@gmx.de
#
# /usr/local/bin/getip
#
# Version:  7.3.1
#
# Requires: ifconfig
#
# 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.

export LC_ALL=C

nameserver=8.8.8.8

if [ -n "$1" ]; then
  EXT_IF="$1"

  if echo $EXT_IF | grep -q "ppp"; then
     IP_PARAM=$(/bin/ifconfig $EXT_IF | grep -v inet6 | grep inet | sed -e 's/\(inet.\)\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)[^0-9].*/\2/')
  elif echo $EXT_IF | grep -q "external\|remote"; then
     IP_PARAM=$(/usr/bin/wget -q -4 -O- 'http://www4.deelkar.net/cgi-bin/getip.sh?v=4&format=plain')
  fi

#  echo -n "$EXT_IF: "
  echo $IP_PARAM

else
  EXT_IF=ppp0

  IP_PARAM=$(/bin/ifconfig $EXT_IF 2>/dev/null | grep -v inet6 | grep inet | sed -e 's/\(inet.\)\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)[^0-9].*/\2/')

  if [[ -z "$IP_PARAM" ]]; then
    IP_PARAM=$(/usr/bin/wget -q -4 -O- 'http://www4.deelkar.net/cgi-bin/getip.sh?v=4&format=plain')
    EXT_IF="remote"
  fi

  echo -n "$EXT_IF: "
  echo $IP_PARAM

#  nslookup deelkar.ath.cx $nameserver |grep  Address: | tail -n1 | sed -e 's/.*:/deelkar.ath.cx:/g'
  nslookup artena.deelkar.de $nameserver | grep Address: | grep '\.' | tail -n1 | sed -e 's/.*:/artena.deelkar.de:/g'
  nslookup deelkar.3-a.net $nameserver |grep  Address: | tail -n1 | sed -e 's/.*:/deelkar.3-a.net:/g'
  nslookup artena.deelkar.de ::1 |grep  Address: | grep '\.' | tail -n1 | sed -e 's/.*:/artena.deelkar.de (::1):/g'
  nslookup artena.deelkar.de 192.168.70.20 |grep  Address: | grep '\.' | tail -n1 | sed -e 's/.*:/artena.deelkar.de (lennier):/g'
  nslookup artena.deelkar.de 192.168.64.1 |grep  Address: | grep '\.' | tail -n1 | sed -e 's/.*:/artena.deelkar.de (FB):/g'
  nslookup deelkar.yi.org $nameserver |grep  Address: | tail -n1 | sed -e 's/.*:/deelkar.yi.org:/g'
fi
