#!/bin/sh
#
# Copyright (c) 1997-2001 Silicon Graphics, Inc.  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 hotproc PMDA and/or PMNS
#
# XXX these values are overwritten by pmdaproc.sh!
tmp=`mktemp -d /tmp/pcp.XXXXXXXXX` || exit 1
status=1	# failure is the default!
trap "cd /; rm -rf $tmp; exit \$status" 0 1 2 3 15

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

iam=hotproc
pmda_interface=2
forced_restart=false

pmdaSetup

daemon_opt=true		# can install as daemon
dso_opt=false
pipe_opt=true		# supports pipe IPC
socket_opt=false	# no socket IPC

# be careful that mortals cannot write any configuration files, as
# these would present a security problem
#
umask 022

# PMDA variables
#
configfile=""
refresh=60
debug=0
storable="n"

do_debug=false

_parsedefaults()
{
    echo "Extracting options from current installation ..."
    while getopts D:d:h:i:l:p:st:u: c
    do
    	case $c in
	    \?)		echo "Warning: Unrecognized option in $PCP_PMCDCONF_PATH"
			echo "	       Remove line for the $iam PMDA in $PCP_PMCDCONF_PATH and re-run ./Install"
	    		exit 2;;
	    D )		debug=$OPTARG;;
	    t )		refresh=$OPTARG;;
	    s )		storable="n";;
	    * )		;;
	esac
    done
    eval configfile='$'$OPTIND
}

if $do_pmda
then

    # set options from $PCP_PMCDCONF_PATH, if possible
    #
    ans=`$PCP_AWK_PROG <$PCP_PMCDCONF_PATH '
$1 == "'$iam'"	{ printf "%s",$6
		  for (i=7;i<=NF;i++) printf " %s",$i
		  print ""
	        }'`
    if [ ! -z "$ans" ]
    then
	_parsedefaults $ans
    fi

    # go figure out which configuration file to use ...
    #
    default_configfile=./sample.conf
    pmdaChooseConfigFile
    if [ -z "$configfile" ]
    then
	echo ""
	echo "Error: Abandoning installation as no configuration file was specified."
	exit 1
    fi

    # make sure that the chosen configuration file parses ok
    #    
    $pmda_dir/$pmda_name -C $configfile >$tmp/err 2>&1
    if [ $? -eq 1 ]
    then
	echo ""
	echo "Error: Abandoning installation due to errors in configuration file:"
	cat $tmp/err
	exit 1
    fi

    echo
    echo "Enter the refresh interval (in seconds) [$refresh] \c"
    read ans
    if  [ ! -z "$ans" ]
    then
    	refresh=$ans
    fi

    echo
    echo "Do you want to modify the configuration predicate or refresh interval"
    echo "at run time [$storable]? \c"
    read ans
    if [ ! -z "$ans" ]
    then
	case $ans in
	    N|n|NO|No|no) storable=n;;
	    Y|y|YES|Yes|yes) storable=y;;
	esac
    fi
    if [ $storable = y ]
    then
	store_opt=""
    else
	store_opt="-s"
    fi

    if [ "$do_debug" = true ]
    then
	echo
	echo "Enter the debugging flag (see pmdbg(1)) [$debug] \c"
	read ans
	if [ ! -z "$ans" ]
	then
	    debug=$ans
	fi
    fi

    args="$store_opt -t $refresh -D $debug $configfile"
fi

pmdaInstall
echo "Please note that instance related metrics will not be available"
echo "until after the initial refresh of $refresh seconds."

status=0
exit 0
