#! /bin/ksh -p
#
# ident "@(#)explorer_wrapper.ksh 1.6     03/01/05 SMI"
#
# Copyright (c) 2003 Sun Microsystems, Inc.
# All rights reserved.
# Use is subject to license terms.
#


##############################################
#
#
#	Service wrapper for explorer(1M)
#
#
#
# This wrapper provides three explorer related functions:
#
#	run:
#		Invoke explorer, adding any additional arguments
#		from optional file ${ARGFILE}.${clmode} according
#		to whether or not the node is booted in cluster mode.
#
#		Additional arguments are for adding commands such as
#		'vfstab-global-mount-points' to be run by explorer.
#		Users may wish to add '-keep' or '-w <module-name>'
#		to this file as well.
#		The contents of the file must be a single line,
#		appropriate for consumption by explorer.
#
#	gzipname:
#		Return filename of most recent compressed explorer
#		results.
#
#	dirname:
#		Return directoryname of most recent uncompressed
#		explorer results.
#
# This wrapper is intended for use only by sccheck(1M) java
# code. It is *not* intended for user-command-line use


# allow environmental overrides via config file
typeset SCCHECK_CONF=/etc/default/sccheck

if [[ -e ${SCCHECK_CONF} ]]; then
    . ${SCCHECK_CONF}
fi


typeset EXPLORER_HOME=${EXPLORER_HOME:-/opt/SUNWexplo}
typeset -r HOSTID=$(hostid)
typeset -r UNAME=$(/usr/bin/uname -n)
typeset -r EXPL_RESULTS_BASE=output/explorer.${HOSTID}.${UNAME}

typeset -r ARGFILE_BASE=/usr/cluster/lib/sccheck/explorer_args
typeset clmode=


if [[ ! -x ${EXPLORER_HOME}/bin/explorer ]]; then
   	printf "$(gettext '%s: %s not found.')\n" ${0} ${EXPLORER_HOME}/bin/explorer >&2
	exit 1
fi


if [[ -x /usr/sbin/clinfo ]]; then
    /usr/sbin/clinfo > /dev/null
    if [[ $? -eq 0 ]]; then
	clmode="cluster"
    else
	clmode="non-cluster"
    fi
else
    clmode="non-cluster"
fi

typeset ARGFILE=${ARGFILE_BASE}.${clmode}
typeset MOREARGS=""


# validate args

if [[ $# -ne 1 ]]
then
	printf "$(gettext 'usage: %s run | gzipname | dirname')\n" ${0} >&2
	exit 1
fi

# execute explorer

if [[ "$1" = "run" ]]; then
	NODE=$(uname -n)

	if [[ -f $ARGFILE ]]; then
	    
	    # read the first content line that doesn't contain a '#'
	    while read MOREARGS
	    do
		echo ${MOREARGS} | /usr/bin/grep "#"
		if [[ $? -eq 1 && -n ${MOREARGS} ]]; then 
		    break
		fi
	    done < ${ARGFILE}

	    # echo to stdout
	    printf "$(gettext 'Additional explorer arguments: %s')\n" "${MOREARGS}"
	fi

	# launch explorer
	eval ${EXPLORER_HOME}/bin/explorer ${MOREARGS}
	exit $?
fi


# get (newest) results file name

if [[ "$1" = "gzipname" ]]; then
	/usr/bin/ls -d ${EXPLORER_HOME}/${EXPL_RESULTS_BASE}*gz > /dev/null 2>&1 
	if [[ $? -ne 0 ]]; then
	    exit 1
	else
	    /usr/bin/ls -d ${EXPLORER_HOME}/${EXPL_RESULTS_BASE}*gz | /usr/bin/tail -1
	    exit 0
	fi
fi


# get (newest) results dir name

if [[ "$1" = "dirname" ]]; then
	/usr/bin/ls -d ${EXPLORER_HOME}/${EXPL_RESULTS_BASE}* | /usr/bin/grep -v tar.gz > /dev/null 2>&1
	if [[ $? -ne 0 ]]; then
	    exit 1
	else
	    /usr/bin/ls -d ${EXPLORER_HOME}/${EXPL_RESULTS_BASE}* | /usr/bin/grep -v tar.gz | /usr/bin/tail -1
	    exit 0
	fi
fi

# else

printf "$(gettext '%s: unknown option: %s')\n" ${0} ${1} >&2

exit 1

