#!/bin/sh

LOGGER=/usr/bin/logger

$LOGGER "Ran for $reason"

#if [ $reason = FAIL ]; then
# $LOGGER "FAIL- aborting early"
# exit
#fi

#PROXYCONF=/usr/local/etc/oops/oops.cfg
#PROXYCONFBASE=${PROXYCONF}.base
#PROXYPID=/var/run/oops/oops.pid
#PROXYSTART=/usr/local/sbin/oopsctl
#PROXYSHORT=oops

PROXYCONF=/usr/local/etc/tinyproxy/tinyproxy.conf
PROXYCONFBASE=${PROXYCONF}.base
PROXYPID=/var/run/tinyproxy/tinyproxy.pid
PROXYSTART=/usr/local/etc/rc.d/tinyproxy.sh.sample
PROXYSHORT=tinyproxy

STUNNEL=/usr/local/etc/rc.d/stunnel.sh.sample
DEFHOSTNAME=inchoate.localdomain

setup() {
  if [ -z "$1" ]; then
    ip="127.0.0.1"
  else
    ip=$1
  fi
  case "$ip" in
    10.0.2.*)
      PROXYADDR=proxy.dons.net.au
      PROXYPORT=8080
      NOPROXY=
      SCRIPT=/home/darius/bin/dons.sh
      STUNNELLOC=dons
      ;;

    203.31.81.*)
      PROXYADDR=proxy.gsoft.com.au
      PROXYPORT=3128
      HAVEPROXY=
      SCRIPT=/home/darius/bin/gsoft.sh
      STUNNELLOC=gsoft
      ;;
    
    203.38.248.*)
      PROXYADDR=proxy
      PROXYPORT=3128
      HAVEPROXY=
      SCRIPT=
      STUNNELLOC=gsoft
      ;;

    *)
      PROXYADDR=
      PROXYPORT=
      HAVEPROXY="#"
      SCRIPT=
      STUNNELLOC=gsoft
      ;;
    esac 

    $LOGGER "Setting up for $ip - $STUNNELLOC, reason - $reason"
    $SCRIPT
    NS=`cat /etc/resolv.conf | grep nameserver | awk '{print $2}' | head -1`

    if [ -r $PROXYPID ]; then
        kill `cat $PROXYPID`
    fi
    if [ ! -s $PROXYCONFBASE ]; then
	logger "$PROXYCONFBASE doesn't exist or is too small, quitting"
	killall dhclient
	exit 1
    fi
    sed -e "s#%%PROXYADDR%%#$PROXYADDR#g" \
	-e "s#%%PROXYPORT%%#$PROXYPORT#g" \
	-e "s#%%HAVEPROXY%%#$HAVEPROXY#g" \
	-e "s#%%NS%%#$NS#" \
	<$PROXYCONFBASE >$PROXYCONF
    if [ ! -s $PROXYCONF ]; then
      logger "$PROXYCONF is zero bytes long?!"
      killall dhclient
      exit 1
    fi
    logger "Running $PROXYSTART start"
    $PROXYSTART start | logger -t $PROXYSHORT 2>&1 &

    #$STUNNEL stop
    #killall -9 stunnel
    #$STUNNEL start $STUNNELLOC

    # HUP sendmail so it picks up the hostname change
    killall -HUP sendmail
}

if [ x$reason = xPREINIT ]; then
    status=`ifconfig $interface | grep status: | awk '{print $2}'`
    logger "Status - $status";
    if [ ! -z $status ]; then
	if [ $status != "active" ]; then
	    logger "Bailing, not active"
	    #exit_status=1
	fi
    fi
fi

# No IP address allocated (ie couldn't find a server)
if [ x$reason = xTIMEOUT ] || [ x$reason = xFAIL ]; then
  logger "Setting up hostname and IP for non-connect for $interface"
  hostname="inchoate.localdomain"
  inet="10.2.1.1 netmask 0xffffff00"
  route=""

  if [ -n "$hostname" ]; then
    #hostname $hostname
  fi 
  if [ -n "$inet" ]; then
    ifconfig $interface $inet
  fi
  if [ -n $route ]; then
    route delete default
    route add default $route
  fi

  setup
  #killall dhclient
fi

# Found a new IP from a working server
if [ x$reason = xBIND ] || [ x$reason = xRENEW ] || [ x$reason = xBOUND ] || \
   [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then

  # Don't change hostname, it hoses too much stuff
  if [ 0 ]; then
  current_hostname=`hostname`
  if [ x$new_host_name != x ]; then
    # Canonify the passed in hostname
    canon_host=`host -t a $new_host_name | awk '{print $1}'`
    
    if [ $? -eq 0 ]; then
      $LOGGER "New Derived Hostname: $canon_host"
      #hostname $canon_host
    else
      $LOGGER "Unresolvable address ($canon_host), setting default hostname"
      #hostname $DEFHOSTNAME
    fi
  else
    dhost=`host -t a $new_ip_address | awk '{print $5}'` 
    if [ x$dhost != x ]; then
      $LOGGER "Reversed $new_ip_address for Hostname: $dhost"
      #hostname $dhost
    else
      $LOGGER "Un-reversable $new_ip_address, setting default hostname"
      #hostname $DEFHOSTNAME
    fi
  fi
  fi
  setup $new_ip_address $reason
fi

