#! /bin/sh
#
# Copyright (c) 2017 Red Hat.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# Install the HAProxy PMDA
#

. $PCP_DIR/etc/pcp.env
. $PCP_SHARE_DIR/lib/pmdaproc.sh

iam=haproxy
domain=145
python_opt=true
daemon_opt=false

# Get user and connection definitions
eval `$PCP_PYTHON_PROG $PCP_PMDAS_DIR/haproxy/connect --config`

if [ -z "$user" ]; then
    echo "Failed to get user configuration, sorry this is fatal"
    status=1
    exit
fi

if [ -z "$socket" -a -z "$url" ]; then
    echo "Failed to get connection configuration, sorry this is fatal"
    status=1
    exit
fi

if [ -z "$url" -a ! -S "$socket" ]; then
    echo "Warning: $socket: socket does not exist"
fi

id -g $user >/dev/null 2>&1
if test $? -ne 0; then
    echo "Cannot change user to $user, sorry this is fatal"
    status=1
    exit
fi

mkdir -p "$PCP_VAR_DIR/config/pmda" 2>/dev/null
indoms="0"
for indom in $indoms
do
    if [ ! -f "$PCP_VAR_DIR/config/pmda/$domain.$indom" ]
    then
	# First time Install, initialize PMDA's indom cache file
	#
	if "$PCP_BINADM_DIR/indomcachectl" -u $user $domain.$indom
	then
	    :
	else
	    echo "Cannot create indom cache files for user $user"
	    echo "Failed on: $PCP_VAR_DIR/config/pmda/$domain.$indom"
	    status=1
	    exit
	fi
    fi
done

pmdaSetup
pmdaInstall
exit
