#!/bin/sh
################################################################################
# $Id: config_services 375 2006-12-15 23:15:36Z makia $
################################################################################
#  Copyright (C) 2004 The Regents of the University of California.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Makia Minich <makia@llnl.gov>
#  UCRL-CODE-155916
#  
#  This file is part of GeDI, a diskless image management tool.
#  For details, see <http://www.llnl.gov/linux/gedi/>.
#  
#  GeDI is free software; you can redistribute it and/or modify it under
#  the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
#  
#  GeDI is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
#  details.
#  
#  You should have received a copy of the GNU General Public License along
#  with GeDI; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
################################################################################
#
# Borrowed initially from makedhcp created by Egan Ford (egan@us.ibm.com),
# this will convert information in the MAC.info file into valid dhcpd.conf.

# Source function library.
. /etc/gedi/variables

umask 133

. ${LIBDIR}/modify_dhcp
. ${LIBDIR}/modify_other

RESTART_DHCP=0

if [ ! -r "$MACINFO" ] ; then
   echo "$MACINFO must be available.  (Try running MAC_Collector first.)"
   exit 1
fi

delete_node()
{
   host=$1
   group=$(grep "host $host {" ${DHCP} | sed 's/.*group=//')

   echo "Removing entry for $host ($group): "

   delete_dhcp_entry $host
   delete_dhcp_pool $group
   delete_dhcp_group $group
   [ "$MODIFY_EXPORTS" = "YES" ] && del_host_from_exports $host
   [ "$MODIFY_HOSTS_FILE" = "YES" ] && del_host_from_hosts $host

   RESTART_DHCP=1
}

add_node()
{
   mac=$1
   host=$2
   ip=$3
   group=$4

   add_dhcp_entry $mac $host $ip $group
   [ "$MODIFY_EXPORTS" = "YES" ] &&
      add_host_to_exports "$IMAGE/$group" $host $ip
   [ "$MODIFY_HOSTS_FILE" = "YES" ] &&
      add_host_to_hosts $host $ip

   RESTART_DHCP=1
}

echo "Configuring Services (as needed):"

write_global_options
[ "$MODIFY_TFTP" = "YES" ] && fix_tftp

echo "0) Checking for entries in DHCP that should be removed."
for dhcp_hosts in $(grep 'host ' $DHCP | awk '{print $2}') ; do
   [ -z "$dhcp_hosts" ] && continue

   ( ! cat $MACINFO | grep -v "^#" | 
       grep -q "[[:space:]]${dhcp_hosts}[[:space:]]" ) &&
      delete_node $dhcp_hosts
done
for range in $(grep 'range ' $DHCP | \
               awk '{sub(/;/, ""); sub(/pool=/, ""); print $2"-"$3"_"$NF}') ; do
   [ -z "$range" ] && continue

   group=${range/*_/}
   range=${range/_*/}

   ( ! cat $MACINFO | grep -v "^#" |
       grep -q "^$range[[:space:]]$group" ) &&
      ( delete_dhcp_range $range || RESTART_DHCP=1 )
done
for dgroup in $(grep "} # end pool" $DHCP | awk '{print $NF}') ; do
   delete_dhcp_pool $dgroup
done
for dgroup in $(grep "} # end group" $DHCP | awk '{print $NF}') ; do
   delete_dhcp_group $dgroup
done

# Next, we'll make sure everything is kosher.
echo "1) Remove nodes as needed."
exec 3< $MACINFO
while read nodeline <&3 ; do
   [ -z "$nodeline" ] && continue
   ( echo "$nodeline" | grep -qv "^#" ) || continue
   ( echo ${nodeline} | awk '{print $1}' | grep -q ':' ) || continue

   for rem in $(check_entry_exists $nodeline) ; do
      delete_node $rem && RESTART_DHCP=1
   done
done
exec 3>&-

echo "2) Add nodes as needed."
exec 3< $MACINFO
while read nodeline <&3 ; do
   [ -z "$nodeline" ] && continue
   ( echo "$nodeline" | grep -qv "^#" ) || continue

   if ( $(echo ${nodeline} | awk '{print $1}' | grep -q ':') ) ; then
      host=$(echo $nodeline | awk '{print $2}')

      if ( ! grep -q "host $host " $DHCP ) ; then
         echo "* Adding node ${host}:"
         add_node $nodeline || RESTART_DHCP=1
      fi
   elif ( $(echo ${nodeline} | awk '{print $1}' | grep -q '-') ) ; then
      range=$(echo $nodeline | awk '{print $1}')
      add_dhcp_range $nodeline && RESTART_DHCP=1
      echo "Exports and hosts file will not be modified due to use of range."
   fi
done
exec 3>&-

[ "$RESTART_DHCP" = 1 ] && (
   echo -n "Restarting DHCPD:"
   service dhcpd restart > /dev/null 2>&1 && echo_success || echo_failure
)

echo "Done."
