#!/usr/bin/ksh -p
#
# ident "@(#)utdhcpservice.ksh	1.11 05/04/05 SMI"
#
# Copyright 2004,2005 Sun Microsystems, Inc.  All rights reserved.
#

#
# Part of the Sun Ray server implementation to interface to DHCP
# (Dynamic Host Configuration Protocol) on Linux based on the
# Internet Systems Consortium, Inc. (ISC) implementation.
#
# Options:
#	enable	- enable the DHCP service
#	disable	- disable the DHCP service
#	start	- start the DHCP daemon
#	stop	- stop the DHCP daemon
#	restart	- restart the DHCP daemon
#	status	- (undocumented) list current DHCP status
#
# allows undocumented status option in case this gets included
# in the activation framework yet to be defined
#
#
# Return values:
#
#	0	- Successful completion
#	1	- Failed
#	2	- DHCP not installed
#	3	- DHCP not enabled
#		  (or already enabled if the option is enable)
#

export LC_ALL=C

export PATH=/bin:/usr/bin:/sbin:/usr/sbin

UT_ETC="/etc/opt/SUNWut";
UT_BASEDIR=`(cd /etc/opt/SUNWut/basedir ; /bin/pwd)`
UT_DHCP_BASEDIR=`(cd ${UT_ETC}/dhcp ; /bin/pwd)`

PROGRAM_ID=$(basename $0)

#
# get literal ${SUBNETFILEPATHPREFIX} to test for configured state
#
. ${UT_DHCP_BASEDIR}/dhcp_config_linux

# names of files and directories that will be touched by this script
#
DHCP_CONFIG="/etc/dhcpd.conf";
DHCP_DIR="/var/lib/dhcp";
TMPDIR="/var/opt/SUNWut/tmp"
CORONA_NAME="SunRay";
CORONA_TITLE="Sun Ray";

SUNRAY_FILE_MARK="${CORONA_TITLE} default DHCP config file"

#
# These booleans keep track of:
#	if the DHCP package is installed
#	if the DHCP package is enabled
#	if the DHCP configuration includes a Sun Ray subnet / inteface
#	if the DHCP daemon was running at the time this script was invoked.
#
typeset DHCP_INSTALLED=true
typeset DHCP_ENABLED=true
typeset DHCP_CONFIGURED=true
typeset DHCP_RUNNING=true
typeset DHCP_PACKAGE_RH="dhcp"
typeset DHCP_PACKAGE_SU="dhcp-server"
typeset DHCP_PACKAGE="not installed"
typeset DHCP_STATE="online"

#
# Usage message
#
function Usage {

print "
Usage:
$PROGRAM_ID [ enable | disable ]
$PROGRAM_ID [ start | stop | restart ]
"

exit 1
}

function StatusDHCP {
    typeset PACKAGE="${DHCP_PACKAGE}"
    if $DHCP_INSTALLED ; then
      # get the actual package name including version and release
      PACKAGE="$(rpm -q ${DHCP_PACKAGE} 2> /dev/null)"
    fi
    if ! $DHCP_RUNNING && [ $DHCP_STATE != "unconfigured" ] ; then
      DHCP_STATE="disabled"
    fi
    print "begin dhcpstate"
    print "installed=${DHCP_INSTALLED}"
    print "enabled=${DHCP_ENABLED}"
    print "configured=${DHCP_CONFIGURED}"
    print "running=${DHCP_RUNNING}"
    print "packages=${PACKAGE}"
    print "status=${DHCP_STATE}"
    print "end"
}

function EnableDHCP {
    if $DHCP_ENABLED ; then
      return 3
    fi

    if [[ -f ${DHCP_CONFIG} ]]; then
	dhcpd -t > /dev/null 2>&1
	if [[ $? == 0 ]]; then
		cat > ${DHCP_CONFIG}.$$ <<-!
# ${SUNRAY_FILE_MARK} /etc/dhcpd.conf
!
		cat ${DHCP_CONFIG} >> ${DHCP_CONFIG}.$$
		rm ${DHCP_CONFIG}
		mv ${DHCP_CONFIG}.$$ ${DHCP_CONFIG}	
		chkconfig --add dhcpd >/dev/null 2>&1
		# Hack for Red Hat to create Start link in /etc/rc.d/rc5.d
		chkconfig --level 5 dhcpd on >/dev/null 2>&1
		return 0
	else
		mv ${DHCP_CONFIG} ${DHCP_CONFIG}.sunray
	fi
    fi
	cat > ${DHCP_CONFIG} <<-!
# ${SUNRAY_FILE_MARK} /etc/dhcpd.conf

ddns-update-style ad-hoc;
!
    # allow leases to be assigned
    touch ${DHCP_DIR}/dhcpd.leases
    chkconfig --add dhcpd >/dev/null 2>&1
    # Hack for Red Hat to create Start link in /etc/rc.d/rc5.d
    chkconfig --level 5 dhcpd on >/dev/null 2>&1

    return 0
}

function DisableDHCP {
    if ! $DHCP_ENABLED ; then
      return 3
    fi

    if $DHCP_CONFIGURED || $DHCP_RUNNING ; then
      return 1
    fi

    #
    # is the config file unique to Sun Ray or has the user modified it?
    grep "${SUNRAY_FILE_MARK}" ${DHCP_CONFIG} 2>/dev/null 1>&2
    if [[ $? != 0 ]] ; then
      return 1
    fi

    cat > ${DHCP_CONFIG}.$$ <<-!
# ${SUNRAY_FILE_MARK} /etc/dhcpd.conf

ddns-update-style ad-hoc;
!
    diff ${DHCP_CONFIG} ${DHCP_CONFIG}.$$ 2>/dev/null 1>&2
    if [[ $? == 0 ]]; then
	rm -f ${DHCP_CONFIG}
	if [[ -f ${DHCP_CONFIG}.sunray ]]; then
	    mv ${DHCP_CONFIG}.sunray ${DHCP_CONFIG}
	fi
	chkconfig --del dhcpd >/dev/null 2>&1
	rm -f ${DHCP_CONFIG}.$$
	rm -f ${DHCP_DIR}/dhcpd.leases
	return 0
    fi
	
    sed -e "/.*$SUNRAY_FILE_MARK.*/d" ${DHCPDCONF} > ${DHCPDCONF}.$$	
    rm -f ${DHCPDCONF}
    mv ${DHCPDCONF}.$$ ${DHCPDCONF}
    return 0
}

function StartDHCP {
    if ! $DHCP_ENABLED ; then
      return 3
    fi

    /etc/init.d/dhcpd start > /dev/null 2>&1
    return $?
}

function StopDHCP {
    if ! $DHCP_ENABLED ; then
      return 3
    fi

    /etc/init.d/dhcpd stop > /dev/null 2>&1
    return $?
}

function RestartDHCP {
    if ! $DHCP_ENABLED ; then
      return 3
    fi

    /etc/init.d/dhcpd restart > /dev/null 2>&1
    return $?
}

#
# main
#
${UT_BASEDIR}/lib/utprodinfo -t installed ${DHCP_PACKAGE_RH} 2>/dev/null 1>&2
if [[ $? -ne 0 ]] ; then
  ${UT_BASEDIR}/lib/utprodinfo -t installed ${DHCP_PACKAGE_SU} 2>/dev/null 1>&2
  if [[ $? -ne 0 ]]; then
    DHCP_INSTALLED=false
    DHCP_ENABLED=false
    DHCP_CONFIGURED=false
    DHCP_RUNNING=false
    DHCP_STATE="uninstalled"
  else
    DHCP_PACKAGE=${DHCP_PACKAGE_SU}
  fi
else
  DHCP_PACKAGE=${DHCP_PACKAGE_RH}
fi

if $DHCP_INSTALLED; then
  if [[ ! -f ${DHCP_CONFIG} ]] ; then
    DHCP_ENABLED=false
    DHCP_CONFIGURED=false
    DHCP_RUNNING=false
    DHCP_STATE="unconfigured"
  else
    grep "Sun Ray" ${DHCP_CONFIG} 2>/dev/null 1>&2
    if [[ $? -ne 0 ]]; then
	DHCP_ENABLED=false
    	DHCP_CONFIGURED=false
    	DHCP_RUNNING=false
    else
    	egrep "${SUBNETFILEPREFIX}|${INTFILEPREFIX}" ${DHCP_CONFIG} > /dev/null 2>&1
    	if [[ $? != 0 ]] ; then
      	    DHCP_CONFIGURED=false	# Interface / subnet not defined
    	fi

    	/etc/init.d/dhcpd status 2> /dev/null | grep "running" >/dev/null 2>&1
    	if [[ $? -ne 0 ]]; then
      	    DHCP_RUNNING=false
    	fi
    fi
  fi
fi

if [[ $# -gt 1 ]] ; then
  Usage
fi

#
# allow undocumented status option for now in case this gets included
# in the activation framework yet to be defined
#
if [[ $# -eq 0 || "$1" = "status" ]] ; then
  StatusDHCP
  exit 0
fi

if ! $DHCP_INSTALLED ; then
  exit 2
fi

case $1 in
"enable")
  EnableDHCP ;;
"disable")
  DisableDHCP ;;
"start")
  StartDHCP ;;
"stop")
  StopDHCP ;;
"restart")
  RestartDHCP ;;
*)
  Usage ;;
esac

return $?
