#!/bin/sh

#
# get-ip  (C) 2001 Malte J. Wetz <mail@malte-wetz.de>
#
# Script for retrieving the IP adress of any network interface.
# Result will be printed to stdout. For use with raccess4vbox.
#
# To use this script for a specific network interface, simply
# create a symlink named ip-xxx (where xxx is the name of the
# interface) and call that symlink from raccess4vbox.
# Therefore, you will only need to adopt this script to your
# system.
#
# Example:
#     earth:/usr/share/isdn/vbox/bin # ln -s getip ip-ippp0
#     earth:/usr/share/isdn/vbox/bin # ip-ippp0
#     2 & 1 & 2 & dot & 1 & 1 & 3 & dot & 5 & dot & 1 & 0
#     earth:/usr/share/isdn/vbox/bin # _
#


################################################################
# SETTINGS: Change these parameters to meet your requirements.
################################################################

# Full path of ifconfig-binary. Use "which ifconfig" if you
# don't know it yourself.
IFCONFIG=/sbin/ifconfig

# Full path of grep-binary. Use "which grep" if you
# don't know it yourself.
GREP=/usr/bin/grep

# Full path of cut-binary. Use "which cut" if you
# don't know it yourself.
CUT=/usr/bin/cut

# Full path of awk-binary. Use "which awk" if you
# don't know it yourself.
AWK=/usr/bin/awk

# These strings will be used to generate output text. Change
# them to adopt output to your language.
#
# Uncomment these line for german:
#DOT="punkt"
#NOIFACE="nicht verfgbar"
# Uncomment this line for english:
DOT="dot"
NOIFACE="not available"

################################################################
# END SETTINGS: There should be nothing to change below this
# point.
################################################################


# Get basename and parse the interface name from it.
BASE=${0##*/}
LINK=${BASE#*ip-}

# Check if interface name was correctly retrieved.
if [ -z "$LINK" ] || [ "$LINK" = "$BASE" ] ; then
  cat << EOF >&2

  Error. Do not call this script directly. Instead, create a symlink named
  ip-xxx pointing to $BASE, where xxx is the name of the network interface
  you want to get the IP from. See comments in $BASE for details.

EOF
  exit 1
fi

IF=$LINK

# Make a "hidden" call to ifconfig to check wether the
# network device exists
test -z "$NOIFACE" || {
  $IFCONFIG $IF >/dev/null 2>&1 || {
    echo "$NOIFACE: $IF"
    exit 1
  }
}

# Get the ip adress of that interface
IP=`$IFCONFIG $IF | $GREP "inet addr:" | $CUT -d : -f 2 | $CUT -d ' ' -f 1`
if [ -n "$IP" ]; then
	IPN=""
	for i in `echo $IP | $AWK 'gsub(//," ")'`; do
		if [ "$i" = "." ]; then
			i=$DOT
		fi
		IPN="$IPN & $i"
	done
	IPN=${IPN:3}
	echo "$IPN"
fi

exit 0
