#! /bin/sh
#
# Copyright (c) 2016 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 libvirt PMDA
#

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

iam=libvirt
domain=140
python_opt=true
daemon_opt=false

# qa/1041 is failing on bozo because we probe the metrics too
# quickly ... this is probably a generic issue, so add a little
# delay here
#
check_delay=5

for module in libvirt lxml; do
    $PCP_PYTHON_PROG -c "import $module" 2>/dev/null
    if test $? -ne 0; then
        echo "$PCP_PYTHON_PROG module $module not found"
        status=1
        exit
    fi
done

# Get user and uri definitions
eval `$PCP_PYTHON_PROG $PCP_PMDAS_DIR/libvirt/connect --config`

if [ -z "$user" ]; then
    echo "Failed to get configuration, sorry this is fatal"
    status=1
    exit
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
