#!/bin/ksh -p
# $Id: //depot/stable/explorer/tools/scextended#3 $
#
# Explorer Version:
EXP_VERSION=4.0
#

# Source tools functions
. ${EXP_LIB}/exp_tools

# Run if specified to do so
script=${0##*/}
which_gate_exit extended interactive $script default all

# defaults
SC_NAME="NOTNOISE"
SC_TIME=60
TTY=`tty`
if [ "${TTY}" = "not a tty" ]
then
    TTY=""
fi

collect_data () {
    # Use small default time for first command to ensure this is an SC
    OPT="telnet:sc:${SC_NAME}:${SC_PASS}:5"
    get_cmd "showsc -v" ${OPT} sc/${SC_NAME}/

    # Did we fail to run showsc?
    NUMLINES=`wc -l "${EXP_TARGET}/sc/${SC_NAME}/showsc_-v.out" |  nawk '{ print $1 }' | sed -e 's/ //g'`
    if [ "${NUMLINES}" -lt 4 ]
    then
	# Try to run it again
	rm -f "${EXP_TARGET}/sc/${SC_NAME}/showsc_-v.out"
	get_cmd "showsc -v" ${OPT} sc/${SC_NAME}/
    fi

    # Still unable to get output?
    NUMLINES=`wc -l "${EXP_TARGET}/sc/${SC_NAME}/showsc_-v.out" |  nawk '{ print $1 }' | sed -e 's/ //g'`
    if [ "${NUMLINES}" -lt 4 ]
    then
	rm -rf ${EXP_TARGET}/sc/${SC_NAME}
	TEXT=`gettext "unable to collect data for %s"`
	TEXT=`printf "${TEXT}" "${SC_NAME}"`
	logfile ERROR "${TEXT}"
	return
    fi

    # Make sure platform password correct
    if egrep -s -e "Invalid password" ${EXP_TARGET}/sc/${SC_NAME}/showsc_-v.out
    then
	rm -rf ${EXP_TARGET}/sc/${SC_NAME}
	TEXT=`gettext "SC platform password for %s is incorrect"`
	TEXT=`printf "${TEXT}" "${SC_NAME}"`
	logfile ERROR "${TEXT}"
	return
    fi

    # Make sure we are talking to an SC
    if ! egrep -s -e "Platform Shell" ${EXP_TARGET}/sc/${SC_NAME}/showsc_-v.out
    then
	rm -rf ${EXP_TARGET}/sc/${SC_NAME}
	TEXT=`gettext "%s is not a System Controller"`
	TEXT=`printf "${TEXT}" "${SC_NAME}"`
	logfile ERROR "${TEXT}"
	return
    fi

    which_gate fru && FRU=notempty
    if [ -n "${FRU}" ]
    then
	if [ -z "${SC_PASS}" ]
	then
	    get_cmd "${EXP_HOME}/bin/rprtfru.`uname -p` -b ${SC_NAME}:none -x" sc/${SC_NAME}/prtfru_-x
	else
	    get_cmd "${EXP_HOME}/bin/rprtfru.`uname -p` -b ${SC_NAME}:${SC_PASS} -x" sc/${SC_NAME}/prtfru_-x
	fi
    fi

    # Default collection
    OPT="telnet:sc:${SC_NAME}:${SC_PASS}:${SC_TIME}"
    get_cmd "showplatform -v"       ${OPT} sc/${SC_NAME}/
    get_cmd "showplatform -p frame" ${OPT} sc/${SC_NAME}/
    get_cmd "showplatform -d a"     ${OPT} sc/${SC_NAME}/
    get_cmd "showplatform -d b"     ${OPT} sc/${SC_NAME}/
    get_cmd "showplatform -d c"     ${OPT} sc/${SC_NAME}/
    get_cmd "showplatform -d d"     ${OPT} sc/${SC_NAME}/
    get_cmd "showdate -v"           ${OPT} sc/${SC_NAME}/
    get_cmd "showdate -v -d a"      ${OPT} sc/${SC_NAME}/
    get_cmd "showdate -v -d b"      ${OPT} sc/${SC_NAME}/
    get_cmd "showdate -v -d c"      ${OPT} sc/${SC_NAME}/
    get_cmd "showdate -v -d d"      ${OPT} sc/${SC_NAME}/
    get_cmd "showlogs -v"           ${OPT} sc/${SC_NAME}/
    get_cmd "showlogs -v -d a"      ${OPT} sc/${SC_NAME}/
    get_cmd "showlogs -v -d b"      ${OPT} sc/${SC_NAME}/
    get_cmd "showlogs -v -d c"      ${OPT} sc/${SC_NAME}/
    get_cmd "showlogs -v -d d"      ${OPT} sc/${SC_NAME}/

    # Increase timeout
    OPT="telnet:sc:${SC_NAME}:${SC_PASS}:300"
    get_cmd "showboards -e"            ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -p proms"      ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -p cpu"     ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -p memory"  ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v"            ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -d a"       ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -d b"       ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -d c"       ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -d d"       ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -p board"   ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -p clock"   ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -p io"      ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -p power"   ${OPT} sc/${SC_NAME}/
    get_cmd "showboards -v -p version" ${OPT} sc/${SC_NAME}/
    get_cmd "showcomponent -d a"       ${OPT} sc/${SC_NAME}/
    get_cmd "showcomponent -d b"       ${OPT} sc/${SC_NAME}/
    get_cmd "showcomponent -d c"       ${OPT} sc/${SC_NAME}/
    get_cmd "showcomponent -d d"       ${OPT} sc/${SC_NAME}/
    get_cmd "showenvironment -tv"      ${OPT} sc/${SC_NAME}/

    # Get a list of boards from showboards -e
    if [ ! -f "${EXP_TARGET}/sc/${SC_NAME}/showboards_-e.out" ]
    then
	TEXT=`gettext "Unable to parse showboards for %s"`
	TEXT=`printf "${TEXT}" "${SC_NAME}"`
	logfile ERROR "${TEXT}"
	return
    fi

    # Parse showboards -e
    while read line
    do
	BOARD=`echo $line | nawk '{ print $1 }'`

	# Check to see if board is On
	if echo $line | egrep -s -e On
	then
	    echo "${BOARD}" >> ${EXP_TMPDIR}/brd.${EXP_PID}
	fi
    done < "${EXP_TARGET}/sc/${SC_NAME}/showboards_-e.out"

    OPT="telnet:sc:${SC_NAME}:${SC_PASS}:${SC_TIME}"
    # Get component data for boards that were on
    if [ -f "${EXP_TMPDIR}/brd.${EXP_PID}" ]
    then
	for BOARD in `cat ${EXP_TMPDIR}/brd.${EXP_PID}`
	do
	    get_cmd "showcomponent ${BOARD}" ${OPT} sc/${SC_NAME}/
	done
	rm -f ${EXP_TMPDIR}/brd.${EXP_PID}
    fi
}

# Interactive?
RUN=""
which_gate interactive && RUN=notempty
which_gate scextended  && RUN=notempty
which_gate extended    && RUN=notempty

egrep -v '^#' ${EXP_ETC}/scinput.txt
if [ $? = 0 ]
then
	RUN=""
fi

if [ -n "${RUN}" ]
then
    logfile "Collecting extended data interactively"

    TEXT=`gettext "\nYou will be prompted for a System Controller name\n    If hostname cannot be resolved, please use IP address.\n\nType return to end the list.\n\n"`
    prompt "${TEXT}"
    prompt ""

    while [ -n "${SC_NAME}" ]
    do
	TEXT=`gettext "Please enter the name of the SC: "`
	prompt "${TEXT}\c"
	read SC_NAME

	if [ -n "${SC_NAME}" ]
	then
	    # Can we ping it?
	    TEXT=`gettext "Verifying connection to %s"`
	    TEXT=`printf "${TEXT}" "${SC_NAME}"`
	    logfile "${TEXT}"
	    ping ${SC_NAME} >/dev/null 2>&1
	    if [ "$?" -ne 0 ]
	    then
		TEXT=`gettext "Unable to connect to ${SC_NAME}"`
		TEXT=`printf "${TEXT}" "${SC_NAME}"`
		logfile ERROR "${TEXT}"
		prompt ""
		continue
	    fi

	    # Get password
	    TEXT=`gettext "Please enter the SC platform password for %s: "`
	    TEXT=`printf "${TEXT}" "${SC_NAME}"`
	    prompt "${TEXT}\c"
	    [ -n "${TTY}" ] && stty -echo
	    read SC_PASS
	    [ -n "${TTY}" ] && stty echo
	    prompt ""

	    collect_data
	    echo "Data collection complete."
            TEXT=`gettext "Data gathering complete for SC: %s."`
            TEXT=`printf "${TEXT}" "${SC_NAME}"`
            logfile "${TEXT}"
	    prompt ""
	fi
    done
fi

# Collect input file if secure mode selected (-w secure)
which_gate secure && SECURE=notempty
if [ -n "${SECURE}" ]
then
    get_file "${EXP_ETC}/scinput.txt" sc/
fi

# Not interactive, input file?
get_cmd "/bin/ls -l ${EXP_ETC}/scinput.txt" sc/
if [ -r "${EXP_ETC}/scinput.txt" ]
then
    # Make sure input file is mode 0600
    if `ls -l ${EXP_ETC}/scinput.txt | nawk '{ print $1 }' | egrep -s -e '------$'`
    then
	logfile "Collecting extended data from ${EXP_ETC}/scinput.txt file"
	while read line
	do
	    # Skip comments and blank lines
	    echo $line | sed -e 's/^ *//' -e 's/ *$//' | egrep -s -e '^#' && continue
	    echo $line | egrep -s -e '^$' && continue

	    SC_NAME=`echo $line | nawk '{ print $1 }'`
	    SC_PASS=`echo $line | nawk '{ print $2 }'`

	    if [ -d "${EXP_TARGET}/sc/${SC_NAME}" ]
	    then
		logfile "Data for ${SC_NAME} already collected"
	    else
		# Can we ping it?
		TEXT=`gettext "Verifying connection to %s"`
		TEXT=`printf "${TEXT}" "${SC_NAME}"`
		logfile "${TEXT}"
		ping ${SC_NAME} >/dev/null 2>&1
		if [ "$?" -ne 0 ]
		then
		    TEXT=`gettext "Unable to connect to ${SC_NAME}"`
		    TEXT=`printf "${TEXT}" "${SC_NAME}"`
		    logfile ERROR "${TEXT}"
		    prompt ""
		    continue
		fi

		logfile "Collecting extended data for ${SC_NAME}"
		collect_data
                TEXT=`gettext "Data gathering complete for %s."`
                TEXT=`printf "${TEXT}" "${SC_NAME}"`
                logfile "${TEXT}"
	    fi
	done < ${EXP_ETC}/scinput.txt
    else
	logfile WARNING "${EXP_ETC}/scinput.txt file not correct mode"
    fi
fi
