#!@SHELL@
#######################################################################
#
# xinetd LCFG Component
#
# @AUTHOR@
# Version @VERSION@ : @DATE@
#
# @MSG@
#
#######################################################################
 
@TESTVARS@ . @LCFGCOMP@/ngeneric

pidfile=/var/run/xinetd.pid
daemoninit=/etc/rc.d/init.d/xinetd
daemonname=xinetd

##########################################################################
IsDaemonRunning() {
##########################################################################

  # xinetd seems to be quite good at removing pid file
  # but I'd still rather confirm the running daemon	

  [ -f $pidfile ] && [ `cat $pidfile` -eq `pidof $daemonname` ] && return 0
  return 1;
}


##########################################################################
Configure() {
##########################################################################

  # create dirs
  mkdir -p @TMPDIR@
  [ $? != 0 ] && Fail "failed to create @TMPDIR@ (see logfile)"
  mkdir -p $LCFG_xinetd_confdir
  [ $? != 0 ] && Fail "failed to create $LCFG_xinetd_confdir (see logfile)"

  # empty tmp dir and destination dir
  /bin/rm -f @TMPDIR@/*
  /bin/rm -f $LCFG_xinetd_confdir/*

  # Use sxprof to substitute the configuration parameters from the
  # environment into the template.
  @LCFGBIN@/sxprof -i $_COMP @TEMPLATE@ \
                             @CONFIGFILE@

  # Was anything changed? Or did the substitution fail?
  status=$?; [ $status = 2 ] && LogMessage "configuration changed"
  [ $status = 1 ] && Fail "failed to create config file (see logfile)"


  # now we want to see which services need to be enabled and either
  # copy across files from /etc/xinetd.d to /etc/xinetd.lcfg or create
  # new files from resources, we'll use a tmp dir to work in

  # first, see if any services are enabled
  if [ -n "$LCFG_xinetd_enableservices" ]; then
     LogMessage "Enabling services: $LCFG_xinetd_enableservices"
     for service in $LCFG_xinetd_enableservices; do

	 # if there's a file in xinetd.d, copy that (remove disable line)
	 if [ -f $LCFG_xinetd_basedir/$service ]; then
	    LogMessage "Copying file $LCFG_xinetd_basedir/$service"
	    egrep -v '^[[:space:]]disable' \
		     $LCFG_xinetd_basedir/$service \
		     > @TMPDIR@/_$service.CONF
         else
	    LogMessage "No file for $service found in $LCFG_xinetd_basedir"
	 fi

     done

     # we can use sxprof to make temporary config files from resources
     for serviceres in $LCFG_xinetd_services; do

	# only process resources if the service is enabled
	if echo $LCFG_xinetd_enableservices | grep "\<$serviceres\>" > /dev/null; then

	    LogMessage "Using LCFG resources for $serviceres"
	    eval /bin/sed -e 's/SERVICENAME/$serviceres/g' @TEMPLATE2@ \
			 > @TMPDIR@/_template
	    @LCFGBIN@/sxprof -i $_COMP @TMPDIR@/_template \
			 > @TMPDIR@/_$serviceres.RES
	    /bin/rm -f @TMPDIR@/_template

	fi

     done

     # at this stage we should have a TMPDIR filled with service.CONF
     # (from /etc/xinetd.d/) and service.RES (from resources), now we
     # have to copy and/or merge them
     for service in $LCFG_xinetd_enableservices; do
	 if [ ! -f @TMPDIR@/_$service.CONF ] && \
		      [ ! -f @TMPDIR@/_$service.RES ]; then
	     Warn "No service definition for $service"
	 elif [ -f @TMPDIR@/_$service.CONF ] && \
		      [ ! -f @TMPDIR@/_$service.RES ]; then
	     /bin/cp @TMPDIR@/_$service.CONF "$LCFG_xinetd_confdir/$service"
	 elif [ ! -f @TMPDIR@/_$service.CONF ] && \
		      [ -f @TMPDIR@/_$service.RES ]; then
	     /bin/cp @TMPDIR@/_$service.RES "$LCFG_xinetd_confdir/$service"
	 elif [ -f @TMPDIR@/_$service.CONF ] && \
		      [ -f @TMPDIR@/_$service.RES ]; then
	     # we have both, merge them
	     # I could work at doing this in shell for the rest of my life, so 
	     # here's a big horrible line of perl instead     
	     /usr/bin/perl -e 'my $echoing=1;my $processing=0;my %vals;
		      while (<>) {
			  if ($_ =~ /^\s*\{\s+$/)
			     { print $_ if $echoing; $echoing=0; $processing=1; }
			  if ($_ =~ /^\s*\}\s+$/) { $processing=0; next; }
			  if ($processing) {
			      chomp;
			      if ($_ =~ /^\s*(\w+)\s*((?:\+|-)*=)\s*(.*)$/)
				 { $vals{"$1"}="$2 $3\n"; }
			  }
			  else { print $_ if $echoing; }
		      }
		      foreach (keys %vals) { print "\t$_ $vals{$_}"; }
		      print "}\n";' \
		      @TMPDIR@/_$service.CONF @TMPDIR@/_$service.RES \
		      > "$LCFG_xinetd_confdir/$service"
	 fi
     done


     if IsDaemonRunning; then	 
	$daemoninit reload && LogMessage "Ran $daemoninit reload"
     else
	$daemoninit start && LogMessage "Ran $daemoninit start"
     fi

  else
     LogMessage "no services enabled"
     if IsDaemonRunning; then
        $daemoninit stop && LogMessage "Ran $daemoninit stop"
     fi
  fi

}

##########################################################################
Start() {
##########################################################################

  # don't do anything here as Configure() method decides whether
  # or not to start daemon
  return;
}

##########################################################################
Stop() {
##########################################################################

  if IsDaemonRunning; then
     $daemoninit stop && LogMessage "Ran $daemoninit stop"
  else
     LogMessage "No daemon running"
  fi
  return;
}

##########################################################################
# Dispatch methods
##########################################################################

Dispatch "$@"
