#!/bin/sh
# Release Version 0.61
#set -x

# If you prefer FQDN names rather than leafnames set to 'y';
# This causes the code to ignore the HOSTNAME returned by the BOOTP
# server and do a gethostbyname() on the IP number we were given.
# This will only work if the resolver can be set up or you have the
# correct entry in /etc/hosts (!)
FORCEFQDN='n'

# Put search not domain line in resolv.conf for 4.9.3 based resolver
# code (still works with older code too) if you like the old 4.8.3
# search rules with the new resolver code then set to 'y'
MAKESEARCH='y'

BOOTPC=/sbin/bootpc
# location of ifconfig
IFCONFIG=/sbin/ifconfig
# location of route
ROUTE=/sbin/route
# location of hostname binary
BINHOST=/bin/hostname
# What device to do the bootp on
DEV=eth0
# What address to send bootp request to (broadcast by default)
ASKSERVER="255.255.255.255"
# To set the time we are willing to wait for a reply (roughly)
# This defaults to 70 seconds anyway in the code.
TW="--timeoutwait 70"
# The location of the resolver config file
RCONF=/etc/resolv.conf
# location of the hosts file
EHOSTS=/etc/hosts
## Location of tmp file for storing options
TMPFILE=/tmp/bootp-results.$$
INITFILE=/etc/init.d/network
BOOTOPT=/var/named/boot.options

## Start of the functions
# Stick the nameserver lines in the resolver file.
setservers() {
# Insert the DNS servers themselves
  for i in $1
  do
    echo "nameserver $i" >> ${RCONF}
  done
}

# A function for setting the DNS stuff if we have all the info
setresolv() {
  [ -f ${RCONF} ] && mv -f ${RCONF} ${RCONF}.old
# Insert provided information
  if [ "${MAKESEARCH}" = 'y' ]; then
    echo "search $2" > ${RCONF}
  else
    echo "domain $1" > ${RCONF}
  fi
  setservers "127.0.0.1 $3"
}

# A function for setting the DNS stuff if we don't know out domainname
fakeresolv() {
  [ -f ${RCONF} ] && mv -f ${RCONF} ${RCONF}.fake
# This means nothing, but provides a domain line (some(old) revolvers
#  break if there isn't one present.)
  echo "domain ." > ${RCONF}
  setservers "$1"
}

# And putting the original back if it exists
unfakeresolv() {
  [ -f ${RCONF}.fake ] && mv -f ${RCONF}.fake ${RCONF}
}

# update the DNSSERVERS but not the domainname, in case of
# reverse lookup failure.
updateresolv() {
  [ -f ${RCONF} ] && mv -f ${RCONF} ${RCONF}.old
# Copy over the domainname from the old file
  egrep 'domain|search' ${RCONF}.old > ${RCONF}
  setservers "$1"
}

# Remove the networking by taking down the interface
netdown() {
  ${ROUTE} del default
  ${IFCONFIG} ${DEV} down
}

## Start of the actual work
# Bring up minimal networking use 0.0.0.0 as our address as we don't
# know it yet (Means "Me but I don't know my address or network")
${IFCONFIG} ${DEV} up ${IFCONFOPTS1} 0.0.0.0
${ROUTE} add default dev ${DEV}

# Perform the bootp  --  doesn't return unless it gets an answer
if ${BOOTPC} --dev ${DEV} --server ${ASKSERVER} --returniffail ${TW} > ${TMPFILE}
then
# Take down networking (use the 0.0.0.0 for as short a time as possible)
  netdown
# Read in the values   
  eval `cat ${TMPFILE}`
# And delete the temporary file
  rm ${TMPFILE}
else
# Take down networking (use the 0.0.0.0 for as short a time as possible)
  netdown
# give message and quit
  echo "Bootp failed -- disabling network."
  exit 1
fi

[ -z "$NETMASK" ] && NETMASK="255.255.0.0"
[ -z "$NETWORK" ] && NETWORK="130.88.0.0"
[ -z "$BROADCAST" ] && BROADCAST="130.88.255.255"
[ -z "$GATEWAY" ] && GATEWAY="130.88.205.250"
umask 022
cat <<EOF2 >${INITFILE}
#! /bin/sh
${IFCONFIG} lo 127.0.0.1
route add -net 127.0.0.0
IPADDR=${IPADDR}
NETMASK=${NETMASK}
NETWORK=${NETWORK}
BROADCAST=${BROADCAST}
GATEWAY=${GATEWAY}
EOF2
echo >>${INITFILE} 'ifconfig eth0 ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}'
echo >>${INITFILE} 'route add -net ${NETWORK}'
echo >>${INITFILE} '[ "${GATEWAY}" ] && route add default gw ${GATEWAY} metric 1'
. ${INITFILE}

eval `${BOOTPC} --in2host ${IPADDR}`
[ -z "$HOSTFULL" -o -z "$HOSTLEAF" ] && eval `lmhostname ${IPADDR}`
cat <<EOF1 >${EHOSTS}
127.0.0.1	localhost
EOF1
[ -n "$HOSTFULL" -o -n "$HOSTLEAF" ] && echo "${IPADDR}	${HOSTFULL} ${HOSTLEAF}" >>${EHOSTS}
[ -n "$HOSTLEAF" ] && echo "$HOSTLEAF" >/etc/hostname
[ -n "$HOSTLEAF" ] && ${BINHOST} "$HOSTLEAF"

[ -z "$DOMAIN" ] && DOMAIN="${HOSTFULL#$HOSTLEAF.}"
SEARCH=''
TEMP="$DOMAIN"
while [ -n "$TEMP" ];do
  TA="${TEMP%%.*}"
  if [ -z "$TA" ];then TEMP=''
  elif [ "$TA" = "$TEMP" ];then TEMP=''
  else SEARCH="$SEARCH $TEMP"
    TEMP="${TEMP#$TA.}"
  fi
done
if [ -n "${DNSSRVS}" -a -n "${DOMAIN}" ];then
    setresolv "${DOMAIN}" "${SEARCH}" "${DNSSRVS}" 
    install -d -m 755 /var/named
    cat <<EOF3 >${BOOTOPT}
;
; Options for name server
; Use \`bindconfig' to automatically configure this file
;

forwarders	${DNSSRVS}

; type		domain			source		file
primary		localhost				named.local
primary		127.in-addr.arpa			named.rev-local

;; Custom configurations below (will be preserved)
EOF3
fi
