#!/bin/sh
#
# Plugin to monitor the number of installed Spip instances
#
# Parameters:
# 	
# 	config
# 	autoconf
#
#
#%# family=contrib
#%# capabilities=autoconf

CACHEFILE="/var/local/cache/cms-versions-installed/spip.txt"

if [ "$1" = "autoconf" ]; then
	if [ -f $CACHEFILE ]; then
		echo yes
		exit 0
	else
		echo "no (could not find cache file)"
	fi
        exit 1
fi

if [ "$1" = "config" ]; then
	echo 'graph_title Spip - number of installations'
	echo 'graph_vlabel quantity'
	echo 'graph_category cms'
	# echo 'graph_scale no'
	# echo 'graph_args --base 1024 -l 0'

	cat $CACHEFILE \
		| awk '{print $2}' \
		| sed 's/\./v/g'  \
		| awk ' {foo[$1]++} END { for (f in foo ) { print "v" f ".label", f }}' \
		| sed 's/ \([0-9]*\)v\([0-9]*\)v\([0-9a-zA-Z]*\)/ \1.\2.\3/' \
		| sed 's/ \([0-9]*\)v\([0-9]*\)/ \1.\2/' \
		| sed 's/v.label /unknown.label unknown/'
	exit 0
fi

cat $CACHEFILE |  awk '{print $2}' | sed 's/\./v/g'  | awk ' {foo[$1]++} END { for (f in foo ) { print "v" f ".value", foo[f] }}' | sed 's/v.value /unknown.value /'
