#! /bin/sh
#
# Copyright (c) 2018 Red Hat.  All Rights Reserved.
# Copyright (c) 2011 Aconex.  All Rights Reserved.
# 
# 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 PostgreSQL PMDA
#

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

iam=postgresql
domain=110
python_opt=true
daemon_opt=false

conf=$PCP_PMDAS_DIR/$iam/pmdapostgresql.conf
if [ ! -f "$conf" ]; then
    echo "Error: missing config file \"$conf\""
    status=1
    exit
fi

eval `grep "^osuser=" $conf`
if [ -n "$osuser" ]; then
    echo "OS user defined in $conf is \"$osuser\""
    if ! id $osuser >/dev/null 2>&1; then
        echo "Error: no such user \"$osuser\" on this system"
        status=1
        exit
    fi

    gid=`id $osuser | sed -e 's/^.*gid=//' -e 's/(.*$//'`
    echo "group id for user \"$osuser\" is \"$gid\""

    mkdir -p "$PCP_VAR_DIR/config/pmda" 2>/dev/null
    indoms="0 1 2 3 4 5 6 7 8 9 10 11 12"
    for indom in $indoms
    do
        failed=false
        c="$PCP_VAR_DIR/config/pmda/$domain.$indom"
        if [ ! -f "$c" ]; then
	    # First time Install, initialize PMDA's indom cache file
	    #
	    if "$PCP_BINADM_DIR/indomcachectl" $domain.$indom; then
		chgrp $gid "$c"
	    else
		echo "Error: failed to create indom cache file \"$c\""
		status=1
		exit
	    fi
        fi
    done
fi

pmdaSetup
pmdaInstall
exit
