#!/bin/sh

: <<=cut

=head1 NAME

pacman_unrequired_packages - Plugin to monitor packages no longer required.

=head1 APPLICABLE SYSTEMS

All systems with pacman as their package manager.

=head1 CONFIGURATION

The plugin needs no additional configuration and works out of the box.

=head1 INTERPRETATION

This plugin will draw one block: the number of packages no longer required.

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 VERSION

  1.0.0

=head1 AUTHOR

Olivier Mehani <shtrom+munin@ssji.net>
Bert Peters <bert@bertptrs.nl> (pacman_pending_updates)

=head1 LICENSE

GPLv2

=cut

case $1 in
	config)
		cat <<'EOM'
graph_args --base 1000 -l 0
graph_title Unrequired packages
graph_vlabel packages
graph_category security
unrequired.label unrequired
unrequired.info Current number of unrequired packages. They can be removed with `pacman -Rsn $(pacman -Qdtq).` 
unrequired.draw AREA
EOM
		;;

	autoconf)
		if hash pacman >/dev/null 2>&1; then
			echo yes
		else
			echo "no (pacman not found)"
		fi
		;;

	*)
		unrequired="$(pacman -Qdtq)"
		exitcode=$?
		if [ "$exitcode" = 0 ]; then
			if [ -n "$unrequired" ]; then
				echo "unrequired.value $(echo "$unrequired" | wc -l)"
				echo "unrequired.extinfo $(echo "$unrequired" | paste -s -d,)"
			else
				echo "unrequired.value 0"
			fi
		elif [ "$exitcode" = 1 ]; then
			# "pacman" returns the exitcode 1 if no matching package is found
			echo "unrequired.value 0"
		else
			echo "unrequired.value U"
		fi
		;;
esac

exit 0
