#!/bin/sh

: << =cut

=head1 NAME

external_ - Plugin that enables delegation of config and fetch

=head1 CONFIGURATION

[external_*]
# "config" is the file containing the output of "PLUGIN config"
# default is /dev/null
env.config /var/run/my_plugin.config

# "fetch" is the file containing the output of "PLUGIN"
# default is /dev/null
env.fetch /var/run/my_plugin.fetch

# What should be done after a "config" or "fetch" read.
# Possible options are :
# "nothing" : does nothing, this is the default
# "unlink" : unlink() the file just after read.
#            Useful if the external plugin is launched
#            regularly, via cron for example.
# "truncate" : truncate() the file just after read.
#              Useful if you have a long-running process
#              that keeps the file open.
# on_config nothing
# on_fetch truncate
env.on_fetch unlink

=head1 AUTHOR

Steve Schnepp

=head1 LICENSE

GPLv2

=cut


# shellcheck disable=SC1090
. "$MUNIN_LIBDIR/plugins/plugin.sh"

config=${config:-/dev/null}
fetch=${fetch:-/dev/null}
on_config=${on_config:-nothing}
on_fetch=${on_fetch:-nothing}


if [ "$1" = "config" ]; then
	cat "$config"

	# Handling HOOK
	case "$on_config" in
		unlink)
			rm -f "$config"
			;;
		truncate)
			true >"$config"
			;;
		*)
			# Do nothing
			;;
	esac
	exit 0
fi

cat "$fetch"

# Handling HOOK
case "$on_fetch" in
	unlink)
		rm -f "$fetch"
		;;
	truncate)
		true >"$fetch"
		;;
	*)
		# Do nothing
		;;
esac

exit 0
