#!/bin/sh
#@(#)explorer	1.53 06/13/99 16:04:09

# Set the environment variables we will need.
EXPLORER_ADDRESS='explorer-database@sun.com'
export EXPLORER_ADDRESS

EXPLORER_EMAIL=false
export EXPLORER_EMAIL

REV=`cat tools/rev.txt`
export REV

PATH=/usr/bin:/usr/sbin:/sbin:/etc:/usr/ucb:`/usr/bin/pwd`/tools
export PATH

umask 022



# Make sure this is a Solaris 5.x system
if [ "`uname -r |sed 's/./&#/'|sed 's/#.*//'`" != "5" ]
then
    echo 
    echo ""
    echo "This program can be executed on Solaris 2.x systems only"
    echo ""
    exit 1
fi

# Make sure user is root.
if [ "`id |nawk '{print $1}'| grep root`" = "" ]
then
    echo 
    echo ""
    echo "You must be root to execute this program"
    echo ""
    exit 1
fi


# DIRNAME where results will be logged
DIRNAME="explorer.`hostid`.`hostname`-`date '+%Y.%m.%d.%H.%M'`"
export DIRNAME

# Start with a clean slate in case we had a bad run. This will also
# eliminate a security hole.
rm -rf "$DIRNAME"
rm -f "$DIRNAME"-tar.Z
rm -f explorer.`hostid`.uu

# create working dir and make sure root was able to create it
mkdir  "$DIRNAME"  2>/dev/null
STATUS=$?
if [ $STATUS -ne 0 ]
then
    # Exit if directory was not created successfully.
    echo 
    echo ""
    echo "Root must have write permission to the current directory"
    echo ""
    exit 1
fi

cd     "$DIRNAME"

cp ../tools/rev.txt rev

# cycle through all of the input parameters, and process them
# $* passes all of the input params to input
# Note: we are sourcing input to get the paramters carried into this shell
. ../tools/input $*

# Create the README header information
../tools/gen.readme.head

if [ $BATCH_MODE = "false" ]
then
    # Display the banner
    ../tools/banner

    # Gather cutomer name, hostname, etc. and write it into the README
    # gen.readme.1 is for generic interactive mode.
    # gen.readme.e10000 is for the E10000 FAILURE INFORMATION FORM.
    if [ $E10000_return_fru = "false" ]
    then
        ../tools/gen.readme.1
    else
        ../tools/gen.readme.e10000
    fi
fi

../tools/gen.readme.2


# give user a chance to choose whether to mailResults to the explorer
# repository, the file has logic to skip prompting when in batch mode. I
# need to source this file so I can carry the environment variable
# EXPLORER_EMAIL to the current shell.
. ../tools/mailResult

if [ $BATCH_MODE = "false" ]
then
    echo ""
    echo ""
    echo "Gathering information, please wait... "
    echo""
fi

# Get disk information, SSA firmware & revcode,disk serial num etc.
if [ $BATCH_MODE = "false" ]
then
    echo "Getting disk information"
fi
mkdir disks
../tools/disks 1>/dev/null 2>&1
../tools/sds.info
../tools/sonoma
../tools/ssa
../tools/photon
../tools/vx.info

# Get some /etc files
if [ $BATCH_MODE = "false" ]
then
    echo "Getting etc information"
fi
../tools/etc.info 1>/dev/null 2>&1

if [ $BATCH_MODE = "false" ]
then
    echo "Getting network information"
fi
../tools/netinfo

# Get patch and pkginfo listing
if [ $BATCH_MODE = "false" ]
then
    echo "Getting patch and pkgs information"
fi
mkdir patch+pkg
../tools/patch
../tools/pkg


# Get system configuration
if [ $BATCH_MODE = "false" ]
then
    echo "Getting system configuration"
fi
mkdir sysconfig
../tools/sysconfig
../tools/var


# Get System Service Processor configuration, i.e. starfire
if [ $BATCH_MODE = "false" ] && [ -d /var/opt/SUNWssp ]
then
    echo "Getting System Service Processor configuration"
fi
../tools/starfire.ssp

# Get SunCluster and HA information
../tools/cluster

../tools/u4ft

# Get messages files
if [ $BATCH_MODE = "false" ]
then
    echo "Getting messages information"
fi
mkdir messages
cp -p    /var/adm/messages ./messages 1>/dev/null 2>&1
cp -p    /var/adm/messages.[0-9] ./messages 1>/dev/null 2>&1
dmesg > ./messages/dmesg.out

# Do final cleanup of any superfluous files, e.g. core
../tools/cleanup 1>/dev/null 2>&1

# Tar and compress the info, and clean up explorer foot print
cd ..
# produce a /usr/bin/compress compressed file for users who want to
# keep explorer output, this is for compatability with Unix only tools
# as some users may not have gzip.
tar cf - "$DIRNAME" | compress > "$DIRNAME"-tar.Z

# produce a gzip compressed file to be mailed to explorer-database and
# uuencode it (gzip compresses much better than /bin/compress). The gziped
# file is named DIRNAME-tar.z (this will be the name resulting from uudecode).
# The uuencoded file name will be: explorer.hostid.uu, I shourtend the .uu
# file name since there have been problems reported with long file names
# being attached to mailtool

file2mail="explorer.`hostid`.uu"
tar cf - "$DIRNAME" | ./tools/gzip -9c | uuencode "$DIRNAME"-tar.z > $file2mail

rm -rf "$DIRNAME"

if [ $EXPLORER_EMAIL != false ]
then
    if [ $BATCH_MODE = "false" ]
    then
        echo
        echo
        echo "Generating e-mail"
        echo
    fi
    
    # e-mail the results
    mailx -s `echo "$file2mail"` "$EXPLORER_EMAIL" < "$file2mail"
fi

case "$KEEP" in
    false)
	rm -rf tools
	rm -rf explorer
	rm -rf explorer.template
	rm -rf explorer.man
    	;;
    keep)
	rm -rf "$file2mail"
	rm -rf "$DIRNAME"-tar.Z
    	;;
    keepdata)
	rm -rf "$file2mail"
    	;;
esac


if [ $BATCH_MODE = "true" ] || [ $KEEP = "keep" ]
then
    exit 0
fi

clear
cat <<EOF


Done gathering system information

You can extract the data by:

# zcat $DIRNAME-tar.Z | tar xvf -

Output directory naming convention is:
explorer.hostid.hostname-yyyy.mm.dd

Note: the file: $file2mail is tar,gzip,uuencode.

EOF

if [ $EXPLORER_EMAIL = false ]
then

cat <<EOF

You didn't select to e-mail results automatically. You may
copy the file:  $file2mail
and mail it to: $EXPLORER_ADDRESS

Note: Please send ONLY ONE file per e-mail to avoid confusing the
      automatic e-mail processor. Don't get fancy and don't mail a
      file after performing elaborate manipulations on it. e-mail
      processing is automatic, no body is reading it. The best is to
      add the .Z or .uu file as a single attachment, don't tar it or
      further compress it before you mail it. At this point in time
      the e-mail processor is not smart enough to read human minds or
      follow written instructions.

EOF

fi
