#!/bin/ksh -p
# $Id: //depot/stable/explorer/tools/sf15k-sc#2 $
#
# Explorer Version:
EXP_VERSION=4.0
#

# Source tools functions
. ${EXP_LIB}/exp_tools

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

# Require the root SMS package before attempting collection
TEXT=`gettext "SMS not installed"`
require "pkginfo -q SUNWSMSr*" "${TEXT}"

# Function to create a user with appropriate permissions
add_user () {
    # Set user variable
    EXP_SMSUSER="exp${EXP_PID}"

    # useradd
    useradd -o -d / -s /bin/ksh ${EXP_SMSUSER}

    # Provide platform service and admin privileges
    /opt/SUNWSMS/bin/smsconfig -a -u ${EXP_SMSUSER} -G svc platform
    /opt/SUNWSMS/bin/smsconfig -a -u ${EXP_SMSUSER} -G admn platform
    for i in A B C D E F G H I J K L M N O P Q R
    do
        /opt/SUNWSMS/bin/smsconfig -a -u ${EXP_SMSUSER} -G admn $i
    done
}

# Function to remove service user
rm_user () {
    # Make sure EXP_SMSUSER is set
    [ -z "${EXP_SMSUSER}" ] && EXP_SMSUSER="exp${EXP_PID}"

    # Remove platform service and admin privileges
    /opt/SUNWSMS/bin/smsconfig -r -u ${EXP_SMSUSER} -G svc platform
    /opt/SUNWSMS/bin/smsconfig -r -u ${EXP_SMSUSER} -G admn platform
    for i in A B C D E F G H I J K L M N O P Q R
    do
        echo "Explorer is removing the file: \c"
        find /var/opt/SUNWSMS/adm/"${i}"/ -user ${EXP_SMSUSER} -print -exec rm {} \;
        /opt/SUNWSMS/bin/smsconfig -r -u ${EXP_SMSUSER} -G admn $i
    done

    # userdel
    userdel ${EXP_SMSUSER}

    # Unset EXP_SMSUSER variable
    unset EXP_SMSUSER
}

# Trap handler - remove user if we try to stop the script
cleanup () {
    rm_user
    exit
}

trap 'cleanup' 1 2 3 14 15

add_user

# Determine wheher this sc is the MAIN or SPARE sc
# collect information from the hardware
# SC FPs are tricky: I can read the opposite SC without an error but the 
# results are wrong therefore, we need to find out which SC we are running on.
# "showboards -v" tells us which SC is currently MAIN and which is SPARE. 
# Result of the "showboards -v" command are in the "sf15k/showboards_-v.out" file.
# This is useful on the MAIN SC, but most commands will not run on the SPARE. 
# The first step is to consult showfailover -r:
#       MAIN    we are MAIN (wow!)
#       SPARE   we are spare
#       ?@%$#   we are not happy: misconfiguration, software not running, ...
# Determine wheher this sc is the MAIN or SPARE SC

# Set Role SC is playing and write to a file
ROLE=`su ${EXP_SMSUSER} -c "/opt/SUNWSMS/bin/showfailover -r" 2> /dev/null`

# General information files and directories
get_dir  "/var/opt/SUNWSMS/.pcd"                    sf15k/pcd/
get_file "/var/opt/SUNWSMS/adm/.logger"             sf15k/adm/logger!
get_file "/var/opt/SUNWSMS/adm/mess*"               sf15k/adm/
get_file "/var/opt/SUNWSMS/adm/platform/mess*"      sf15k/adm/platform/
get_file "/var/sadm/system/logs/smsbackup"          sf15k/logs/
get_file "/etc/opt/SUNWSMS/config/.fomd_uids.cf"    sf15k/etc/config/fomd_uids.cf!
get_file "/etc/opt/SUNWSMS/config/platform/.postrc" sf15k/etc/config/postrc!
get_dir  "/var/opt/SUNWSMS/adm/platform/dump"       sf15k/adm/platform/dump/
get_dir  recursive "/var/opt/SUNWSMS/adm/anonymous" sf15k/adm/anonymous/
get_dir  recursive "/etc/opt/SUNWSMS/SMS"           sf15k/etc/

# Run smsversion (as root)
get_cmd "/opt/SUNWSMS/bin/smsversion -t" sf15k/

# Get config listing (as root)
get_cmd "/bin/ls -laR /etc/opt/SUNWSMS/SMS/config" sf15k/

# Basic commands as ${EXP_SMSUSER}
get_cmd "/opt/SUNWSMS/bin/showfailover"    user=${EXP_SMSUSER} sf15k/
get_cmd "/opt/SUNWSMS/bin/showfailover -r" user=${EXP_SMSUSER} sf15k/
get_cmd "/opt/SUNWSMS/bin/showfailover -v" user=${EXP_SMSUSER} sf15k/

# Change MAIN to "MAIN" in three places
if [[ "${ROLE}" = "MAIN" ]]
then
        get_cmd "/opt/SUNWSMS/bin/showplatform"    user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showplatform -v" user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showenvironment" user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showdate -v"     user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/marginclock"     user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/marginvoltage"   user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showboards -v"   user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showbus"         user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showbus -v"      user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showcmdsync"     user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showcmdsync -v"  user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showdatasync -l" user=${EXP_SMSUSER} sf15k/
        get_cmd "/opt/SUNWSMS/bin/showdatasync -v" user=${EXP_SMSUSER} sf15k/
fi

# Get thermcal data
# thermcal command may not be supported on sf15k.  Checking on this.
#for brd in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#do
#    # Boards are CS[0-1] and EX[0-17]
#    case ${brd} in
#        0|1)
#           get_cmd "/opt/SUNWSMS/bin/thermcal -r CS${brd}" user=${EXP_SMSUSER} sf15k/
#           get_cmd "/opt/SUNWSMS/bin/thermcal -r EX${brd}" user=${EXP_SMSUSER} sf15k/
#           ;;
#       *)
#           get_cmd "/opt/SUNWSMS/bin/thermcal -r EX${brd}" user=${EXP_SMSUSER} sf15k/
#           ;;
#    esac
#done

# Get revisions on lpost elf files
for OBJ in /opt/SUNWSMS/hostobjs/*.elf
do
    MOD=`basename ${OBJ}`
    get_cmd "/usr/ccs/bin/mcs -p ${OBJ} | grep ${MOD}" sf15k/hostobjs/${MOD}!
done

# Get getfacl output
get_cmd "/bin/getfacl /etc/opt/SUNWSMS/SMS/config/"         sf15k/getfacl/
get_cmd "/bin/getfacl /etc/opt/SUNWSMS/SMS/config/platform" sf15k/getfacl/
get_cmd "/bin/getfacl /var/opt/SUNWSMS/adm/"                sf15k/getfacl/
get_cmd "/bin/getfacl /var/opt/SUNWSMS/adm/platform"        sf15k/getfacl/
get_cmd "/bin/getfacl /var/opt/SUNWSMS/adm/anonymous"       sf15k/getfacl/
get_cmd "/bin/getfacl /var/opt/SUNWSMS/data/"               sf15k/getfacl/

# Domain information
for DOMAIN in A B C D E F G H I J K L M N O P Q R
do
    # Make sure we have a domain configured
    [ ! -f "/var/opt/SUNWSMS/data/${DOMAIN}/nvramdata" ] && continue

    # Domain information
    get_file "/etc/opt/SUNWSMS/config/${DOMAIN}/.postrc" sf15k/${DOMAIN}/postrc!
    get_dir  recursive "/var/opt/SUNWSMS/adm/${DOMAIN}"  sf15k/${DOMAIN}/adm/
    get_dir  "/var/opt/SUNWSMS/data/${DOMAIN}"           sf15k/${DOMAIN}/data/
    get_dir  "/var/opt/SUNWSMS/.lock/${DOMAIN}"          sf15k/${DOMAIN}/lock/

    # Domain commands
    get_cmd "/opt/SUNWSMS/bin/sysid -d ${DOMAIN}"          user=${EXP_SMSUSER} sf15k/${DOMAIN}/data/
    if [[ "${ROLE}" = "MAIN" ]]
    then
        get_cmd "/opt/SUNWSMS/bin/showdate -v -d ${DOMAIN}"    user=${EXP_SMSUSER} sf15k/${DOMAIN}/
        get_cmd "/opt/SUNWSMS/bin/showdevices -v -d ${DOMAIN}" user=${EXP_SMSUSER} sf15k/${DOMAIN}/
        get_cmd "/opt/SUNWSMS/bin/showobpparams -d ${DOMAIN}"  user=${EXP_SMSUSER} sf15k/${DOMAIN}/
        get_cmd "/opt/SUNWSMS/bin/showkeyswitch -d ${DOMAIN}"  user=${EXP_SMSUSER} sf15k/${DOMAIN}/
        su ${EXP_SMSUSER} -c "/opt/SUNWSMS/bin/showkeyswitch -d ${DOMAIN} | cut -d: -f2 | egrep -s -i -e ON"
        if [ "$?" = 0 ]
        then
                get_cmd "/opt/SUNWSMS/bin/showxirstate -d ${DOMAIN}"   user=${EXP_SMSUSER} sf15k/${DOMAIN}/
        fi
    fi

    # getfacl commands
    get_cmd "/bin/getfacl /etc/opt/SUNWSMS/SMS/config/${DOMAIN}" sf15k/getfacl/
    get_cmd "/bin/getfacl /var/opt/SUNWSMS/adm/${DOMAIN}"        sf15k/getfacl/
    get_cmd "/bin/getfacl /var/opt/SUNWSMS/data/${DOMAIN}"       sf15k/getfacl/
done

# Gather the SF15k flash information

# the flash prom image files
OBPIMG=/opt/SUNWSMS/firmware/SCOBPimg.di
POSTIMG=/opt/SUNWSMS/firmware/SSCPOST.di
SBIMG=/opt/SUNWSMS/hostobjs/sgcpu.flash

# sample the current versions from disk
# check that the files are available and substitute with others if not
if [ ! -f "${OBPIMG}" ]
then
        OBPIMG=${SBIMG}
fi

if [ ! -f "${POSTIMG}" ]
then
        POSTIMG=${SBIMG}
fi

if [ ! -f "${SBIMG}" ]
then
        if [ "${SBIMG}" = "${OBPIMG}" -a "${SBIMG"} = "${POSTIMG}" ]
        then
                # no valid file found, quit
                exit -1
        else
                if [ "${SBIMG}" != "${OBPIMG}" ]
                then
                        # ${OBPIMG} is valid (meaning POSTIMG probably is not)
                        SBIMG=${OBPIMG}
                        POSTIMG=${OBPIMG}
                else
                        # ${OBPIMG} is invalid (meaning POSTIMG must be)
                        SBIMG=${POSTIMG}
                        OBPIMG=${POSTIMG}
                fi
        fi
fi

for sc in 0 1
do
        get_cmd "/opt/SUNWSMS/bin/flashupdate -f ${OBPIMG} -n SC${sc}/FP0" user=${EXP_SMSUSER} sf15k/flashupdate/
        get_cmd "/opt/SUNWSMS/bin/flashupdate -f ${POSTIMG} -n SC${sc}/FP1" user=${EXP_SMSUSER} sf15k/flashupdate/
done

# go for the SB's flashproms now if we can
if [[ "${ROLE}" = "MAIN" ]]
then
        sb=0
        while [ ${sb} -lt 18 ]
        do
                get_cmd "/opt/SUNWSMS/bin/flashupdate -f ${SBIMG} -n SB${sb}" user=${EXP_SMSUSER} sf15k/flashupdate/            
                sb=`expr $sb + 1`
        done
fi
 
 
# Clean up -- remove user
rm_user
#  
