#!/bin/bash
# verify the DHCPD configuration file  (/etc/dhcpd.conf)
#
# Usage:
#    verifyDhcpdConf  < true | false | [empty] >
#
# Description:
#    Make sure DHCPD_CONF_FILE exists if we need it
#    Call this script with the parm letting it know if we need the conf file
#    If we need it and it exists, do nothing, but if it is needed and doesn't
#    exist yet, copy the one from our shipped sample (/$HWMCAHOME/data/network/dhcpd.conf).
#    If it isn't needed,but exists already, erase it.
#    If no parameter is specified, verify the validity of the existing dhcpd.conf file, if it exists
#
# Return Codes
#    0: Conf file not needed; doesn't exist; do nothing
#    1: Conf file needed; it already exists
#    2: Conf file needed; we copied it ok
#    3: Conf file no longer needed; we erased it ok
#  100: An error occurred
#
#
# Module History
#    00  11/11/2003  T. Forties  - Initial Release
#    01  02/16/2004  TF  - Copy sample config file correctly for P-series
#        02/16/2004  TF  - touch dhcpd.leases before starting dhcpd - Needed 1st time; won't hurt anytime
#    02  09/22/2004  TF  - Make sure the dhcpd.conf file found in /etc/dhcpd.conf is an IBM supplied version
#    03  10/15/2004  TF  - Added validity only check - needed for non-dhcpd server case (bad dhcpd.conf file supplied)
#    04  03/22/2006  TF  - Added touch of dhcpd.leases in new directory - MCP change

dhcp_conf_needed=$1
system_conf_file="/etc/dhcpd.conf"
z_sample_conf_file="$HWMCAHOME/data/network/dhcpd.conf"
p1_sample_conf_file="$CONSOLE_PATH/data/network/dhcpd.conf"
p2_sample_conf_file="/opt/ccfw/data/network/dhcpd.conf"
unique_id="Licensed Internal Code - Property of IBM"

echo 'dhcp_conf_needed ='"$dhcp_conf_needed"
rc=100


# subroutine for checking if there were errors while collecting data
check_err() {
   if [ -s err.tmp ]; then
      echo "errors follow."
      cat -n err.tmp
      rc=100
   else
      echo "done."
   fi
   rm -f err.tmp
   echo ""
}

# Was no parameter supplied?  (If not, it is determineNetSet calling - just verify if file IBM supplied)
if  [ $# -eq 0  ]; then
   echo 'Only check validity of conf file (if it exists)'
   # If the file exists and contains our unique IBM identifier
   if [ -e $system_conf_file ]; then
      echo 'System conf file exists'
      if grep "$unique_id" $system_conf_file >/dev/null 2>/dev/null ; then
         echo "An IBM supplied system conf file ($system_conf_file) exists"
         rc=1
      else
         echo 'System conf file exists - NOT IBM supplied'
         rm  "$system_conf_file" 2>err.tmp
         rc=3
         check_err
         echo 'System dhcpd config file erased'
      fi
   else
      echo 'System conf file does not exist - Exit OK'
      rc=0
   fi

else
   #
   if  "$dhcp_conf_needed"
   then
      echo 'DHCPD Conf file is needed'
      echo 'touch /var/lib/dhcp/dhcpd.leases'
      touch /var/lib/dhcp/dhcpd.leases

      # MCP4 now looks for leases file in a different directory
      mkdir /var/lib/dhcp/db >/dev/null 2>/dev/null
      touch /var/lib/dhcp/db/dhcpd.leases

   # If the file exists and contains our unique IBM identifier
      if grep "$unique_id" $system_conf_file >/dev/null 2>/dev/null ; then
         echo "An IBM supplied system conf file ($system_conf_file) exists"
         rc=1
      else
         if [ -e $system_conf_file ]; then
           echo 'System conf file exists, but it was not IBM supplied - overlay it'
         else
           echo 'System conf file does NOT exist'
         fi
         if [ -e $p1_sample_conf_file ]; then
            echo 'Use P-series sample conf file(1)'
            cp "$p1_sample_conf_file" "$system_conf_file" 2>err.tmp
            echo 'cp '"$p1_sample_conf_file"' '"$system_conf_file"
         else
            if [ -e $p2_sample_conf_file ]; then
               echo 'Use P-series sample conf file(2)'
               cp "$p2_sample_conf_file" "$system_conf_file" 2>err.tmp
               echo 'cp '"$p2_sample_conf_file"' '"$system_conf_file"
            else
               echo 'Use Z-series sample conf file'
               cp "$z_sample_conf_file" "$system_conf_file" 2>err.tmp
               echo 'cp '"$z_sample_conf_file"' ' "$system_conf_file"
            fi
         fi
         rc=2
         check_err
         echo 'Sample config file copied to System config file'

      fi

   else
      echo 'DHCPD Conf file is NOT needed'
      if [ -e $system_conf_file ]; then
         echo 'System conf file exists - no longer needed'
         rm  "$system_conf_file" 2>err.tmp
         rc=3
         check_err
         echo 'System dhcpd config file erased'

      else
         echo 'System conf file does NOT exist - as expected'
         rc=0
      fi

   fi
fi

echo 'Return code = '"$rc"
exit "$rc"

