#!/bin/bash
#
# This script identifies where I am based on a DHCP assigned address.
#
# Andrew McMillan <whereami @ mcmillan.net.nz> 2001-08-03
#  - with much code copied from Brian Mays PCMCIA package
#
WIRELESS_DEVICE=${1:-"eth1"}
RESOLV=/etc/resolv.conf

METHOD=dhcpc
[ -e /sbin/pump ] && METHOD=pump

#
# Ensure DHCP client isn't running
/etc/init.d/dhcpcd stop >>/dev/null 2>&1

# We put all of the following in a subshell as that means that we
# can 'exit' from it OK, which makes it easier to cut and paste
# Brian Mays stuff from the /etc/pcmcia/network script!

(
  /sbin/ifconfig $WIRELESS_DEVICE up 0.0.0.0 >>/dev/null 2>&1
  /sbin/route add default dev $WIRELESS_DEVICE >>/dev/null 2>&1
  if [ -x /sbin/dhcpcd ] ; then
      /sbin/dhcpcd -t 10 $WIRELESS_DEVICE >/dev/null 2>&1 || exit 1
  elif [ -e /sbin/pump ]; then
      # Might be advisable to set a timeout in your /etc/pump.conf
      /sbin/pump -i $WIRELESS_DEVICE 2>&1 | grep -q "failed" && exit 1
  elif [ -x /sbin/dhclient ] ; then
      /sbin/dhclient $WIRELESS_DEVICE >/dev/null 2>&1 || exit 1
  else
      exit 1
  fi

  #
  #
  # Chances are we have a valid location now
  #
  LOCATED=1
  if ( ifconfig | grep "192\.168\.55\." >>/dev/null ); then
    LOCATION=home
  else
    LOCATED=0
    exit 1
  fi

  #
  # We only put the location out if we find one.
  #
  echo $LOCATION >>iwillbe
  LOCATED=1
)
