#!/bin/ksh

# @15 08/15/03 LCL Add registry info to DIG.tar
# @14 07/07/03 LCL Linux details improved
# @13 06/27/03 LCL Add Linux
# @12 10/15/02 LCL AIX bitmode from kernel
# @11 03/23/02 LCL AIX 5.x 32/64 bit check
# @10 03/13/02 LCL AIX OS level derived from bos.rte.boot
# @09 06/28/01 LCL -t option to dump CMS tables
# @08 04/11/01 LCL Force directory dump to follow links
# @07 11/21/00 LCL Add IgnoreAgentLogs option
# @06 07/19/00 LCL Add stuff to PATH
# @05 07/13/00 LCL Display cmdline. Improve 64-bit AIX test. Repl echos.
# @04 07/05/00 LCL Get AIX/JDK build info
# @03 06/20/00 LCL Fix bugs, add -h
# @02 06/19/00 LCL Switch null to /dev/null
# @01 06/09/00 LCL Initial development (lots borrowed from RJantz, PBanks)
#
# Script to find and display site/user information related to Candle installs

PROGNAME=$(basename $0)

Err_report() {
   case $1 in
      #$NLS-E$ "$PROGNAME warning: $2,continuing ..."
      warn) print -u2 "$PROGNAME `nls_replace 'KCI0535I'`: $2,`nls_replace 'KCI0536I'` ..."
            sleep 2
            return
            ;;
	  #$NLS-E$ "$PROGNAME failure: $2."		
      fail) print -u2 "$PROGNAME `nls_replace 'KCI0537I'`: $2."
            exit 2
            ;;
      info) print -u2 "$PROGNAME        : $2."
            return
            ;;
   esac
}

Use_report() {
    #$NLS-E$ "$PROGNAME [-h candle_directory] [-a] [-s] [-k] [-i] [-t] [-l label]\n -a build a tar file of All your site information.\n -s to get a 1 line OS Summary\n -k Keep the working directory after completion.\n -i Ignore agent logs when building the site info tar file.\n -t get cms Tables.\n -l define a Label or text string identifier to display on the output.\n -h specify a CandleHome directory."
    print -u2 "$PROGNAME [-h candle_directory] [-a] [-s] [-k] [-i] [-t] [-l label]"
	print "`nls_replace 'KCI0795I' -a`"
	print "`nls_replace 'KCI0796I' -s`"
	print "`nls_replace 'KCI0797I' -k`"
	print "`nls_replace 'KCI0798I' -i`"
	print "`nls_replace 'KCI0799I' -t`"
	print "`nls_replace 'KCI0800I' -l`"
	print "`nls_replace 'KCI0801I' -h`"
    exit 2
}

###################################################################
#                         Main Routine                            #
###################################################################
bindir=`pwd`/$(dirname $0)
[ "$(print $0 | grep '\./')" = "" ] && [ "$(print $0 | grep '^/.*')" = "" ] && [ `pwd` = '/' ] && bindir=`pwd`$(dirname $0)
[ "$(print $0 | grep '^/.*')" != "" ] && bindir=$(dirname $0)

export TEXTDOMAINDIR=$bindir/../config/nls/msg
export PATH=$bindir:$PATH

export PATH=/usr/bin:/usr/sbin:/usr/ucb:$PATH
Mode=""
Summary="no"
Keep="no"
OSType=""
OSVersion=""
OSBits=""
UsrCmd=""
IgnoreAgentLogs="no"
Tables="no"
Debug="off"
[ -z "$(whoami 2>/dev/null)" ] && alias whoami='id | cut -d"(" -f2 | cut -d")" -f1'

typeset -i ver=0 vr=0

while getopts ":askigtl:h:u:" OPTS
do case $OPTS in
   a) Mode="all"
      ;;
   h) CANDLEHOME=$OPTARG
      ;;
   g) Debug="on"
      ;;
   k) Keep="yes"
      ;;
   i) IgnoreAgentLogs="yes"
      ;;
   s) Summary="yes"
      ;;
   l) Label=$OPTARG
      ;;
   t) Tables="yes"
      ;;
   u) UsrCmd=$OPTARG
      ;;
   ?) Use_report
      ;;
   esac
done

[ "$Debug" = "on" ] && set -x

# Check options for conflicts

# Asking for Tables also gets "all"
[ "$Tables" = "yes" ] && Mode="all"

# Can't (k)eep if we're not asking for (a)ll
if [ "$Keep" = "yes" -a "$Mode" = "" ]
then
   #$NLS-E$ "KEEP option not valid unless ALL is requested "
   Err_report warn "`nls_replace 'KCI0802I' KEEP ALL`"
fi

[ -n "$CANDLEHOME" ] || CANDLEHOME=$(cd $(dirname $0) ; dirname $(pwd) )
export CANDLEHOME

if [ -z "$CANDLEHOME" ]
then
  #$NLS-E$ "CANDLEHOME not specified"
  Err_report fail "`nls_replace 'KCI0803E' CANDLEHOME`"
fi

if [ $(print "$CANDLEHOME" | wc -w) -ne 1 ]
then
  #$NLS-E$ "Blanks are not permitted in CANDLEHOME"
  Err_report fail "`nls_replace 'KCI0682E' CANDLEHOME`"
fi

# Where to log system info
# Default appends summary info to the install log, "all" writes a new file
if [ "$Mode" = "all" ]
then
   OutDir=$CANDLEHOME/tmp
   #$NLS-E$ "invalid output work area: $OutDir"
   [ -d "$OutDir" ] || Err_report fail "`nls_replace 'KCI0804E' $OutDir`"

   # Initialize the work area
   DigDir=$OutDir/DIG
   rm -f $OutDir/DIG.tar*
   rm -f $OutDir/WrapUp
   rm -rf $DigDir
   mkdir $DigDir
   #$NLS-E$ "unable to create work area: $DigDir"
   [ -d "$DigDir" ] || Err_report fail "`nls_replace 'KCI0805E' $DigDir`"

   #  Initialize the output file with the date
   DigInfo=$DigDir/system.info
   print "Distributed Install Group User Profile" > $DigInfo
   #$NLS-E$ "invalid output log: $DigInfo"
   [ -w "$DigInfo" ] || Err_report fail "`nls_replace 'KCI0806E' $DigInfo`"
else
   DigInfo=$CANDLEHOME/logs/candle_installation.log
   touch $DigInfo
   #$NLS-E$ "invalid output log: $DigInfo"
   [ -w "$DigInfo" ] || Err_report fail "`nls_replace 'KCI0806E' $DigInfo`"
fi

# Get installer version
ciVFile=cienv.ver
typeset curRel="" curVer="" regFile=""

regFile=$CANDLEHOME/registry/$ciVFile
#$NLS-E$ "could not read file: $regFile"
[ -r $regFile ] || Err_report fail "`nls_replace 'KCI0807E' $regFile`"

# Get the version and release numbers.

curVer=$(grep "^ver =" $regFile | cut -d"=" -f2)
#$NLS-E$ "could not determine the version number from $regFile"
[ -n "$curVer" ] || Err_report fail "`nls_replace 'KCI0808E' $regFile`"
curRel=$(grep "^rel =" $regFile | cut -d"=" -f2)
#$NLS-E$ "could not determine the release number from $regFile"
[ -n "$curRel" ] || Err_report fail "`nls_replace 'KCI0809E' $regFile`"

# We usually capture the user command from the main scripts, unless we run
# digup manually. In that case, UsrCmd is just the current command line. 
[ -n "$UsrCmd" ] || UsrCmd="$0 $*"

# Gather summary information

print " " >> $DigInfo
print "***********************************************************" >> $DigInfo
print "Begin DigUp:: User Profile on $(date) [$Label]\n" >> $DigInfo
if [[ "$(uname -s)" = "NONSTOP_KERNEL" ]]
then
print "User      : $(whoami)\t"  >> $DigInfo
print "Host name : $(uname -n)\t Installer Lvl:$curVer /$curRel" >> $DigInfo;
else
print "User      : $(whoami)\t Group: $(groups)" >> $DigInfo
print "Host name : $(hostname)\t Installer Lvl:$curVer /$curRel" >> $DigInfo;
fi
print "CandleHome: $CANDLEHOME" >> $DigInfo
print "Command   : $UsrCmd \n" >> $DigInfo

# Additional platform specific details
print "==> uname information:\n $(uname -a)" >> $DigInfo

OSType=$(uname -s)

case "$OSType" in
   HP-UX)  OSVersion=$(uname -r)
           print "\n==> HP-UX version detail:\n $OSVersion" >> $DigInfo

           print "\n==> 32/64 Bit Architecture:" >> $DigInfo
	    
           # HPUX is 32-bit prior to HPUX 11.
	   vr=${OSVersion#*.}  # e.g. OSVersion= A.10.20 for HPUX 1020.
	   ver=${vr%.*}
	   if [ ver -lt 11 ]
	   then
	      OSBits="32 Bit (pre-HP11)"
	   fi

	   # Method-1 (works for HP11)
           if [ "$OSBits" = "" ]
           then
	      OSBits=$(getconf KERNEL_BITS 2> /dev/null)
	   fi

	   # Cop-out
           if [ "$OSBits" = "" ]
           then
	      OSBits="32 Bit (assumed)"
           fi
           print $OSBits >> $DigInfo

           print "\n==> ulimit Information:\n $(sh ulimit -a)" >> $DigInfo;

           break
           ;;
   AIX)    oslvl=$(lslpp -L bos.rte.boot | grep Boot) # boot fileset version
           oslvl=$(print $oslvl | cut -f 2 -d " ")     # Get the version number
           [ -z "$oslvl" ] && oslvl=$(oslevel)        # if fails, use oslevel
	   set -A osNum $(print $oslvl | sed "s/\./ /g") # lose 4th number
	   oslvl=${osNum[0]}.${osNum[1]}.${osNum[2]}

           OSVersion=$(print $oslvl)
	   ver=${OSVersion%.*}
           print "\n==> AIX version detail: $OSVersion" >> $DigInfo

           print "\n==> 32/64 Bit Architecture:" >> $DigInfo

	   # default is 32 bit
           OSBits="32 Bit"
	   $CANDLEHOME/bin/lsc 2>&1 |grep kernel3264|grep "64-bit" > /dev/null
           if [ $? = 0 ]
           then
              OSBits="64 Bit"
	   fi
           print $OSBits >> $DigInfo

           print "\n==> Processor information:" >> $DigInfo
           lscfg|grep Proc >> $DigInfo

	   JdkInfo=$(grep "Full version:" /usr/jdk_base/README 2> /dev/null)
	   lastcc=$?
           if [ $lastcc -gt 1 ]
	   then
	     JdkInfo="[unknown] /usr/jdk_base/README was not found"
           fi
           print "\n==> Native JDK info: $JdkInfo" >> $DigInfo

           print "\n==> ulimit Information:\n $(sh ulimit -a)" >> $DigInfo;

           break
	   ;;
         Linux) OSVersion=$(uname -r)
           # arch name is formed from "L" plus 1st two chars of hardware plus
           # 1st two digits of the kernel - ie, ls322 for OS/390 kernel 2.2.16
           os=l$(uname -m | cut -c1-2)
           ver=$(uname -r | cut -f1 -d.)$(uname -r | cut -f2 -d.)
           print "\n==> Linux kernel/distribution detail: $(uname -r)" >> $DigInfo
           print "\n==> 32/64 Bit Architecture:" >> $DigInfo
           # check for 32 or 64 bit
           # As of 4/24/2003, all in-house Intel (li624) are 32-bit
           # and OS390 are 32-bit unless uname-m gets "s390x"
           OSBits=32
           [ "$(uname -m)" = "s390x" ] && OSBits=64
           print $OSBits >> $DigInfo
           ;;
   SunOS)  OSVersion=$(uname -r)
           print "\n==> SunOS version detail: $OSVersion" >> $DigInfo

           print "\n==> 32/64 Bit Architecture:" >> $DigInfo

           # Sun is 32-bit prior to Sol 2.7
	   ver=${OSVersion%%.*}  # e.g. OSVersion= 5.6 for Solaris 2.6
	   ver=ver-3
	   rel=${OSVersion#*.}
	   rel=${rel%%.*}  # Remove any mod levels (e.g. remove .1 from 5.5.1)
	   if [ ver -lt 2 ]
	   then
	      OSBits="32 Bit (pre-Solaris 2.7)"
	   elif [ ver -eq 2 ] && [ rel -lt 7 ]
	   then
	      OSBits="32 Bit (pre-Solaris 2.7)"
	   fi

	   # Method-1
           if [ "$OSBits" = "" ]
           then
	      OSBits=$(isainfo -b 2> /dev/null)
	   fi

	   # Method-2
           if [ "$OSBits" = "" ]
           then
              OSBits=$(dmesg|egrep "Generic&32-bit|64-bit"|grep -v "not found")
	   fi

	   # Cop-out
	   if [ "$OSBits" = "" ]
	   then
              OSBits="32 Bit (assumed)"
	   fi
           print $OSBits >> $DigInfo

           print "\n==> ulimit Information:\n $(ulimit -a)" >> $DigInfo;

           break
	   ;;
  NONSTOP_KERNEL)
           break
           ;;
   *)      print "$(uname) is unknown" >> $DigInfo
	   ;;
esac

print "\nEnd DigUp:: User Profile on $(date)" >> $DigInfo
print "***********************************************************" >> $DigInfo

if [ "$Summary" = "yes" ] 
then
   if [ "$(uname -s)" != "NONSTOP_KERNEL" ]
   then
      print "Host=$(hostname)  OS=$OSType-$OSVersion  BitConfig=$OSBits"
   else
      print "Host=$(uname -n) OS=$OSType-$OSVersion  BitConfig=$OSBits"
   fi
fi

if [ "$Mode" = "all" ]
then

   # Extended site information.  All dirs are relative to Candlehome.
   cd $CANDLEHOME

   ################# Gather the candlehome directory info     
   #$NLS-E$ "...Gathering directory information"
   print "`nls_replace 'KCI0810I'`";
   ls -laRL $CANDLEHOME >> $DigDir/candlehome.info;
   find $CANDLEHOME -ls >> $DigDir/candlehome_find.info;
   
   ################# Gather the config directory
   #$NLS-E$ "...Gathering config directory";   
   print "`nls_replace 'KCI0811I'`";
   tar -cf $DigDir/config.tar ./config;
   
   ################# Gather the registry directory
   #$NLS-E$ "...Gathering registry directory"
   print "`nls_replace 'KCI0812I'`";
   tar -cf $DigDir/registry.tar ./registry;
   
   ################# Gather the log files
   #$NLS-E$ "...Gathering log files"
   print "`nls_replace 'KCI0813I'`";
   if [ "$IgnoreAgentLogs" = "no" ]
   then
     tar -cf $DigDir/logs.tar ./logs;
   else
     tar -cf $DigDir/logs.tar ./logs/candle_installation.log;
   fi
   
   #Gather the plugin log files
   tar -cf $DigDir/plugin.tar ./InstallITM/plugin/executionEvents;
   
   ################# Gather network host info
   #$NLS-E$ "...Gathering /etc/hosts information"
   print "`nls_replace 'KCI0814I' /etc/hosts`";
   cat  /etc/hosts >> $DigDir/hosts.info;
   
   ################# Gather the environment
   #$NLS-E$ "...Gathering environment information"
   print "`nls_replace 'KCI0815I'`";
   env >> $DigDir/env.info;

   ################# Gather the CMS tables directory
   if [ "$Tables" = "yes" ]
   then
     #$NLS-E$ "...Gathering tables directory"
     print "`nls_replace 'KCI0816I'`";
     tar -cf $DigDir/tables.tar ./tables/!(cicatrsq|PLACEHOLDER)
   fi
   
   ################# Gather component information
   #$NLS-E$ "...Gathering component information"
   print "`nls_replace 'KCI0817I'`";

   case "$OSType" in
     HP-UX) PSTUB=hp11 ;;
     AIX) PSTUB=aix ;;
     Linux) PSTUB=l$(uname -m | cut -c1-2) ;;
     SunOS) PSTUB=sol ;;
   esac

   export DigDir
   export Debug

   VER_FILES=`ls $CANDLEHOME/registry/??$PSTUB*.ver 2>/dev/null`

   for VF in $VER_FILES; do
     BN=`basename $VF`
     COMP=`print $BN | cut -c-2`
     CANDLEARCH=`print $BN | cut -c3- | cut -d. -f1`
     CFILE="$CANDLEHOME"/bin/"$COMP"_digup

     if [ -x "$CFILE" ]; then
       CANDLEARCH=$CANDLEARCH "$CFILE"
     fi
   done

   ################# wrap it all up in a zip file 
   #$NLS-E$ "...Creating zip file"
   print "`nls_replace 'KCI0818I'`";
   rm -f $OutDir/DIG.tar.gz 2>/dev/null
   cd $DigDir
   tar -cf $OutDir/DIG.tar *
   gzip -9 $OutDir/DIG.tar
   print "********************************************************************"
   print " $OutDir/DIG.tar.gz"
   #$NLS-E$ "This file has been created and contains diagnostic information."
   print "`nls_replace 'KCI0819I'`"
   #$NLS-E$ "Forward this binary file to your IBM Support representative."
   print "`nls_replace 'KCI0820I'`"
   print "********************************************************************"

   # Purge the working directory unless they ask to keep it
   cd $CANDLEHOME
   if [ "$Keep" = "no" ]
   then
      rm -rf $DigDir
   else
      print "cd $DigDir"                 > $OutDir/WrapUp
      print "tar -cf $OutDir/DIG.tar *" >> $OutDir/WrapUp
      print "compress $OutDir/DIG.tar"  >> $OutDir/WrapUp
      chmod 777 $OutDir/WrapUp
      print "print \"**************************************\" " >> $OutDir/WrapUp
      print "print " $OutDir/DIG.tar.Z" "  >> $OutDir/WrapUp
      print "print "This file contains diagnostic info""  >> $OutDir/WrapUp
      print "print "Fwd this file to your IBM Support rep""  >> $OutDir/WrapUp
      print "print \"**************************************\" " >> $OutDir/WrapUp
	  #$NLS-E$ "You've elected to Keep the working directory: "
      print "`nls_replace 'KCI0821I'`"
      print " $OutDir/DIG"
      print " "
      #$NLS-E$ "If you wish to edit or remove sensitive information before"
      #$NLS-E$ "forwarding to your IBM Support representative, you should"
      #$NLS-E$ "do so by modifying the contents of the above working directory."
      print "`nls_replace 'KCI0822I'`"
	  
	  print " "
      #$NLS-E$ "When completed, use the command:"
      #$NLS-E$ " $OutDir/WrapUp"
      #$NLS-E$ " "
      #$NLS-E$ "which will complete the job of packaging the directory and files."
      print "`nls_replace 'KCI0823I' $OutDir/WrapUp '\n'`"
	  print "*****************************************************************"
   fi

fi
