#!/bin/sh
#%# family=poker-network
#%# capabilities=autoconf suggest
#
# Copyright (C) 2007, 2009 Loic Dachary <loic@dachary.org>
#
#
# This software's license gives you freedom; you can copy, convey,
# propagate, redistribute and/or modify this program under the terms of
# the GNU Affero General Public License (AGPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the AGPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file in the toplevel directory called
# "AGPLv3".  If not, see <http://www.gnu.org/licenses/>.
#

set -e

: ${IP:=$(basename $0 | sed 's/^poker_players_//g')}
: ${DEBCONF:=/var/cache/debconf/config.dat}
: ${WGET:=wget}

if [ "$1" = "autoconf" ]; then
   echo yes
   exit 0
fi

if [ "$1" = "suggest" ]; then
   test -f ${DEBCONF} || exit 0
   perl -ne 'if(m|^Name: python-poker-network/www| .. m|^$|) { print $1 if(/Value: (.*)/); }' < ${DEBCONF}
   exit 0
fi

if [ "$1" = "config" ]; then
   echo "graph_title poker players"
   echo "graph_vlabel poker players"
   echo "graph_category poker"
   echo "players.label players"
   echo "players.type GAUGE"
   exit 0
fi

script=$(cat <<'EOF'
$obj = jsonToObj($_);
print "players.value $obj->[0]{'players'}\n";
EOF
)
${WGET} -q -O - --post-data '{"type": "PacketPokerStatsQuery"}' http://$IP:19384/POKER_REST?session=no | perl -MJSON -ne "$script"
