#! /bin/sh
#
# Copyright 1997,2003 Silicon Graphics, Inc.
# ALL RIGHTS RESERVED
# 
# UNPUBLISHED -- Rights reserved under the copyright laws of the United
# States.   Use of a copyright notice is precautionary only and does not
# imply publication or disclosure.
# 
# U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
# in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
# in similar or successor clauses in the FAR, or the DOD or NASA FAR
# Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
# 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
# 
# THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
# INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
# DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
# PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
# GRAPHICS, INC.
#
# Install the Trace PMDA and/or PMNS
#
# $Id: Install,v 1.22 2000/08/24 08:38:04 max Exp $
#

# Get standard environment
. /etc/pcp.env

# Get the common procedures and variable assignments
#
. $PCP_SHARE_DIR/lib/pmdaproc.sh

# The name of the PMDA
#
iam=trace

# Override interactive dialog from pmdaSetup in pmdaproc.sh
#
__choose_mode()
{
    echo "Installing the \"$iam\" Performance Metrics Domain Agent (PMDA) ..."
    echo
}

# Using libpcp_pmda.so.2 and PMDA_INTERFACE_2
#
pmda_interface=2

numeric=0

getnumeric()
{
    if [ "X$2" = "X" ]
    then
	numeric=0
    elif [ "X`expr 0 + $2 2>/dev/null`" != "X$2" ]
    then
	echo "-- Sorry, $1 must be numeric (not $2) --"
	return 1
    else
	numeric=$2
    fi
    return 0
}

getunits()
{
    metric_name=$1
    option_name=$2

    # Default dimension and scale values
    #
    dimspace=0
    dimtime=0
    dimcount=0
    scalespace=0
    scaletime=0
    scalecount=0

    cat - <<EOF

The dimension and scale for the ${metric_name} metrics may be expressed
in terms of Space, Time and Count (i.e. events or messages).  The default
dimension and scale is "None".  Do you wish to accept the default dimension
EOF
    $PCP_ECHO_PROG $PCP_ECHO_N "and scale [y]? ""$PCP_ECHO_C"

    read ans
    unitschange=false
    case $ans in
	N|n|NO|No|no)	unitschange=true;;
	*)		unitschange=false;;
    esac

    if [ $unitschange = true ]
    then
	echo
echo "The dimension is expressed in terms of powers of Space, Time and Count."
	echo "For example, bytes per second would be 1 -1 0, and milliseconds per message "
	echo "would be 0 1 -1."
	while true
	do
	    $PCP_ECHO_PROG $PCP_ECHO_N "Enter the dimension for Space, Time and Count [0 0 0]: ""$PCP_ECHO_C"
	    dimspace=0; dimtime=0; dimcount=0
	    read s t c

	    getnumeric 'Scale dimension' $s
	    [ $? -eq 1 ] && continue
	    dimspace=$numeric

	    getnumeric 'Time dimension' $t
	    [ $? -eq 1 ] && continue
	    dimtime=$numeric

	    getnumeric 'Count dimension' $c
	    [ $? -eq 1 ] && continue
	    dimcount=$numeric

	    break
	done

	if [ "$dimspace" != 0 ]
	then
	    while true
	    do
		echo "Scale for the Space dimension is expressed as:"
		echo "  0  (bytes)"
		echo "  1  (kilobytes)"
		echo "  2  (megabytes)"
		echo "  3  (gigabytes)"
		echo "  4  (terabytes)"
		$PCP_ECHO_PROG $PCP_ECHO_N 'Enter the scale for the Space dimension [0]: '"$PCP_ECHO_C"
		read scale
		scalespace=0

		getnumeric 'Space scale' $scale
		[ $? -eq 1 ] && continue
		if [ $numeric -lt 0 -o $numeric -gt 4 ]
		then
		    echo "-- Sorry, Space scale must be between 0 and 4 --"
		    continue
		fi
		scalespace=$numeric
		break
	    done
	fi

	if [ "$dimtime" != 0 ]
	then
	    while true
	    do
		echo "Scale for the Time dimension is expressed as:"
		echo "  0  (nanoseconds)"
		echo "  1  (microseconds)"
		echo "  2  (milliseconds)"
		echo "  3  (seconds)"
		echo "  4  (minutes)"
		echo "  5  (hours)"
		$PCP_ECHO_PROG $PCP_ECHO_N "Enter the scale for the Time dimension: ""$PCP_ECHO_C"
		read scale
		scaletime=0

		getnumeric 'Time scale' $scale
		[ $? -eq 1 ] && continue
		if [ $numeric -lt 0 -o $numeric -gt 5 ]
		then
		    echo "-- Sorry, Time scale must be between 0 and 5 --"
		    continue
		fi
		scaletime=$numeric
		break
	    done
	fi

	if [ "$dimcount" != 0 ]
	then
	    while true
	    do
		echo "Scale for the Count dimension is expressed:"
		$PCP_ECHO_PROG $PCP_ECHO_N "  as a power of 10 (e.g. 6 for 10^6, or -3 for 10^-3) [0]: ""$PCP_ECHO_C"
		read scale
		scalecount=0

		getnumeric 'Count scale' $scale
		[ $? -eq 1 ] && continue
		scalecount=$numeric
		break
	    done
	fi
    fi

    # show what units we're going with ..
    echo
    echo "Using the following units for ${metric_name}:"
    echo "  Dimensions:  space=$dimspace time=$dimtime count=$dimcount"
    echo "  Scale:       space=$scalespace time=$scaletime count=$scalecount"
    echo

    if [ $unitschange = true ]
    then
	uarg="$dimspace,$dimtime,$dimcount,$scalespace,$scaletime,$scalecount"
	args="$args ${option_name} $uarg"
    fi
}


pmdaSetup

$PCP_ECHO_PROG $PCP_ECHO_N "Use the default installation [y]? ""$PCP_ECHO_C"
read nogo
goforit=false
case $nogo in
    n|no|N|NO|No)	goforit=true;;
    *)			goforit=false;;
esac
echo
if [ $goforit = false ]
then
    :
elif $do_pmda
then
    args=""
    value=""

    $PCP_ECHO_PROG $PCP_ECHO_N "Trace period (in seconds) [60]? ""$PCP_ECHO_C"
    read value
    getnumeric 'Trace period' $value >/dev/null
    [ $? -ne 1 -a $numeric -ne 0 ] && args="-T $numeric"

    echo 
    $PCP_ECHO_PROG $PCP_ECHO_N "Number of buckets [5]? ""$PCP_ECHO_C"
    read value
    getnumeric 'Number of buckets' $value >/dev/null
    [ $? -ne 1 -a $numeric -ne 0 ] && args="$args -N $numeric"

    echo 
    $PCP_ECHO_PROG $PCP_ECHO_N "Port number for client connections [4323]? ""$PCP_ECHO_C"
    read value
    getnumeric 'Port number' $value >/dev/null
    [ $? -ne 1 -a $numeric -ne 0 ] && args="$args -I $numeric"

    getunits trace.observe.value -U
    getunits trace.counter.value -V

    while true
    do
	access=""
	echo
	$PCP_ECHO_PROG $PCP_ECHO_N "Client host access - (A)llow/(D)isallow [Enter to complete install]? ""$PCP_ECHO_C"
	read access
	access=`echo $access | tr -d ' '`
	[ "X$access" = "X" ] && break
	case $access in
	  A|a|Allow|allow)
	    echo
	    $PCP_ECHO_PROG $PCP_ECHO_N "Host specification (IP mask/Enter to cancel): ""$PCP_ECHO_C"
	    read access
	    access=`echo $access | tr -d ' '`
	    if [ "X$access" != "X" ]
	    then
		maxconns=""
		echo
		$PCP_ECHO_PROG $PCP_ECHO_N "Maximum number of connections from $access (Enter for no limit): ""$PCP_ECHO_C"
		read maxconns
		maxconns=`echo $maxconns | tr -d ' '`
		[ "X$maxconns" = "X" ] && maxconns=0
		args="$args"" -A ""allow:$access:$maxconns"
	    fi;;
	  D|d|Disallow|disallow)
	    echo
	    $PCP_ECHO_PROG $PCP_ECHO_N "Host specification (IP mask/Enter to cancel): ""$PCP_ECHO_C"
	    read access
	    access=`echo $access | tr -d ' '`
	    [ "X$access" != "X" ] && args="$args"" -A ""disallow:$access";;
	  *) echo 'Try again, "'$access'" not supported.';;
	esac
    done
    echo
fi

# for debugging the PMDA, uncomment this line ...
#
#args="-D appl0,appl1,pdu $args"

# Do it ...
#
pmdaInstall
echo Note: some warnings are expected until trace API calls are made - refer to
echo "      the man pages for pmtrace(1) and pmdatrace(3) for further details."

exit 0
