#!/bin/sh
#

OUT=`tempfile`
DEST=/var/cache/nagios/plugins-auto.cfg

cat > $OUT <<EOT
# This file was autogenerated on `date` and
# should not be manually altered. Use update-nagios to
# build this file. If you need to add a command specification, edit
# the files in /etc/nagios-plugins/config/
# or put a new command in /etc/nagios/checkcommands.cfg
#
# NOTE: Changes here are overwritten with the next update-nagios run!

EOT

FOUND_PLUGINS=false

# Automatically convert netsaint plugin configuration files to a format
# understood by nagios.  Assumes the incoming file contains lines like:
#
# command[ssh_disk]=/usr/lib/nagios/plugins/check_by_ssh -H $HOSTADDRESS$ -C '/usr/lib/nagios/plugins/check_disk -w 95% -c 85% -p $ARG1$'
#
for f in /etc/nagios-plugins/config/*.cfg ; do
  if [ -f $f ] ; then # to avoid getting * literally if no match
    echo "# Included from $f" >>$OUT

    cat $f >>$OUT
    echo >>$OUT
    FOUND_PLUGINS=true
  fi
done

mv -f $OUT $DEST
chown nagios:nagios $DEST

if $FOUND_PLUGINS; then
	if [ -x /etc/init.d/nagios -a "$1" != "--no-reload" ] ; then	
		/etc/init.d/nagios reload
    fi
else
    echo "Created an empty plugins-auto.cfg file, because no plugin configs"
    echo "found in /etc/nagios-plugins/config. Won't reload nagios."
    exit 0
fi
