#!/bin/ksh
#
# Copyright 1997, 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 shping PMDA and/or PMNS
#
# $Id: Install,v 1.2 1999/05/11 00:28:03 kenmcd Exp $
#

# Some degree of paranoia here ...
PATH=/usr/sbin:/usr/bsd:/sbin:/usr/bin:/bin:/usr/pcp/bin
export PATH

# Get the common procedures and variable assignments
#
. /usr/pcp/lib/pmdaproc.sh

# The name of the PMDA
#
iam=shping

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

# Do it
#
pmdaSetup

# controls for installation procedures
#
daemon_opt=true		# can install as daemon
dso_opt=false
pipe_opt=true		# supports pipe IPC
socket_opt=false	# force pipe IPC
check_delay=10		# give the PMDA a chance to set itself up

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


# PMDA variables
#
tmp=/tmp/$$
configfile=""
cycle=120
timeout=20
debug=0

trap "rm -f $tmp.*; exit" 0 1 2 3 15

_quit()
{
    rm -f $tmp*
    exit $1
}

do_debug=false

_parsedefaults()
{
    echo "Extracting options from current installation ..."
    while getopts D:I:d:l:t: c
    do
    	case $c in
	    \?)		echo "Warning: Unrecognized option in /etc/pmcd.conf"
			echo "         Remove line for the $iam PMDA in /etc/pmcd.conf and re-run ./Install"
	    		_quit 2;;
	    D )		debug=$OPTARG;;
	    I )		cycle=$OPTARG;;
	    t )		timeout=$OPTARG;;
	    * )		;;
	esac
    done
    eval configfile='$'$OPTIND
}

if $do_pmda
then

    # set options from /etc/pmcd.conf, if possible
    #
    ans=`nawk </etc/pmcd.conf '
$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 [ ! -f "$configfile" ]
    then
	echo "Do you wish to enter commands to create a new configuration file? [y] \c"
	read ans
	if [ "X$ans" = "Xy" -o "X$ans" = "XY" -o -z "$ans" ]
	then
	    configfile="$configdir/$iam.conf"
	    if [ -f $configfile ]
	    then
		    echo "Removing old configuration file \"$configfile\""
		    rm -f $configfile
		    if [ -f $configfile ]
		    then
			    echo "Cannot remove \"$configfile\""
			    _quit 1
		    fi
	    fi

	    echo
	    echo \
'Enter one ping specification per line, in the format

tag		command line details

where the "tag" is a single unique word (no spaces) and the "command line
details" are the corresponding sh(1) command.  For example

dns-self	nslookup `/usr/bsd/hostname`

An empty line terminates the specification process and there must be at
least one specification.
'

	    args=""
	    touch $configfile
	    if [ ! -f $configfile ]
	    then
		echo "Installation aborted."
		_quit 1
	    fi

	    while [ ! -s "$configfile" ]
	    do
		while true
		do
		    echo "Tag Command: \c"
		    read tag cmd
		    [ -z "$tag" ] && break
		    if grep "^$tag " $configfile >/dev/null
		    then
			echo "Sorry, tag \"$tag\" already in use.  Please try again."
			continue
		    fi
		    echo "$tag $cmd" >>$configfile
		done
	    done
	else
	    echo ""
	    echo "Error: Abandoning installation as no configuration file was specified."
	    _quit 1
	fi
    fi

    if fgrep "CONFIGURE-ME-PLEASE" $configfile >/dev/null
    then
	# sample configuration file needs a little customization
	#
	if [ -f /etc/resolv.conf ]
	then
	    my_dns_server=`nawk </etc/resolv.conf '$1 == "nameserver" { print $2; exit }'`
	else
	    my_dns_server=`/usr/bsd/hostname`
	fi
	ex - $configfile <<End-of-File
g/CONFIGURE-ME-PLEASE/d
g/^dns.*DEFAULT-DNS-SERVER/s/DEFAULT-DNS-SERVER/$my_dns_server/
w!
q
End-of-File
    fi

    echo
    echo "All commands are run one after another as a group and the group is run"
    echo "once per \"cycle\" time.   Enter the cycle time in seconds [$cycle] \c"
    read ans
    if  [ ! -z "$ans" ]
    then
    	cycle=$ans
    fi

    echo
    echo "Each command must complete within a timeout period, or it will be aborted"
    echo "by the \"$iam\" PMDA.  Enter the timeout period (in seconds) [$timeout] \c"
    read ans
    if  [ ! -z "$ans" ]
    then
    	timeout=$ans
    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="-I $cycle -t $timeout -D $debug $configfile"
fi

pmdaInstall

_quit 0
