#!/bin/sh
#
# DHCP auto configuration.
#
function dhcp_auto_configuration () {
    #
    # DNS server.
    #
    AUTO_DNS_SERVER=`/bin/grep -m 1 "option  *domain-name-servers" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *domain-name-servers  *\"*\([^ ;,\"]*\)\"*[ ;,]*.*$/\\1/"`
    #
    # Time server.
    #
    AUTO_TIME_SERVER=`/bin/grep -m 1 "option  *time-servers" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *time-servers  *\"*\([^ ;,\"]*\)\"*[ ;,]*.*$/\\1/"`
    #
    # NTP server.
    #
    AUTO_NTP_SERVER=`/bin/grep -m 1 "option  *ntp-servers" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *ntp-servers  *\"*\([^ ;,\"]*\)\"*[ ;,]*.*$/\\1/"`
    #
    # NFS root path.
    #
    AUTO_ROOT_PATH=`/bin/grep -m 1 "option  *root-path" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *root-path  *\"*\([^ ;,\"]*\)\"*.*$/\\1/"`
    #
    # NIS domain.
    #
    AUTO_NIS_DOMAIN=`/bin/grep -m 1 "option  *nis-domain" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *nis-domain  *\"*\([^ ;,\"]*\)\"*[ ;,]*.*$/\\1/"`
    #
    # NIS server.
    #
    AUTO_NIS_SERVER=`/bin/grep -m 1 "option  *nis-servers" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *nis-servers  *\"*\([^ ;,\"]*\)\"*[ ;,]*.*$/\\1/"`
    #
    # LPR server.
    #
    AUTO_LPR_SERVER=`/bin/grep -m 1 "option  *lpr-servers" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *lpr-servers  *\"*\([^ ;,\"]*\)\"*[ ;,]*.*$/\\1/"`
    #
    # Log server.
    #
    AUTO_LOG_SERVER=`/bin/grep -m 1 "option  *log-servers" \
        /var/lib/dhcp3/dhclient.leases \
        | sed "s/^ *option  *log-servers  *\"*\([^ ;,\"]*\)\"*[ ;,]*.*$/\\1/"`
    #
    # Restart the portmapper.
    #
    if [ "$AUTO_ROOT_PATH" != "" ] || [ "$AUTO_NIS_SERVER" != "" ]
    then
        #
        # Need to restart the portmapper.
        #
        /etc/init.d/portmap stop
        /etc/init.d/portmap start
    fi  
    #
    # Rebuild the file "/etc/resolv.conf"
    #
    if echo $AUTO_DNS_SERVER | grep "^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$"
    then
        #
        # Update.
        #
        cp -f /etc/resolv.conf /etc/resolv.conf.orig
        #
        rm -f                                 /etc/resolv.conf
        echo "nameserver $AUTO_DNS_SERVER" >  /etc/resolv.conf
        #
    fi      
    #
    # Try to update the clock.
    #
    if [ "$AUTO_NTP_SERVER" != "" ]
    then
        #
        # We don't want to wait for the time server, so we
        # put all inside a (...) and send it in background.
        #
        (
            if ( [ -x /usr/sbin/ntpdate ] && ntpdate -u -b -s $AUTO_NTP_SERVER ) \
               || [ "$AUTO_TIME_SERVER" != "" ] \
                  && ( [ -x /usr/sbin/rdate ] && rdate -s $AUTO_TIME_SERVER )
            then
                hwclock -u -w 
            fi
        )  2>&1 > /dev/null &
        #
    elif [ "$AUTO_TIME_SERVER" != "" ]
    then
        #
        # We don't want to wait for the time server, so we
        # put all inside a (...) and send it in background.
        #
        (
            if ( [ -x /usr/sbin/rdate ] && rdate -s $AUTO_TIME_SERVER )
            then
                hwclock -u -w 
            fi
        )  2>&1 > /dev/null &
        #
    fi      
    #
    # Mount "/home/" from a remote NFS server, and
    # modify the "/etc/fstab".
    #
    if [ "$AUTO_ROOT_PATH" != "" ]
    then
        #
        # Remove last "/" if any.
        #
        AUTO_ROOT_PATH=`echo $AUTO_ROOT_PATH | sed "s/^\(.*\)\/$/\\1/"`
        #
        # Update.
        #
        echo "${AUTO_ROOT_PATH}/home  /home  nfs  user,auto,dev,exec,suid,soft,tcp  0  0" \
            > /tmp/fstab                                2> /dev/null
        #
        grep -v "/home" /etc/fstab >> /tmp/fstab        2> /dev/null
        #
        cat /tmp/fstab > /etc/fstab                     2> /dev/null
        #
        # Mount: before the mount, unmount if there is something already,
        # like a local USB stick, mounted automatically.
        #
        umount /home 2> /dev/null
        #
        mount -t nfs -o dev,exec,suid,soft,tcp ${AUTO_ROOT_PATH}/home /home
        #
    fi
    #
    # Set the NIS domain name.
    #
    if [ "$AUTO_NIS_DOMAIN" != "" ]
    then
        #
        # Update.
        #
        echo "$AUTO_NIS_DOMAIN" >  /etc/defaultdomain
        domainname "$AUTO_NIS_DOMAIN"
        #
    fi
    #
    # Configure the NIS server.
    #
    if [ "$AUTO_NIS_SERVER" != "" ]
    then
        #
        # Update.
        #
        echo "ypserver $AUTO_NIS_SERVER" > /etc/yp.conf
        #
        # Restart.
        #
        /etc/init.d/nis stop
        /etc/init.d/nis start
        #
    fi
    #
    # Remote printer configuration.
    #
    if [ "$AUTO_LPR_SERVER" != "" ]
    then
        #
        # Update.
        #
        rm -f                                      /etc/printcap
        echo -n ""                               > /etc/printcap
        echo "lp:\\"                            >> /etc/printcap
        echo "        :sd=/var/spool/lpd/lp:\\" >> /etc/printcap
        echo "        :af=/var/log/lp-acct:\\"  >> /etc/printcap
        echo "        :lf=/var/log/lp-errs:\\"  >> /etc/printcap
        echo "        :pl#66:\\"                >> /etc/printcap
        echo "        :pw#80:\\"                >> /etc/printcap
        echo "        :pc#150:\\"               >> /etc/printcap
        echo "        :mx#0:\\"                 >> /etc/printcap
        echo "        :mc#999:\\"               >> /etc/printcap
        echo "        :rp=lp:\\"                >> /etc/printcap
        echo "        :rm=$AUTO_LPR_SERVER:\\"  >> /etc/printcap
        echo "        :sh:"                     >> /etc/printcap
        echo ""                                 >> /etc/printcap
        #
        # Must restart "/etc/init.d/lprng".
        #
        /etc/init.d/lprng restart
        #
    fi
    #
    # Configure the remote log server.
    #
    if [ "$AUTO_LOG_SERVER" != "" ]
    then
        #
        # Update.
        #
        grep -v "\*\.\*.*@.*" /etc/syslog.conf > /tmp/syslog.conf
        echo ""                                >> /tmp/syslog.conf
        echo "*.*    @$AUTO_LOG_SERVER"        >> /tmp/syslog.conf
        #
        rm -f /etc/syslog.conf
        cat /tmp/syslog.conf > /etc/syslog.conf
        #
        # Must reload syslog daemon.
        #
        /etc/init.d/sysklogd reload-or-restart
        #
    fi
    #
    # It is assumed that a remote scanner is available
    # where some other service is..
    #
    if [ -d /etc/sane.d ]
    then
        rm -f                               /etc/sane.d/net.conf
        echo -n ""                       >  /etc/sane.d/net.conf
        echo "# /etc/sane.d/net.conf"   >>  /etc/sane.d/net.conf
        echo "127.0.0.1"                >>  /etc/sane.d/net.conf
        echo "[::1]"                    >>  /etc/sane.d/net.conf
        #
        # Add some nodes as possible remote scanner server.
        #
        if [ "$AUTO_NIS_SERVER" != "" ]
        then
            echo "$AUTO_NIS_SERVER"     >>  /etc/sane.d/net.conf
        fi
        #
        if [ "$AUTO_LPR_SERVER" != "" ]
        then
            echo "$AUTO_LPR_SERVER"     >>  /etc/sane.d/net.conf
        fi
        #
        if [ "$AUTO_LOG_SERVER" != "" ]
        then
            echo "$AUTO_LOG_SERVER"     >>  /etc/sane.d/net.conf
        fi
        #
    fi
}
#
#
#
dhcp_auto_configuration
#
