#! /bin/bash
#
# Written by Andrew McMillan, 10th November 1999
#  - styled on the pcmcia 'network' script by David Hinds (thanks)
#

DEVICE=whereami
RESOLV=/etc/resolv.conf

function buildresolv() {
  for PART in $@ ; do
    case "$PART" in
      domain|search|nameserver)
        echo -en "\n$PART"
		  ;;
      *)
	      echo -en " $PART"
		  ;;
    esac
  done
  echo ""
}
 

if [ -x '/sbin/resolvconf' ] ; then
  # Just use resolvconf
  buildresolv $@ | resolvconf -a $DEVICE 2>&1 | logger -t whereami-setresolver

else
  # Insert a section into the file in a manner similar to
  # the entries inserted by the pcmcia package
  if [ "$1" != "" ] ; then
    echo -n "# $DEVICE begin" > $RESOLV.N
    buildresolv $@ >> $RESOLV.N
    echo -e "\n# $DEVICE end" >> $RESOLV.N

    # Now replace that section into the existing file
    sed -e "/# $DEVICE begin/,/# $DEVICE end/d" $RESOLV >> $RESOLV.N

    mv $RESOLV.N $RESOLV
  fi
fi
