#!/bin/ksh

# *************************************************************************
#
# Licensed Materials - Property of IBM
# (C) Copyright IBM Corp. 2006, 2008  All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# Version = 1.14
#
# *************************************************************************

TMPDIR=/tmp
CANLOGOPT=Y
HISTOPT=Y
DBOPT=Y
typeset -i rc=0
OPSYS=`uname`
NOPROMPT=N
case $OPSYS in
  Linux) DASHE=-e
         TAROPT=
         XARGSOPT="-i -s 65536" ;;
  AIX)   DASHE=
         TAROPT=
         XARGSOPT="-I {} -s65536" ;;
  SunOS) DASHE=
         TAROPT=E
         XARGSOPT="-I {} -s 2048" ;;
    *)   DASHE=
         TAROPT=N
         XARGSOPT="-I {} -s 2048" ;;
esac

PGMNAME=$0
OPTLIST=$*

while [ -n "$1" ]
do

P=`echo $1 | sed "-es/ /_/g"`
if [ "$P" = "${P#-}" ]
then
# not an option
TMPDIR=$P
else
# option
typeset -l O=$P
case $O in
 -[h?]) rc=4
       break ;;
 -help) rc=4
       break ;;
 -nohist) HISTOPT=N ;;
 -nodb) DBOPT=N ;;
 -nologs) CANLOGOPT=N ;;
 -noapp) ;;
 -nosec) ;;
 -nosys) ;;
 -noevent) ;;
 -noprompt) NOPROMPT=Y ;;
 -candlehome) cd "$2"
       if [ $? = 0 ]
       then
        CANDLEHOME=`pwd`
        cd - >/dev/null 2>/dev/null
       else
        echo Directory $2 does not exist
        rc=12
       fi
       shift ;;
 *) echo Option "$1" is not recognized
       rc=16
       break ;;
esac
fi

shift

done

if [ $rc = 0 ]
then

cd "$TMPDIR" 1>/dev/null 2>/dev/null
if [ $? != 0 ]
then
 while [ ! -d "$TMPDIR" ]
 do
  if [ "$NOPROMPT" = "N" ]
  then
   echo Directory $TMPDIR does not exist. Would you like to create it? If so, enter
   echo "Y" to continue processing. Otherwise, enter "N" to stop processing:
   ANS="."
   read ANS
  else
   ANS="Y"
  fi
  if [ "$ANS" = "N" -o "$ANS" = "n" ]
  then
   rc=20
   break
  elif [ "$ANS" = "Y" -o "$ANS" = "y" ]
  then
   mkdir -p "$TMPDIR"
   if [ $? = 0 ]
   then
    cd "$TMPDIR"
    TMPDIR=`pwd`
    cd - >/dev/null 2>/dev/null
    rc=0
   else
    echo Unable to create directory $TMPDIR
    rc=24
   fi
   break
  fi
 done
 else
  TMPDIR=`pwd`
  if [ "$TMPDIR" = "/" ]
  then
   echo Do not specify the root directory as the work directory
   rc=8
  fi
  cd - >/dev/null
 fi
fi

if [ $rc = 0 ]
then

PROD_HOME=${CANDLEHOME:-`dirname $PGMNAME`/..}
cd $PROD_HOME
PROD_HOME=`pwd`
echo ITM installation directory is $PROD_HOME
cd - >/dev/null

DESTNAME=pdcollect-`hostname`
DESTNAME=${DESTNAME%%\.*}
DESTDIR=$TMPDIR/$DESTNAME

echo $TMPDIR | grep "$PROD_HOME" >/dev/null
if [ $? -eq 0 ]
then
echo The work directory may not be the same as the ITM install directory or
echo a subdirectory of the ITM install directory
exit 28
fi

[ -a $DESTDIR ] && rm -fr $DESTDIR
[ -f $DESTDIR.tar.Z ] && rm -f $DESTDIR.tar.Z
mkdir -p $DESTDIR
cd $DESTDIR

echo $PGMNAME version 1.14 running at `date` on system `hostname` by `logname` >manifest.txt
echo Parameters: $OPTLIST >>manifest.txt

echo Collecting environment information...
echo Collecting environment information... >>manifest.txt
echo CANDLEHOME=$PROD_HOME >dir.info
ls -laRL $PROD_HOME >>dir.info
env > env.info
id >> env.info
if [ $OPSYS = "HP-UX" ]
then
ulimit > ulimit.info
else
ulimit -a > ulimit.info
fi

echo Collecting system information...
echo Collecting system information... >>manifest.txt
case $OPSYS in
 SunOS) prtconf > system.info ;;
 AIX) prtconf > system.info
      lscfg  >> system.info ;;
 Linux) echo No prtconf on Linux > system.info ;;
 *) : ;;
esac

echo Collecting maintenance information...
echo Collecting maintenance information... >>manifest.txt
case $OPSYS in
 SunOS) pkginfo > maint.info ;;
 AIX) lslpp -L > maint.info ;;
 HP-UX) swlist > maint.info ;;
 Linux) rpm -qa > maint.info ;;
 *) echo Unrecognized OS: $OPSYS > maint.info ;;
esac
if [ -x $PROD_HOME/bin/cinfo ]
then
$PROD_HOME/bin/cinfo -i  >cinfo.info
$PROD_HOME/bin/cinfo -R >>cinfo.info
else
echo $PROD_HOME/bin/cinfo not found >cinfo.info
fi

echo Collecting disk space information...
echo Collecting disk space information... >>manifest.txt
if [ $OPSYS = "AIX" ]
then
df > disk.info
else
df -l > disk.info
fi

echo Collecting network information...
echo Collecting network information... >>manifest.txt
case $OPSYS in
 Linux) /sbin/ifconfig -a > ipconfig.info ;;
 HP-UX) [ -a ipconfig.info ] && rm -f ipconfig.info
        for d in 0 1 2 3 4 5 6 7
         do
          ifconfig lan$d >>ipconfig.info 2>&1
         done
        ifconfig lo0 >>ipconfig.info 2>&1 ;;
 *) ifconfig -a > ipconfig.info ;;
esac
netstat -a -n > netstat.info
netstat -a -r >> netstat.info
if [ $OPSYS = "AIX" ]
then
cp -p /etc/netsvc.conf .
else
cp -p /etc/nsswitch.conf .
fi
cp -p /etc/hosts .
cp -p /etc/resolv.conf .

if [ $OPSYS = "SunOS" ]
then
[ -a /usr/sbin/pooladm ] && /usr/sbin/pooladm >zone.info 2>&1
[ -a /usr/sbin/zoneadm ] && /usr/sbin/zoneadm list -p >>zone.info 2>&1
[ -a /usr/sbin/prstat ] && /usr/sbin/prstat -Z 1 1 >>zone.info 2>&1
if [ -d /etc/zones ]
then
mkdir $DESTDIR/zones
cp -pr /etc/zones/* $DESTDIR/zones
fi
fi

echo Collecting running process information...
echo Collecting running process information... >>manifest.txt
ps -ef >tasklist.info

if [ "$CANLOGOPT" != "N" ]
then
echo Collecting log files...
echo Collecting log files... >>manifest.txt
if [ -d $PROD_HOME/logs ]
then
cd $PROD_HOME
find . -name logs -type d -print >/tmp/logfile.lst
xargs $XARGSOPT mkdir -p $DESTDIR/logs/{} </tmp/logfile.lst
xargs $XARGSOPT /bin/ksh -c "cp -pr {}/* $DESTDIR/logs/{}" </tmp/logfile.lst
rm -f /tmp/logfile.lst
else
echo Unable to locate $PROD_HOME/logs subdirectory
echo Unable to locate $PROD_HOME/logs subdirectory >>manifest.txt
fi

echo Collecting OpsLog files...
echo Collecting OpsLog files... >>manifest.txt
if [ -d $PROD_HOME/itmLogs ]
then
cp -pr $PROD_HOME/itmLogs $DESTDIR/itmLogs
else
echo Unable to locate $PROD_HOME/itmLogs subdirectory
echo Unable to locate $PROD_HOME/itmLogs subdirectory >>manifest.txt
fi

cd $PROD_HOME
[ -f /tmp/IBM_AppSupport_Install.log ] && cp -p /tmp/IBM_AppSupport_Install.log $DESTDIR/logs
[ -d $PROD_HOME/patchlogs ] && cp -rp $PROD_HOME/patchlogs $DESTDIR/logs
case $OPSYS in
 AIX) find . -name \*.log -print | grep "^./aix[0-9][0-9][0-9]/" >/tmp/logfile.lst ;;
 Linux) find . -name \*.log -print | grep "^./li[0-9]*/" >/tmp/logfile.lst ;;
esac

if [ -s /tmp/logfile.lst ]
then
xargs $XARGSOPT /bin/ksh -c "[ -d $DESTDIR/\`dirname {}\` ] 2>/dev/null || mkdir -p $DESTDIR/\`dirname {}\`" </tmp/logfile.lst
xargs $XARGSOPT /bin/ksh -c "cp -p {} $DESTDIR/\`dirname {}\`" </tmp/logfile.lst
fi
[ -a /tmp/logfile.lst ] && rm -f /tmp/logfile.lst
cd $DESTDIR
fi

set -A VL `fgrep -h RKDSCATL *.config | sed -e "s/^RKDSCATL='//" -e "s/\/RKDSCATL'$//" -e "s/\/\//\//g" | sort -u`
if [ -n "${VL[0]}" ]
then
if [ ${#VL[*]} -gt 1 ]
then
echo Multiple hub directories found
echo Multiple hub directories found >>manifest.txt
if [ "$NOPROMPT" = "N" ]
then
PS3="Select the correct hub directory:"
select HUB_DIR in ${VL[*]}
do
if [ -z "$HUB_DIR" ]
then
echo Selection out of range
else break
fi
done
else
echo Unable to prompt, using first directory
echo Unable to prompt, using first directory >>manifest.txt
HUB_DIR=${VL[0]}
fi
else
HUB_DIR=${VL[0]}
fi
HUB_NAME=`basename $HUB_DIR`
echo Hub subdirectory is $HUB_DIR
echo Hub subdirectory is $HUB_DIR >>manifest.txt
echo Hub name is $HUB_NAME
echo Hub name is $HUB_NAME >>manifest.txt

if [ "$DBOPT" != "N" ]
then
echo Collecting database files...
echo Collecting database files... >>manifest.txt
mkdir -p $DESTDIR/$HUB_NAME
cp -p $HUB_DIR/QA1*.DB  $DESTDIR/$HUB_NAME
cp -p $HUB_DIR/QA1*.IDX $DESTDIR/$HUB_NAME
fi

if [ "$HISTOPT" != "N" ]
then
echo Collecting history files...
echo Collecting history files... >>manifest.txt
if [ -d $HUB_DIR ]
then
[ ! -d $DESTDIR/$HUB_NAME ] && mkdir -p $DESTDIR/$HUB_NAME
cd $HUB_DIR
find . \( -type d ! -name . -prune \) -o \( -name \*.hdr -print \) | xargs $XARGSOPT basename {} .hdr | xargs $XARGSOPT cp -p {} {}.hdr $DESTDIR/$HUB_NAME 2>/dev/null
cd $DESTDIR
if [ -a $HUB_DIR/RKCF*.* ]
then
cp -p $HUB_DIR/RKCF*.*   $DESTDIR/$HUB_NAME
else
echo No RKCF*.* files found in $HUB_DIR
fi
else
echo No history files found...
echo No history files found... >>manifest.txt
fi
fi

else
echo Unable to determine subdirectory for DB files
echo Unable to determine subdirectory for DB files >>manifest.txt
fi

mkdir -p $DESTDIR/conf
find $PROD_HOME -name \*env         -print | xargs $XARGSOPT cp -p {} $DESTDIR/conf
find $PROD_HOME -name \*.ini        -print | xargs $XARGSOPT cp -p {} $DESTDIR/conf
find $PROD_HOME -name \*.properties -print | xargs $XARGSOPT cp -p {} $DESTDIR/conf
find $PROD_HOME -name \*.config     -print | xargs $XARGSOPT cp -p {} $DESTDIR/conf
find $PROD_HOME -name \*.ctx        -print | xargs $XARGSOPT cp -p {} $DESTDIR/conf

echo >>manifest.txt
echo File list at end of collection phase: >>manifest.txt
ls -Rla >>manifest.txt

if [ "$NOPROMPT" = "N" ]
then
 echo Prompting user for edits and additional files... >>manifest.txt
 echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 echo Data collection is complete.
 echo
 echo You will now be given an opportunity to examine the files, edit them to
 echo remove information that you do not want to expose to IBM, or add additional
 echo files to the set.
 echo
 echo Enter "exit" when you are finished.
 echo
 echo Any files which are left will be compressed in preparation for
 echo transmission to IBM.
 echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 echo

 export PS1="Type \\\"exit\\\" when done>"
 /bin/ksh
fi

echo Packing collected data...
echo Packing collected data... >>manifest.txt
cd $DESTDIR
if [ $OPSYS = "Linux" ]
then
tar -c${TAROPT}zf ../$DESTNAME.tar.Z *
else
tar -c${TAROPT}f - * | compress -c >../$DESTNAME.tar.Z
fi
cd ..
rm -fr $DESTDIR

echo
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo The file
echo $DESTDIR.tar.Z
echo contains the collected diagnostic information.
echo Forward this binary file to your IBM Support representative.
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo

elif [ $rc = 4 ]
then

echo
echo This program is intended to collect diagnostic information from the
echo computer on which it is executed. Collected data includes:
echo
echo $DASHE "\t"- system configuration
echo $DASHE "\t"- environment
echo $DASHE "\t"- disk space information
echo $DASHE "\t"- network state
echo $DASHE "\t"- configuration and log files
echo
echo Command line syntax is:
echo
echo $DASHE "\t" `basename $PGMNAME` [options] [work-directory]
echo
echo where:
echo
echo $DASHE "\twork-directory" is the directory to be used to collect the data.
echo
echo $DASHE "\t\t" DO NOT specify the root directory as the work-directory.
echo $DASHE "\t\t" If not specified, /tmp will be used.
echo
echo $DASHE "\toptions" are any of the following, in any order or case:
echo
echo $DASHE "\t\t-candlehome dir\t" specifies the name of the ITM install directory
echo $DASHE "\t\t\t\t" Note that the directory name is case-sensitive.
echo
echo $DASHE "\t\t-nohist\t" specifies that history files are NOT to be collected
echo
echo $DASHE "\t\t-nologs\t" specifies that ITM log/trace files are NOT to be collected
echo
echo $DASHE "\t\t-nodb\t" specifies that ITM database files are NOT to be collected
echo
echo $DASHE "\t\t-noprompt\t" specifies that all interaction with the user is suppressed
echo

fi
exit $rc
