#!/usr/bin/ksh
# /* +----------------------------------------------------------------+ */
# /* ! Licensed Materials - Property of IBM                           ! */
# /* !                                                                ! */
# /* ! "Restricted Materials of IBM"                                  ! */
# /* !                                                                ! */
# /* ! 5621-290, 5765-270, 5622-615, 5765-468, 5697-216               ! */
# /* !                                                                ! */
# /* !   Copyright IBM Corp. 1993, 1997  All Rights Reserved.         ! */
# /* !                                                                ! */
# /* ! US Government Users Restricted Rights - Use, duplication or    ! */
# /* ! disclosure restricted by GSA ADP Schedule Contract with        ! */
# /* ! IBM Corp.                                                      ! */
# /* +----------------------------------------------------------------+ */
#
# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* #
#                                                                           #
# Collecting FlowMark trace and related information.                        #
#                                                                           #
# This shell script should run after a problem with FlowMark occurs.        #
# All related information will be put together and compressed in one file.  #
#                                                                           #
# ------------------------------------------------------------------------- #
#                                                                           #
# You can custimze the general behavior of the shell script by modifying    #
# the switches which control the information to be collected.               #
#                                                                           #
# The default values for the above specified options can be modified in the #
# following section. Allowed values are yes and no and ff for future use.   #
# Using an option will change the default.                                  #
#                                                                           #
# *-*-*-*-*-*-*-*-*-*-* #                                                   #
ord='/tmp/'`hostname`   #                                                   #
                        #  h  Display help information (no optarray enrty)  #
    optarray[1]='no'    #  a  Attaching to processes                        #
    optarray[2]='no'    #  c  Analyse core files                            #
    optarray[4]='ans'   #  p  Change process component id default (exm)     #
    optarray[5]='no'    #  F  FlowMark information is included              #
    optarray[6]='no'    #  A  Audit trail information is included           #
    optarray[7]='no'    #  T  Trace information is included                 #
    optarray[8]='no'    #  o  ObjectStore information is included           #
    optarray[9]='no'    #  s  Collecting system information                 #
   optarray[10]='no'    #  z  Compressing results                           #
   optarray[11]='no'    #  f  Use the specific FlowMark profile             #
   optarray[12]=$ord    #  d  changing directory for results                #
   optarray[13]='no'    #  m  display menu                                  #
   optarray[14]='fu'    #     for future use                                #
                        #                                                   #
# *-*-*-*-*-*-*-*-*-*-* #       Defaults:  no  = actual set to no           #
#                                          yes = actual set to yes          #
#                                      'value' = defined value              #
#                                          ans = actual not supported       #
# End of customization area                fu  = reserved for future ues    #
# *-*-*-*-*-*-*-*-*-*-* #                                                   #
    optarray[3]='no'    #     Collecting dbx results (set if 1 or 2 = yes ) #
  arraymax='13'         #                                                   #
# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* #
# ------------------------------------------------------------------------  #
# ------------------------------------ do not touch the following source -  #
# ------------------------------------------------------------------------  #
# Author ..........:  E. Voesch  - Member of the FlowMark team
# Created at ......:  December 1997
# Last modification:
#      date .......:  21.Jan 1998
#      reason .....:  Handle missing TelePath if FlowMark is active
# ------------------------------------------------------------------------  #

dsp_help () {
if [[ $1 != 'noclear' ]]; then
#   clear
   echo ' ----------------------------------------------------------------------
   echo ' Defined options and their meaning:
fi
echo '                                                                        '
echo ' opt. parameter   description                                  default  '
echo ' ---------------------------------------------------------------------- '
echo '  -h              Display help information                       -      '
echo '                                                                        '
echo '  -a              Attaching to processes                        '${optarray[1]}
echo '  -c              Analyse core files                            '${optarray[2]}
# -ans- echo '  -p [compid]     Change process component id default (exm)     '${optarray[4]}
#       echo '                  If specified a [compid] is required
echo '                                                                        '
echo '  -F              FlowMark information is included              '${optarray[5]}
echo '  -A              Audit trail information is included           '${optarray[6]}
echo '  -T              Trace information is included                 '${optarray[7]}
echo '  -o              ObjectStore information is included           '${optarray[8]}
echo '  -s              Collecting system information (all)           '${optarray[9]}
echo '                                                                        '
echo '  -m              Display menu                                  '${optarray[13]}
echo '  -d              Directory for results                         '${optarray[12]}
echo '  -z              Compressing results                           '${optarray[10]}
echo '  -f [file]       Use the specific FlowMark profile             '${optarray[11]}
echo ' ---------------------------------------------------------------------  '
echo ' If you specify an option and the default will be changed               '
echo ' ---------------------------------------------------------------------  '
}

# ===========================================================================
# ---------------------------------------------------------------------------
# Option handling incl the needed function
# ---------------------------------------------------------------------------

chdefault () {
   case ${optarray[$1]} in
     no)  optarray[$1]='yes'
          return             ;;
     yes) optarray[$1]='no'
          return             ;;
     fu)  optarray[$1]='fu'
          return             ;;
     ans) clear
          echo ' This option is actual not supported. Please try another one.'
          dsp_help "noclear"
          exit               ;;
   esac
   echo 'Invalid default for option customized. Please check the shell script.'
   exit
}

## -ans-         p
while getopts :acf:FATozmsd:h optname
do
    case $optname in
      a)   chdefault 1  ;;    #  Attaching to processes
      c)   chdefault 2  ;;    #  Analyse core files
# autoset  chdefault 3  ;;    #  Collecting the results from dbx analysis
      p)   chdefault 4
           compid=$OPTARG ;;  #  Change process component id default (exm)
      F)   chdefault 5  ;;    #  FlowMark information is included
      A)   chdefault 6  ;;    #  Audit trail information is included
      T)   chdefault 7  ;;    #  Trace information is included
      o)   chdefault 8  ;;    #  ObjectStore information is included
      s)   chdefault 9  ;;    #  Collecting system information
      z)   chdefault 10 ;;    #  Compressing results
      f)   chdefault 11
           fmprof=$OPTARG ;;  #  Use the specific FlowMark profile
      d)   optarray[12]=$OPTARG ;;
                              #  Specify another result directory
      m)   chdefault 13 ;;    #  Display menu
      h)   dsp_help           # Display help information
           exit           ;;
      :)   echo 'missing value for '$optname
           dsp_help "noclear"
           exit  ;;
      ?)   echo 'unknown option specified'
           dsp_help "noclear"
           exit  ;;
    esac
done
shift $(($OPTIND -1))

# ---------------------------------------------------------------------------
# Menus (incl initial value specifcation)
# ---------------------------------------------------------------------------

clear
echo "+-------------------------------------------------------------------+"
echo "  Initialization - this may take some seconds - please be patient    "
echo "+-------------------------------------------------------------------+"

oslevel | grep 4.1 > /dev/null
oslevel_rc=$?
sysvalue[1]=' -A'
sysvalue[2]=' -D'
sysvalue[3]=' -f'
sysvalue[4]=' -g'
sysvalue[5]=' -G'
                                   # operating system level is not 4.1
if [[ $oslevel_rc > 0 ]]; then
  sysvalue[6]=' -i'
else
  sysvalue[6]=' '
fi
sysvalue[7]=' -k'
sysvalue[8]=' -l'
sysvalue[9]=' -n'
sysvalue[10]=' -p'
sysvalue[11]=' -s'
sysvalue[12]=' -S'
sysvalue[13]=' -t'
snapvalue=''

i=1
until (( $i > 13 ));
do
  sysarray[$i]=${optarray[9]}
  (( i=i+1 ))
done

sysimenu () {
clear
echo "+-------------------------------------------------------------------+"
echo "  Specify System Information Collection Options                      "
echo "+-------------------------------------------------------------------+"
echo "   1)  Asynchronous TTY information                 "${sysarray[1]}
echo "   2)  Dump and /unix information                   "${sysarray[2]}
echo "   3)  File system information                      "${sysarray[3]}
echo "   4)  Exact operating system environment           "${sysarray[4]}
echo "   5)  Predefined Object Data Manager files         "${sysarray[5]}
echo "   6)  Kernel information                           "${sysarray[7]}
echo "   7)  Programming language information             "${sysarray[8]}
echo "   8)  Network File System information              "${sysarray[9]}
echo "   9)  Printer information                          "${sysarray[10]}
echo "  10)  System Network Architecture information      "${sysarray[11]}
echo "  11)  Security files information                   "${sysarray[12]}
echo "  12)  TCP/IP information                           "${sysarray[13]}
if [[ $oslevel_rc > 0 ]] ; then
  echo "  13)  Installation debug vital product data        "${sysarray[6]}
fi
echo "                                                    "
echo "  90)  Previous menu                                "
echo "                                                    "
echo "   0)  Accept specified values and start collection "
echo "                                                    "
echo "+-------------------------------------------------------------------+"
if [[ $invsel != '' ]];then
   echo "  "$invsel
   echo "+-------------------------------------------------------------------+"
fi
read sel01
}

chsysdefault () {
   case ${sysarray[$1]} in
     no)  sysarray[$1]='yes'
          return             ;;
     yes) sysarray[$1]='no'
          return             ;;
   esac
}

msel01 () {
sel01='x'
until [[ $sel01 = '0' ]]
do
   sysimenu
   invsel=''
   case $sel01 in
      0)  sel='0'         ;;
      1)  chsysdefault 1  ;;
      2)  chsysdefault 2  ;;
      3)  chsysdefault 3  ;;
      4)  chsysdefault 4  ;;
      5)  chsysdefault 5  ;;
      6)  chsysdefault 6  ;;
      7)  chsysdefault 7  ;;
      8)  chsysdefault 8  ;;
      9)  chsysdefault 9  ;;
     10)  chsysdefault 10 ;;
     11)  chsysdefault 11 ;;
     12)  chsysdefault 12 ;;
     13)  chsysdefault 13 ;;
     90)  sel01='0'       ;;
      *)  invsel='Invalid selection specified - try again' ;;
   esac
done
}

mainmenu () {
clear
echo "+-------------------------------------------------------------------+"
echo "  Analysis and Collecting FlowMark Related Data  -  Main Menu        "
echo "+-------------------------------------------------------------------+"
echo "   1)  Attaching to processes                       "${optarray[1]}
echo "   2)  Analyse core files                           "${optarray[2]}
echo "   3)  Include FlowMark information                 "${optarray[5]}
echo "   4)  Include Audit Trail information              "${optarray[6]}
echo "   5)  Include trace information                    "${optarray[7]}
echo "   6)  Include ObjectStore information              "${optarray[8]}
echo "   7)  Compressing the results                      "${optarray[10]}
echo "                                                    "
echo "  10)  Specify System Information Collection Options"
##echo "  20)  Define granularity of analysis with dbx      "
echo "                                                    "
echo "  99)  Exit program                                 "
echo "                                                    "
echo "   0)  Accept defined values and start collection   "
echo "                                                    "
echo "+-------------------------------------------------------------------+"
if [[ $invsel != '' ]];then
   echo "  "$invsel
   echo "+-------------------------------------------------------------------+"
fi
read sel
}

if [[ ${optarray[13]} = 'yes' ]]; then
   sel='x'
   until [[ $sel = '0' ]]
   do
      mainmenu
      invsel=''
      case $sel in
         0)  ;;
         1)  chdefault 1  ;;
         2)  chdefault 2  ;;
         3)  chdefault 5  ;;
         4)  chdefault 6  ;;
         5)  chdefault 7  ;;
         6)  chdefault 8  ;;
         7)  chdefault 10 ;;
        10)  msel01       ;;
##     20)  msel02       ;;
        99)  exit         ;;
         *)  invsel='Invalid selection specified - try again' ;;
      esac
   done
fi

i=1
until (( $i > 13 ))
do
  if [[ ${sysarray[$i]} = 'yes' ]]; then
    snapvalue=$snapvalue${sysvalue[$i]}
  fi
  (( i=i+1 ))
done
if [[ $snapvalue = '' ]]; then
   optarray[9]='no'
else
   optarray[9]='yes'
fi

# ---------------------------------------------------------------------------
# Set collecting dbx output
# ---------------------------------------------------------------------------
if [[ ${optarray[1]} = 'yes' || ${optarray[2]} = 'yes' ]]; then
   if [[  ${optarray[3]} = 'no' ]]; then
      chdefault 3
   fi
fi

# ===========================================================================
# ---------------------------------------------------------------------------
# Internal customization area
# ---------------------------------------------------------------------------
            dispm='yes'                    # Message display mode (yes or no)
           curdir="."                      # Set current directory
           pwddir=`pwd`
           resdir=${optarray[12]}'/EXMresults'        # Set directory for results
          snapdir=${optarray[12]}'/EXMresults/snap'
          workdir=$curdir'/t'$$            # Set working directory
           maxbak=10                       # Maximum of managed "bak" versions
          logname='EXMcollect.log'
        colexmlog=$resdir/$logname         # File containing the log information
          tarfile=$resdir'/EXMcollect.tar' # tar file name
   chk_socket_pgm='exmpzshm'               # Socket checking program
# ---------------------------------------------------------------------------
# Some temporarly used names and definitions
# ---------------------------------------------------------------------------
       procname=$0                         # Provide this procedure name in general
      exmonitor=$workdir'/exmonitor.sh'    # Monitor shell name
  exmpzchk_outd='exmpzchk.outd'            # Checker output file (option -d)
  exmpzchk_outs='exmpzchk.outs'            # Checker output file (option -s)
  exmpzchk_outi='exmpzchk.outi'            # Checker output file (option -i)
       exmlinks='exmlinks.found'           # Additional specified links
     exminstfix='exminstfix.out'           # Query installed fixes
      exmdgnout='exmpzdgn.out'             # Output from exmpzdgn
           dnum=0                          # duplicate counter
  exm_proc_list='exmprocess.list'          # Running FlowMark process list
      exmlocale='exmlocale.info'           # NLS - System locale information
 tpexist_inlist='yes'                      # Default (reset if required)
# ===========================================================================
# ---------------------------------------------------------------------------
# Check space of working directory
# ---------------------------------------------------------------------------
if [[ ! -d ${optarray[12]} ]] ; then
   mkdir ${optarray[12]}
   if [[ $? > 0 ]];then
      echo 'Invalid directory for results specified'
      exit
   fi
fi

if (( `df -k ${optarray[12]} | grep / | awk '{print $3}'` < 8000 )); then
   echo 'There may be not enough space in the '${optarray[12]}' directory'
   echo 'For this tool you need at least 8MB free space'
   exit
fi

# ---------------------------------------------------------------------------
# Used messages
# ---------------------------------------------------------------------------
col_exm_msg[1]='----------------------------------------------------------------------'
col_exm_msg[2]='======================================================================'
col_exm_msg[3]='++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
col_exm_msg[4]='  test line '
col_exm_msg[5]='========== end of dbx summary information ======'
col_exm_msg[6]='No working directory defined - severe error'
col_exm_msg[7]='No option specified for running dbx'
col_exm_msg[8]='Output of exmpzchk has not been created'
col_exm_msg[9]='Socket checking can not be executed. Please check.'
col_exm_msg[10]=' '             # Used as one blank
col_exm_msg[11]='No valid ObjectStore server definition with this profile'
col_exm_msg[12]='Problem during finding the ObjectStrore transaction log file'
col_exm_msg[13]='ObjectStore settings from the environment'
col_exm_msg[14]='Transaction log is loacted in: '
col_exm_msg[15]='ObjectStore server output file found: ( /tmp/ostore/oss_out )'
col_exm_msg[16]='OjectStore server output file not found: ( /tmp/ostore/oss_out )'
col_exm_msg[17]='ObjectStore cache manager output file found: ( /tmp/ostore/osc4_out )'
col_exm_msg[18]='ObjectStore cache manager output file not found: ( /tmp/ostore/osc4_out } '
col_exm_msg[19]='Sending signal to dbx process '
col_exm_msg[20]='Compressing and collecting ...'
# ........  21 ... see before 38 ....
col_exm_msg[22]='No trace files found.'
col_exm_msg[23]='Profile specification is invalid'
col_exm_msg[24]='Profile found'
col_exm_msg[25]='No valid FlowMark profile found'
col_exm_msg[26]='Your settings for the "hostname" do not match with "uname -n"'
col_exm_msg[27]='No audit files found'
col_exm_msg[28]='Nothing collected during this step'
col_exm_msg[29]='No FlowMark log file found'
col_exm_msg[30]='End of analyzing and collecting FlowMark related service data.'
col_exm_msg[31]='Due to missing FlowMark profile - resetting option or default:'
col_exm_msg[32]='Due to space limitation a result file was compressed:'
col_exm_msg[33]='Due to missing space the result file was NOT compressed:'
col_exm_msg[34]='dbx is not installed - no analysis performed'
col_exm_msg[35]='The tar command returns: '
col_exm_msg[36]='The results are stored in: '
col_exm_msg[37]='There is not enough space in:'
col_exm_msg[21]='Collecting ................:'
col_exm_msg[38]='............ from directory:'
col_exm_msg[39]='Searching for additionl specified links'
col_exm_msg[40]='Severe error profile not found'
col_exm_msg[41]='From process list used profile: '
col_exm_msg[42]='Profile identified by checker'
col_exm_msg[43]='Default profile used from /usr/lpp/exm/bin '
col_exm_msg[44]='No profile could be found '
col_exm_msg[45]='Default profile used due to invalid specification :'
col_exm_msg[46]='Collecting system locale information'
col_exm_msg[47]='Collection of system locale information failed'
col_exm_msg[48]='Profile option used during FlowMark startup'
col_exm_msg[49]='Analyzing process list. Please wait'
col_exm_msg[50]='No profile option used during FlowMark startup'
col_exm_msg[51]='Number of processes started with the profile option is: '
col_exm_msg[52]='Different profiles used ... '
col_exm_msg[53]='Specified snap options: '
col_exm_msg[54]='No results generated with instfix'
col_exm_msg[55]='No results generated with exmpzdgn'
col_exm_msg[56]='System collection not successful '
col_exm_msg[57]='Not enough space in '
col_exm_msg[58]='+ ==> No core file found in the current directory: '
col_exm_msg[59]='+ ==> Please start from the directory where the core file is located.'
col_exm_msg[60]='FlowMark processes active but no active TelePath found.'
col_exm_msg[61]='  -> Therefor no FlowMark checking routines are performed.'
col_exm_msg[62]='+ ==> The located core file is invalid or is corrupted: '

# ===========================================================================
# ---------------------------------------------------------------------------
# General used functions
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# Backup of old output files e.g. the collection log file
# ---------------------------------------------------------------------------
#   function call:  col_bak
#   parameter....:  $1            contains the name of the file to backup

col_bak () {
      if [[ -f $1.bak$maxbak ]]
      then
         echo 'You have '$maxbak' saved versions'
         exit
      fi
      (( i=$maxbak-1 ))
      while (( i > 0 ))
      do
         (( j=i+1 ))
         if [[ -f $1.bak$i ]]
         then
            mv $1.bak$i $1.bak$j
         fi
         (( i=i-1 ))

      done
      mv $1 $1.bak1
}

# ---------------------------------------------------------------------------
# Displaying and recording messages
# ---------------------------------------------------------------------------
#   function call: col_msg
#   parameter....: $1            controls special character lines in the log
#                  $2            message id used as array index
#                  $3            msgvar
#                  $4            optional output file to redirect messages

col_msg () {
      if [[ $4 = '' ]]
      then
         msgout=$colexmlog
      else
         msgout=$4
      fi
      case $1 in
         bla_line )
             echo ${col_exm_msg[10]} >> $msgout ;;
         min_line )
             echo ${col_exm_msg[1]}  >> $msgout ;;
         equ_line )
             echo ${col_exm_msg[2]}  >> $msgout ;;
         plu_line )
             echo ${col_exm_msg[3]}  >> $msgout ;;
      esac
      if [[ $# > 1 ]]; then
         echo ${col_exm_msg[$2]} $3  >> $msgout
         if [[ $dispm = 'yes' ]]
         then
            echo ${col_exm_msg[$2]} $3
         fi
      fi
}

# ---------------------------------------------------------------------------
# Divides a given string into an array and returns the array index
# ---------------------------------------------------------------------------
#   function call:  divide_string
#   parameter....:  $1            contains the string to be divided
#                   $2            special field separator (optional)
divide_string () {
   if [[ $1 = '' ]]
   then
      return 999             # invalid function call
   fi
   if [[ $2 = '' ]]
   then
      ifsset='no'
   else
      IFS_ORG=$IFS
      IFS=$2
      ifsset='yes'
   fi

   set $1
   maxindex=$#
   i=0
   while (( $i < $maxindex ))
   do
      (( i=i+1 ))
      eval stringword[$i]=\$$i
   done

   if [[ $ifsset = 'yes' ]]
   then
      IFS=$IFS_ORG
   fi
   return $maxindex
}


# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
#   function call:  get_fn
#   parameter....:  $1            contains the string to be evaluated

get_fn () {
   if [[ $1 = '' ]]
   then
      return 999            # invalid function call
   fi
# analyse first character
char[1]='/'     #    /       : explizit path specified
char[2]='./'    #    ./      : relative path specified
char[3]='../'   #    ../     : navigate to parent directory
string_dir=''   #    nothing : only a filename
#
   i=1
##for##   for i in 1 2 3
##for##   do
      if [[ ${char[$i]} = `echo $1 $i | awk '{print substr($1,$2,1)}'` ]]
      then
         string_dir=${char[$i]}
      fi
##for##      (( i=i+1 ))
##for##   done

   divide_string $1 /

   i=1
   while (( $i < $maxindex ))
   do
      if [[ ${stringword[$i]} = '' ]]
      then
         (( i=i+1 ))
      else
         string_dir=$string_dir${stringword[$i]}
         (( i=i+1 ))
         if [[ $i < $maxindex ]]
         then
            string_dir=$string_dir/
         fi
      fi
   done
}

# ---------------------------------------------------------------------------
# Check for free space
# ---------------------------------------------------------------------------
#   function call: check_space
#   parameter....: $1            file name to be checked for enough space
#                  $2            location (filesystem)
check_space ()
{
      free_space=`df -k $2 | grep / | awk '{print $3}'`
      need_space=`ls -s $1 | awk '{print $1}'`
      if (( $free_space > $need_space )) ; then
         return 0
      else
         compress $1
         need_space=`ls -s $1.Z | awk '{print $1}'`
         if (( $free_space > $need_space )) ; then
            return 1
         else
            compress -d $1
            return 9
         fi
      fi
}
# ---------------------------------------------------------------------------
# Taring subroutine
# ---------------------------------------------------------------------------
#   function call: taring
#   parameter....: $1            tar options
#                  $2            tarfile
#                  $3            object to be tared
#                  $4            subdirectory
#                  $5            duplicate switch/directory
taring ()
{
      if [[ $4 = '' ]]; then
         tarsubdir=''
      else
         tarsubdir='-C '$4
         if [[ $5 = '' ]]; then
             col_msg "no_line" "38" $4
         else
             col_msg "no_line" "38" $5
             echo '.............. changed name:' $3 >> $colexmlog
         fi
      fi
      tar $1 $2 $tarsubdir $3
      tar_ret=$?
      if [[ $tar_ret = '0' ]] ; then
         return 0
      else
         col_msg "plu_line" "35" $tar_ret
         (( tar_return_sum=tar_return_sum+1 ))
         return 1
      fi
}

# ---------------------------------------------------------------------------
# Collecting the detailed information found
# ---------------------------------------------------------------------------
#   function call: collect
#   parameter....: $1            file name to be collected
#                  $2            subdirectory
collect ()
{
      if [[ $2 = '' ]]; then
          c_dir=`pwd`
      else
          c_dir=$2
      fi
      if [[ $1 = "" ]]; then
          col_msg "no_line" "28"
          return
      else
         duplicate=''
         cat $colexmlog | grep "Collecting" | grep $1 > /dev/null
         catrc=$?
         if [[ $catrc = 0 ]]
         then
            (( dnum=dnum+1 ))
            cp $c_dir/$1 $workdir/$1.duplicate.$dnum
            duplicate=$c_dir
            c_name=$1.duplicate.$dnum
            c_dir=$workdir
         else
            c_name=$1
         fi

         col_msg "no_line" "21" "$1"
      fi
      check_space $c_dir/$c_name ${optarray[12]}
      check_space_return=$?
      case $check_space_return in
          0 )  taring -rf $tarfile $c_name $c_dir $duplicate   ;;
          1 )  col_msg "min_line" "32" $c_dir/$c_name
               taring -rf $tarfile $c_name $c_dir $duplicate
               compress -d $c_dir/$c_name                      ;;
          9 )  col_msg "plu_line" "33" $c_dir/$c_name          ;;
      esac
}

# ===========================================================================
# ---------------------------------------------------------------------------
# Set variables and check some FlowMark information
# Initialize the collection log file and the tar file
# ---------------------------------------------------------------------------
if [[ ! -d $resdir ]]            # Checking the results directory
then                             # No backup of this directory is done
   mkdir $resdir
fi

if [[ -d $workdir ]]             # Check and creat a working directory
then
   col_bak $workdir
fi
mkdir $workdir

if [[ -f $colexmlog ]]           # Check the collection log file
then
   col_bak $colexmlog
fi

banner FlowMark > $colexmlog     # Create the log file header (init)
col_msg "min_line"
echo ' Creation date: ' `date`         >> $colexmlog
echo ' Version .....:  '$0             >> $colexmlog
echo ' Host ........: ' `uname -n`     >> $colexmlog
echo ' '                               >> $colexmlog
echo `uptime`                          >> $colexmlog
col_msg "min_line"

if [[ -f $tarfile ]]             # Check the tar file
then
   col_bak $tarfile
fi

tar_return_sum=0
check_space $0 $workdir
check_space_return=$?

case $check_space_return in
     0 )  cp -p $0 $workdir/exmpzcol.sav
          if [[ $? = 0 ]]
          then
             col_msg "no_line" "21" "exmpzcol.sav"
             taring -cf $tarfile exmpzcol.sav $workdir
          else
             echo "There is not enough space in the working directory: "$workdir
             exit 1
          fi                                    ;;
     1 )  col_msg "min_line" "37" $workdir
          exit                                  ;;
     9 )  col_msg "plu_line" "37" $workdir
          exit                                  ;;
esac
# collecting this file (init)

# ===========================================================================
# ---------------------------------------------------------------------------
# Check and analyse FlowMark profile
# ---------------------------------------------------------------------------
col_prf_var ()
{
col_exm_files=`cat $1 | grep -v "^#" | grep EXM_FILES | sed -e "s/EXM_FILES//" |sed -e "s/=//" | sed -e "s/ //g"`
col_exm_host=`cat $1 | grep -v "^#" | grep EXM_HOST | sed -e "s/EXM_HOST//" |sed -e "s/=//" | sed -e "s/ //g"`
col_exm_server=`cat $1 | grep -v "^#" | grep EXM_SERVER_NAME | sed -e "s/EXM_SERVER_NAME//" |sed -e "s/=//" | sed -e "s/ //g"`
col_exm_db_name=`cat $1 | grep -v "^#" | grep EXM_DB_NAME | sed -e "s/EXM_DB_NAME//" |sed -e "s/=//" | sed -e "s/ //g"`
col_exm_db_path=`cat $1 | grep -v "^#" | grep EXM_DB_PATH | sed -e "s/EXM_DB_PATH//" |sed -e "s/=//" | sed -e "s/ //g"`
col_exm_trace_file=`cat $1 | grep -v "^#" | grep EXM_TRACE_FILE | sed -e "s/EXM_TRACE_FILE//" |sed -e "s/=//" | sed -e "s/ //g"`
}

# ---------------------------------------------------------------------------
# reset optarray for FlowMark related data if specified and/or unknown profile
# ---------------------------------------------------------------------------
fm_opt_reset ()
{
   i=4
   while (( $i < 7 )); do
     (( i=i+1 ))
     if [[  ${optarray[$i]} = 'yes' ]]; then
        chdefault $i
        col_msg "min_line" "31" $i
     fi
   done
}

# ---------------------------------------------------------------------------
# profile specification functions
# ---------------------------------------------------------------------------
fm_from_ps ()
{
###echo 'vvv fm_from_ps'
   ps -ef | grep exm  >  $workdir/$exm_proc_list
   collect $exm_proc_list $workdir
   # ------------------------------------------------------------------------
   #  If the TelePath is not available in the process list do not run
   #  exmpzchk and the socket checking program ( reset tpexist_inlist )
   # ------------------------------------------------------------------------
   if [[ -s $workdir/$exm_proc_list ]]; then
     j=0
     cat $workdir/$exm_proc_list |
     while read EXMLINE; do
        (( j=j+1 ))
     done
     if [[ `cat $workdir/$exm_proc_list | grep exmpisrv` = '' ]]
     then
        if [[ $j > 1 ]]
        then
           col_msg "plu_line" "60"
           col_msg "no_line" "61"
           tpexist_inlist='no'
        fi
     fi
   fi
   # ------------------------------------------------------------------------
   # Search for profile in process list
   # ------------------------------------------------------------------------
   j=0
   cat $workdir/$exm_proc_list | grep "\-f" > $workdir/$exm_proc_list\.temp
   if [[ -s $workdir/$exm_proc_list\.temp ]]; then
      col_msg "min_line" "48"
      col_msg "no_line" "49"
   else
      col_msg "min_line" "50"
      return 0
   fi
   cat $workdir/$exm_proc_list\.temp |
   while read PROC; do
      i=0
      set $PROC
      maxw=$#
      while (( $i < $maxw )) ; do
         (( i=i+1 ))
         if [[ '-f' = `echo $1 | awk '{print substr($1,1,2)}'` ]]; then
            (( j=j+1 ))
            profile_used[$j]=`echo $1 | sed -e "s/-f//" | sed -e "s/=//"`
         fi
         shift
      done
   done
   col_msg "min_line" "51" $j
   if [[ $j > 1 ]] ; then
      k=0
      s=1
      while (( $k < $j ));do
        (( k=k+1 ))
        i=0
        eqf=0
        while (( $i < $s )); do
           (( i=i+1 ))
           if [[ ${profile_used[$k]} = ${profile_used[$i]} ]];then
              (( eqf=eqf+1 ))
           fi
        done
        if [[ $eqf = 0 ]]; then
           (( s=s+1 ))
           profile_used[$s]=${profile_used[$k]}
        fi
      done
   fi
   if [[ $s > 1 ]];then
      i=0
      while (( $i < $s ));do
         (( i=i+1 ))
         col_msg "no_line" "52" ${profile_used[$i]}
      done
   fi
   return $s
}
# ---------------------------------------------------------------------------
fm_from_pzchk ()
{
   if [[ $1 = '' ]]
   then
      fopt=''
   else
      fopt='-f '$1
   fi
   exmpzchk -i $fopt > $workdir/$exmpzchk_outi
   if [[ -f $workdir/$exmpzchk_outi ]]; then
       set `cat $workdir/$exmpzchk_outi | grep CHK0063I | sed -e "s/\.$//"`
       eval fmprof=\$$#
       if [[ -f $fmprof ]]
       then
          fm_collect $fmprof
                    # collecting alternate found profiles
          cat $workdir/$exmpzchk_outi | grep CHK0075A | sed -e "s/\.$//" |
          while read FMPRF; do
             set $FMPRF
             eval alt_fmprof=\$$#
             get_fn $alt_fmprof
             cp -p $alt_fmprof $workdir/${stringword[$maxindex]}
             collect ${stringword[$maxindex]} $string_dir
          done
       else
          return 1
       fi
   else
       col_msg "plu_line" "8"
       return 1
   fi
}
# ---------------------------------------------------------------------------
fm_from_curdir ()
{
   if [[ -f $curdir/exmpzcfg.prf ]]
   then
      fm_collect $curdir/exmpzcfg.prf
   else
      return 1
   fi
}
# ---------------------------------------------------------------------------
fm_from_inst ()
{
   if [[ -f /usr/lpp/exm/bin/exmpzcfg.prf ]]
   then
      fm_collect /usr/lpp/exm/bin/exmpzcfg.prf
   else
      return 1
   fi
}
# ---------------------------------------------------------------------------
fm_collect ()
{
      get_fn $1
      cp -p $1 $workdir/${stringword[$maxindex]}
      collect ${stringword[$maxindex]} $string_dir
      col_prf_var $1
}
# ---------------------------------------------------------------------------
# profile main functions
# ---------------------------------------------------------------------------
if [[ ${optarray[11]} = "yes" ]]
then
   # ----------------------------------------------------
   # profile specified via option
   # ----------------------------------------------------
   if [[ ! -f $fmprof ]]
   then
      col_msg "min_line" "45" $fmprof
      fmprof='exmpzcfg.prf'
   fi
else
   fmprof='exmpzcfg.prf'
fi

   # ----------------------------------------------------
   # profile from process list
   # ----------------------------------------------------
fm_from_ps
fm_from_ps_ret=$?
if [[ $fm_from_ps_ret > 0 ]]
then
   fmprof=${profile_used[1]}
   col_msg "min_line" "41" $fmprof     # found from ps
fi
   # ----------------------------------------------------
   # profile search and collection
   # ----------------------------------------------------
if [[ $tpexist_inlist = 'yes' ]]
then
   fm_from_pzchk $fmprof          # analyse and collect
   fm_from_pzchk_rc=$?
else
   fm_from_pzchk_rc='1'
fi
if [[ $fm_from_pzchk_rc = 0 ]]
then
   col_msg "min_line" "42"     # found from pzchk
else
   fm_from_curdir
   if [[ $? = 0 ]]
   then
      col_msg "min_line" "43"  # found from curdir
   else
      fm_from_inst
      if [[ $? = 0 ]]
      then
         col_msg "min_line" "43"  # found from inst
      else
         col_msg "plu_line" "44"  # not found
         fm_opt_reset
      fi
   fi
fi

# ===========================================================================
# ---------------------------------------------------------------------------
#  The "hostname" command must return the same result as "uname -n"
#  otherwise FlowMark will produce unpredictable results.
# ---------------------------------------------------------------------------
if [[ `uname -n` = `hostname` ]]; then
   exm_uname=`uname -n`
   else
     col_msg "equ_line" "26"
     exit 2
fi

# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Starting main dbx analysis area
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

# ---------------------------------------------------------------------------
# Initialize the files needed for the dbx analysis
# ---------------------------------------------------------------------------
#   function call: exm_dbx_init
#   parameter....: none

exm_dbx_init () {
   dbxheader=$workdir/exmdbx\.header
   if [[ -f $dbxheader ]]; then
      col_bak $dbxheader
   fi
                                  # -----------------------------------------
                                  #     Header preparation of dbx output file
                                  # -----------------------------------------
   banner 'debug output' > $dbxheader
   echo '------------------------------------------------' >> $dbxheader
   echo ' Creation date: ' `date`    >> $dbxheader
   echo ' Version .....: '$0         >> $dbxheader
   echo ' Host ........: '$exm_uname >> $dbxheader
   echo '------------------------------------------------' >> $dbxheader

          dbxouthead=$workdir/exmdbx\.outhead
   echo '------------------------------------------------'  > $dbxouthead
   echo ' Redirected messages from dbx run               ' >> $dbxouthead
   echo '------------------------------------------------' >> $dbxouthead

   dbxoutput[0]=$workdir/exmdbx\.output
   dbxoutputname[0]=exmdbx\.output
   if [[ -f ${dbxoutput[0]} ]]; then
      col_bak ${dbxoutput[0]}
   fi
   cat $dbxheader > ${dbxoutput[0]}

         dbxdetach=$workdir/dbxdetach
           dbxquit=$workdir/dbxquit
   echo 'detach'  > $dbxdetach
   echo 'quit'    > $dbxquit
}
# ---------------------------------------------------------------------------
# Get the process ids of actual running proccesses
# ---------------------------------------------------------------------------
#   function call: exm_core_init
#   parameter....: none
exm_core_init () {

            dbxinput[1]=$workdir/tmp1in$$
          dbxthreads[1]=$workdir/tmp2in$$
        dbxthreadnum[1]=$workdir/tmp3in$$
   dbxthreads_detail[1]=$workdir/tmp4in$$
          exmthreads[1]=$workdir/etcore.sh
           dbxoutput[1]=$workdir/exmdbx\.output
       dbxoutputname[1]=exmdbx\.output
           dbxoutmsg[1]=$workdir/exmdbx\.outmsg
       dbxoutmsgname[1]=exmdbx\.outmsg
      banner 'core file' > ${dbxoutput[1]}
      echo '------------------------------------------------' >> ${dbxoutput[1]}
      echo ' Creation date: '`date`                           >> ${dbxoutput[1]}
##      echo ' Core file....: '     ????               ${P_pid[$i]}                     >> ${dbxoutput[1]}
##      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutput[1]}
      echo '------------------------------------------------' >> ${dbxoutput[1]}

            max_dbx_run=1
}

# ---------------------------------------------------------------------------
# Get the process ids of actual running proccesses
# ---------------------------------------------------------------------------
#   function call: exm_pids
#   parameter....: none
exm_dbx_pids () {
   i=0
   ps -eo user,pid,comm | grep exm | grep -v "^$procname" |
   while read INPUT; do
      (( i=i+1 ))
              P_user[$i]=`echo $INPUT | cut -d" " -f1`
               P_pid[$i]=`echo $INPUT | cut -d" " -f2`
              P_name[$i]=`echo $INPUT | cut -d" " -f3`
           dbxoutput[$i]=$workdir/exm_dbx\.output\.${P_pid[$i]}
       dbxoutputname[$i]=exm_dbx\.output\.${P_pid[$i]}
           dbxoutmsg[$i]=$workdir/exm_dbx\.outmsg\.${P_pid[$i]}
       dbxoutmsgname[$i]=exm_dbx\.outmsg\.${P_pid[$i]}
            dbxinput[$i]=$workdir/tmp1in$$\.${P_pid[$i]}
          dbxthreads[$i]=$workdir/tmp2in$$\.${P_pid[$i]}
        dbxthreadnum[$i]=$workdir/tmp3in$$\.${P_pid[$i]}
   dbxthreads_detail[$i]=$workdir/tmp4in$$\.${P_pid[$i]}
          exmthreads[$i]=$workdir/et${P_pid[$i]}.sh

      banner 'process' > ${dbxoutput[$i]}
      echo '------------------------------------------------' >> ${dbxoutput[$i]}
      echo ' Creation date: '`date`                           >> ${dbxoutput[$i]}
      echo ' Process-ID...: '${P_pid[$i]}                     >> ${dbxoutput[$i]}
      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutput[$i]}
      echo '------------------------------------------------' >> ${dbxoutput[$i]}

      echo '------------------------------------------------'  > ${dbxoutmsg[$i]}
      echo ' Redirected messages from dbx run               ' >> ${dbxoutmsg[$i]}
      echo ' Process-ID...: '${P_pid[$i]}                     >> ${dbxoutmsg[$i]}
      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutmsg[$i]}
      echo '------------------------------------------------' >> ${dbxoutmsg[$i]}

      echo '------------------------------------------------' >> ${dbxoutput[0]}
      echo ' Detailed output created for  '                   >> ${dbxoutput[0]}
      echo ' Process-ID...: '${P_pid[$i]}                     >> ${dbxoutput[0]}
      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutput[0]}
      echo '------------------------------------------------' >> ${dbxoutput[0]}
   done

   max_dbx_run=$i
}

# ---------------------------------------------------------------------------
# Create built-in shells and command files
# ---------------------------------------------------------------------------
#   function call: exm_dbx_shells
#   parameter....: none
exm_dbx_shells () {
   tgp='\$t'
   i=0
   while (( $i < $max_dbx_run )); do
     (( i=i+1 ))
       # -------------------------------------------------------------------
       # Building shell scripts for preparing the threadids as input for dbx
       # -------------------------------------------------------------------
cat > ${exmthreads[$i]} <<EXM_LABEL1a
#!/usr/bin/ksh
# The first positional parameter contains the output of the dbx thread subcommand
# The second contains the output file to be used
if [[ -s ${dbxthreadnum[$i]} ]]; then
   cat > ${dbxthreads_detail[$i]} <<EXM_LABEL1b
      sh echo '========== thread summary information =============' >> ${dbxoutput[$i]}
      sh echo ' '                                                >> ${dbxoutput[$i]}
      thread                                                     >> ${dbxoutput[$i]}
      sh echo ' '                                                >> ${dbxoutput[$i]}
EXM_LABEL1b
      while read threadid
      do
         cat >> ${dbxthreads_detail[$i]} <<EXM_LABEL1c
         sh echo ' '                                                >> ${dbxoutput[$i]}
         sh echo '---------- starting information for thread: '\$threadid' ----------' >> ${dbxoutput[$i]}
         sh echo ' '                                                >> ${dbxoutput[$i]}
         thread current \$threadid; thread info \$threadid          >> ${dbxoutput[$i]}
         sh echo ' '                                                >> ${dbxoutput[$i]}
         where                                                      >> ${dbxoutput[$i]}
         sh echo ' '                                                >> ${dbxoutput[$i]}
         registers                                                  >> ${dbxoutput[$i]}
#
         sh echo ' '
         sh echo '---- listi for '\$threadid' ----'
         sh echo ' '
         listi
         sh echo '------------------------------------------------'
#
         sh echo ' '                                                >> ${dbxoutput[$i]}
         sh echo '---------- ending information for thread: '\$threadid' ------------' >> ${dbxoutput[$i]}
EXM_LABEL1c
     done < ${dbxthreadnum[$i]}
else
   cat > ${dbxthreads_detail[$i]} <<EXM_LABEL1d
      sh echo ' ' >> ${dbxoutput[$i]}
      sh echo '---------- attention: no thread information located ----------' >> ${dbxoutput[$i]}
      sh echo ' ' >> ${dbxoutput[$i]}
EXM_LABEL1d
fi

exit
EXM_LABEL1a
chmod 755 ${exmthreads[$i]}

# ---------------------------------------------------------------------------
# Define command file for dbx
# ---------------------------------------------------------------------------
cat > ${dbxinput[$i]} <<EXM_LABEL2
   sh echo '========== dbx summary information =============' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- where information -------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   where                                                      >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- dump information --------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   dump                                                       >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- register information ----------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   registers                                                  >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
#
   sh echo ' '                                                ;
   sh echo '---------- listi information -------------------' ;
   sh echo ' '                                                ;
   listi                                                      ;
   sh echo '------------------------------------------------' ;
#
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   thread > ${dbxthreads[$i]};
   sh awk '{print \$1}' ${dbxthreads[$i]} | grep '$tgp' | sed -e "s/>//g" | sed -e "s/$tgp//g" > ${dbxthreadnum[$i]};
   sh ${exmthreads[$i]};
   source ${dbxthreads_detail[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- attribute information ---------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   attribute                                                  >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- mutex information -------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   mutex                                                      >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- condition information ---------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   condition                                                  >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- map information ---------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   map                                                        >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '========== end of dbx summary information ======' >> ${dbxoutput[$i]};
EXM_LABEL2
   done
}

# ---------------------------------------------------------------------------
# Create the dbx monitoring shell
# ---------------------------------------------------------------------------
#   function call: exm_dbx_monitor
#   parameter....: none
exm_dbx_monitor () {
cat > $exmonitor <<EXM_LABEL3
#!/usr/bin/ksh
#  \$1  dbx output file to be used for verification
#  \$2  dbx process id to be monitored
#  \$3  Mode: (a)ttach process or analyse (c)ore file
#  \$4  core file name
#  \$5  core program

while true
do
    ps c \$2 > /dev/null
    if [[ \$? = '1' ]]; then
       break
    fi
    outputsize=\`ls -l \$1 | awk '{print \$5}' \`
    case \$3 in
      a)   echo 'Analyzing. Please wait. Process .........: '\$2
           echo '                   ...  Result file size : '\$outputsize

           ;;
      c)   echo 'Analyzing. Please wait. Core file .......: '\$4
           echo '                   ...  Core program ....: '\$5
           echo '                   ...  Result file size : '\$outputsize
           ;;
    esac
    sleep 20
    if [[ \$outputsize = \`ls -l \$1 | awk '{print \$5}'\` ]]; then
        if [[ \`tail -1 \$1\` = '========== end of dbx summary information ======' ]]; then
           break
        fi
        kill -2 \$2
        echo 'Sending signal to dbx process ' >> $colexmlog
        echo 'Sending signal to dbx process '
## function not exported        col_msg "plu_line" "19" \$2
    fi
done
exit
EXM_LABEL3
chmod 755 $exmonitor
}

# ---------------------------------------------------------------------------
# Verify that dbx is installed
# ---------------------------------------------------------------------------
#   function call: dbx_check
#   parameter....: none
dbx_check () {

if [[ `lslpp -L | grep bos.adt.debug` = '' ]]; then
   echo 'bos.adt.debug is not installed'
   return 1
fi
}

# ---------------------------------------------------------------------------
# Calling dbx and its related monitor
# ---------------------------------------------------------------------------
#   function call: dbx_run
#   parameter....: $1            mode of dbx call ( a=attach c=core )
#                  $2            core program
#                  $3            core file
###                  $4            path of program form core
dbx_run () {

i=0
while (( $i < $max_dbx_run ))
do
  (( i=i+1 ))
  case $1 in
    a)    dbx -d100 -a ${P_pid[$i]} -c ${dbxinput[$i]} -I /usr/lpp/exm/bin < $dbxdetach > ${dbxoutmsg[$i]} 2>&1 &
          dbxpid[$i]=$!        # dbx processid
          $exmonitor ${dbxoutput[$i]} ${dbxpid[$i]} $1 &
          monpid[$i]=$!
            ;;
    c)    dbx -d100 -c ${dbxinput[$i]} $2 $3 < $dbxquit > ${dbxoutmsg[$i]} 2>&1 &
          dbxpid[$i]=$!        # dbx processid
          $exmonitor ${dbxoutput[$i]} ${dbxpid[$i]} $1 $3 $2 &
          monpid[$i]=$!

            ;;
  esac
done

                  # ---------------------------------------------------------
                  # Checking for locked sockets if dbx runs in attached mode
                  # ---------------------------------------------------------
if [[ $1 = 'a' ]]; then
 if [[ $tpexist_inlist = 'yes' ]]; then
   which $chk_socket_pgm > /dev/null
   chk_ret=$?
   if [[ $chk_ret = 0 ]]; then
     echo ' Checking for locked sockets '                    >> ${dbxoutput[0]}
     echo '------------------------------------------------' >> ${dbxoutput[0]}
     i=0
     j=0
     ls /var/exm/sockets/*ipc |
     while read INPUT; do
        echo 'checking ' $INPUT
        (( j=j+1 ))
        $chk_socket_pgm $INPUT > /dev/null
        if [[ $? > 0 ]];then
           (( i=i+1 ))
           echo 'Socket:' $INPUT ' is locked '   >> ${dbxoutput[0]}
        fi
     done
     echo '------------------------------------------------' >> ${dbxoutput[0]}
     echo ' total number of checked sockets: '$j             >> ${dbxoutput[0]}
     echo '------------------------------------------------' >> ${dbxoutput[0]}
     if [[ $i = 0 ]];then
        echo ' no locked sockets found '                        >> ${dbxoutput[0]}
        echo '------------------------------------------------' >> ${dbxoutput[0]}
     else
        echo '================================================' >> ${dbxoutput[0]}
        echo ' ' $i ' locked sockets found '                    >> ${dbxoutput[0]}
        echo '================================================' >> ${dbxoutput[0]}
     fi
     echo '------------------------------------------------' >> ${dbxoutput[0]}
   else
     col_msg "min_line" "9"
   fi
 fi
fi
## to be tested in detail (timing)
## wait for  ${dbxpid[$i]}
   i=0
   while (( $i < $max_dbx_run ))
   do
      (( i=i+1 ))
      while true
      do
         ps c ${dbxpid[$i]} > /dev/null
         if [[ $? = '1' ]]; then
            break
         fi
         sleep 10
         if [[ `tail -1 ${dbxoutput[$i]}` = ${col_exm_msg[5]} ]]; then
            break
         fi
      done
# kill the monitor shell if actual in sleep mode
      ps c ${monpid[$i]} > /dev/null
      if [[ $? = '0' ]]; then
         kill ${monpid[$i]}
      fi
  done
}

# ---------------------------------------------------------------------------
# Finishing dbx work
# ---------------------------------------------------------------------------
#   function call: dbx_finish
#   parameter....: $1                  contains the outputfile
#                  $2                  contains the mode (core or process)

dbx_finish () {
i=0
while (( $i < $max_dbx_run )); do
  (( i=i+1 ))
  cat ${dbxoutput[$i]} >> $1
  cat $dbxouthead      >> $1
  cat ${dbxoutmsg[$i]} >> $1
done

echo '================================================' >> $1
echo ' Analyzing of '$2' information is finished. '     >> $1
echo '================================================' >> $1
echo ' '
echo 'Analyzing of '$2' information is finished.'
}

# ---------------------------------------------------------------------------
# Find and analyse the core files in the system
# ---------------------------------------------------------------------------
#   function call: dbx_core_files
#   parameter....: none

dbx_core_files () {
##   echo 'tbd function locate core files'
##   echo 'actual used core file is' $pwddir'/core'

#          dbxoutput[$i]=$workdir/exm_dbx\.output\.${P_pid[$i]}
#          dbxoutmsg[$i]=$workdir/exm_dbx\.outmsg\.${P_pid[$i]}

   corename[1]=$pwddir/core
   if [[ -f ${corename[1]} ]]           # Check the core file
   then
      dbxouttmp=$workdir/dbxout.temp
      dbx -d100 /usr/bin/ls ${corename[1]} < $dbxquit > $dbxouttmp 2>&1
      corepgmname=`cat $dbxouttmp | grep Core | awk '{print $4}' | sed -e "s/(//" | sed -e "s/)//"`
      if [[ xx$corepgmname != 'xx' ]]
      then
         corepgmtemp=`which $corepgmname`
         corepgmtemp=`ls -Fl $corepgmtemp`
         set $corepgmtemp
         shift $(($# -1))
         eval corepgmtemp=$1
         corepgm[1]=$corepgmtemp
         coreoutname[1]=dbxcore.output
         coreout[1]=$workdir/${coreoutname[1]}
         cat $dbxheader > ${coreout[1]}
         echo ' ' >> ${coreout[1]}
         ls -l ${corename[1]} >> ${coreout[1]}
         echo ' ' >> ${coreout[1]}
         core_file_count=1
         return 0
      else
         col_msg "no_line" "3"
         col_msg "no_line" "62" $pwddir
         col_msg "no_line" "3"
         return 1
      fi
   else
      col_msg "no_line" "3"
      col_msg "no_line" "58" $pwddir
      col_msg "no_line" "59"
      col_msg "no_line" "3"
      return 1
   fi
}

# ========================================================================
# Verify that dbx is installed                       (start) optarray[1+2]
# ========================================================================
dbx_check
# -------------------------- run dbx only if dbx is availbale ------ (1) -
if [[ $? = 0 ]]; then
# -------------------------- check options set for dbx run --------- (2) -
  if [[ ${optarray[1]} = "yes" || ${optarray[2]} = "yes" ]]; then
    exm_dbx_init
# ========================================================================
# Attaching to processes                               (start) optarray[1]
# ========================================================================
    if [[ ${optarray[1]} = "yes" ]]; then
       exm_dbx_pids
       exm_dbx_shells
       exm_dbx_monitor
       dbx_run "a"
       dbx_finish ${dbxoutput[0]} process
       collect ${dbxoutputname[0]} $workdir
    fi
# ========================================================================
#                                                        (end) optarray[1]
# ========================================================================
# ========================================================================
# Analyse core files                                   (start) optarray[2]
# ========================================================================
    if [[ ${optarray[2]} = "yes" ]]; then
       max_dbx_run=1
       exm_core_init
       dbx_core_files
       if [[ $? = 0 ]]; then
         exm_dbx_shells
         exm_dbx_monitor
         i=0
         while (( $i < $core_file_count )) ; do
            (( i=i+1 ))
            dbx_run "c" ${corepgm[$i]} ${corename[$i]} ${corepath[$i]}
            dbx_finish  ${coreout[$i]} core
            collect  ${coreoutname[$i]} $workdir
         done
       fi
    fi
# ========================================================================
#                                                        (end) optarray[2]
# ========================================================================
# ------------------------------------------------------------- else (2) -
  else
     col_msg "plu_line" "7"
  fi
# ------------------------------------------------------------- else (1) -
else
   col_msg "plu_line" "34"
fi
# ========================================================================
#                                                      (end) optarray[1+2]
# ========================================================================


# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Starting main collection area (dbx already done)
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

# ========================================================================
#                                                        (end) optarray[3]
# ========================================================================
# FlowMark information is included                     (start) optarray[5]
# ========================================================================

# ---------------------------------------------------------------------------
# Searching for links
# ---------------------------------------------------------------------------
#   function call: link_search
#   parameter....: $1            directory to be searched
#                  $2            output file
link_search ()
{
   echo ' Searching for links in '$1                 >> $2
   ls -Fl $1 | grep @ >> $workdir/$exmlinks
   if [[ $? = '0' ]]
   then
      echo '------------------------------------------------' >> $2
   else
      echo ' No additional links found      '                 >> $2
      echo '------------------------------------------------' >> $2
   fi
}
                #---------------------------------------------------------
                # Included FlowMark informations:
                #  - exmpkrep.log
                #  - exmpzchk -d   (if TelePath is ok)
                #  - exmpzchk -s   (if TelePath is ok)
                #  - additional added links
                #  -
                #---------------------------------------------------------
if [[ ${optarray[5]} = "yes" ]]; then
   exm_fm_log=$col_exm_files/exmpkrep.log
   if [[ -f $exm_fm_log ]]; then
       collect exmpkrep.log $col_exm_files
   else
       col_msg "plu_line" "29"
   fi
   if [[ $tpexist_inlist = 'yes' ]]
   then
      exmpzchk -d > $workdir/$exmpzchk_outd
      if [[ -f $workdir/$exmpzchk_outd ]]; then
          collect $exmpzchk_outd $workdir
      else
          col_msg "plu_line" "8"
      fi
      exmpzchk -s > $workdir/$exmpzchk_outs
      if [[ -f $workdir/$exmpzchk_outs ]]; then
          collect $exmpzchk_outs $workdir
      else
          col_msg "plu_line" "8"
      fi
                #---------------------------------------------------------
                # Output of FlowMark diagnosis tool
                #---------------------------------------------------------
      echo '================================================' >  $workdir/$exmdgnout
      echo ' Generated at :' `date`                           >> $workdir/$exmdgnout
      echo '================================================' >> $workdir/$exmdgnout
      echo 'n' >  $workdir/$exmdgnout.in1
      echo 'l' >> $workdir/$exmdgnout.in1
      echo 'q' >> $workdir/$exmdgnout.in1
      echo 'x' >> $workdir/$exmdgnout.in1
      found_switch='off'
      count='0'
      echo 'n' >  $workdir/$exmdgnout.in
      echo 'l' >> $workdir/$exmdgnout.in
      echo 'q' >> $workdir/$exmdgnout.in
      exmpzdgn < $workdir/$exmdgnout.in1 |
      while read vx
      do
        if [[ $vx = 'Available applications :' ]]; then
           found_switch='on'
           until [[ $found_switch = 'off' ]]
           do
             read vx
             if [[ $vx = '' ]]; then
                found_switch='off'
             else
               (( count=count+1 ))
               echo 'd' >> $workdir/$exmdgnout.in
               echo $count >> $workdir/$exmdgnout.in
             fi
           done
        fi
      done
      echo 'c' >> $workdir/$exmdgnout.in
      echo '3' >> $workdir/$exmdgnout.in
      echo 'x' >> $workdir/$exmdgnout.in
      exmpzdgn < $workdir/$exmdgnout.in >> $workdir/$exmdgnout
      if [[ -f $workdir/$exmdgnout ]]; then
          collect $exmdgnout $workdir
      else
          col_msg "plu_line" "55"
      fi
   fi
                #---------------------------------------------------------
                # search for links
                #---------------------------------------------------------
   echo '================================================' >  $workdir/$exmlinks
   echo ' Searching for additional specified links       ' >> $workdir/$exmlinks
   echo '================================================' >> $workdir/$exmlinks
   col_msg "min_line" "39"
   link_search /usr/lpp/exm/bin           $workdir/$exmlinks
   link_search /usr/lpp/exm/lib           $workdir/$exmlinks
   link_search /usr/bin/exm\*             $workdir/$exmlinks
   link_search /usr/lib/libexm\*          $workdir/$exmlinks
   link_search /usr/lib/exm\*             $workdir/$exmlinks
   link_search /usr/lpp/ostore/common/bin $workdir/$exmlinks
   link_search /usr/lpp/ostore/common/lib $workdir/$exmlinks
   link_search /usr/lpp/ostore/cset/bin   $workdir/$exmlinks
   link_search /usr/lpp/ostore/cset/lib   $workdir/$exmlinks
   collect $exmlinks $workdir
                #---------------------------------------------------------
                # Search for installed fixes and record details
                #---------------------------------------------------------
   echo '================================================' >  $workdir/$exminstfix
   echo ' Query for installed fixes                      ' >> $workdir/$exminstfix
   echo '================================================' >> $workdir/$exminstfix
   echo '       output of infix -ia                      ' >> $workdir/$exminstfix
   echo '------------------------------------------------' >> $workdir/$exminstfix
   instfix -ia                                             >> $workdir/$exminstfix
   echo '================================================' >> $workdir/$exminstfix
   echo '       output of infix -ic                      ' >> $workdir/$exminstfix
   echo '------------------------------------------------' >> $workdir/$exminstfix
   instfix -ic                                             >> $workdir/$exminstfix
   if [[ -f $workdir/$exminstfix ]]; then
       collect $exminstfix $workdir
   else
       col_msg "plu_line" "8"
   fi
fi

# ========================================================================
#                                                        (end) optarray[5]
# ========================================================================
# Audit trail information is included                  (start) optarray[6]
# ========================================================================
if [[ ${optarray[6]} = "yes" ]]; then
   exm_audit_dir=$col_exm_files/$col_exm_db_name.AUD/$col_exm_server
   if [[ -d $exm_audit_dir ]]; then
       locpwd=`pwd`
       cd $exm_audit_dir
       ls |
       while read exmauditfile; do
          collect $exmauditfile $exm_audit_dir
       done
       cd $locpwd
   else
       col_msg "plu_line" "27"
   fi
fi

# ========================================================================
#                                                        (end) optarray[6]
# ========================================================================
# Trace information is included                        (start) optarray[7]
# ========================================================================
if [[ ${optarray[7]} = "yes" ]]; then
   if [[ -s $col_exm_trace_file ]]; then
       ls $col_exm_trace_file* |
       while read exmtracefile; do
          get_fn $exmtracefile
          collect ${stringword[$maxindex]} $string_dir
       done
   else
       col_msg "plu_line" "22"
   fi
fi

# ========================================================================
#                                                        (end) optarray[7]
# ========================================================================
# Identifying and collecting ObjectStore information   (start) optarray[8]
# ========================================================================
if [[ ${optarray[8]} = "yes" ]]; then
   if [[ $col_exm_host != "" && $col_exm_host != $exm_uname ]]; then
      col_msg "bla_line" "11"
   else
      ostore_log_file='/usr/lpp/ostore/common/etc/'$exm_uname'_server_parameters'
      if [[ ! -s $ostore_log_file ]] ; then
         col_msg "bla_line" "12"
      else
         ostore_trans_log=`cat $ostore_log_file | grep 'Log File:' | sed -e "s/Log//" |sed -e "s/File://" | sed -e "s/ //g"` ;
         col_msg "min_line" "13"
         set | grep OS_ >> $colexmlog
         col_msg "1"
         col_msg "min_line" "14" "$ostore_trans_log"
         if [[ ! -s /tmp/ostore/oss_out ]]; then
            col_msg "plu_line" "16"
         else
            col_msg "no_line" "15"
            collect oss_out /tmp/ostore
         fi
         if [[ ! -s /tmp/ostore/osc4_out ]]; then
            col_msg "plu_line" "18"
         else
            col_msg "no_line" "17"
            collect osc4_out /tmp/ostore
         fi
      fi
   fi
fi
# ========================================================================
#                                                        (end) optarray[8]
# ========================================================================
# Identifying and collecting system information        (start) optarray[9]
# ========================================================================
if [[ ${optarray[9]} = "yes" ]]; then
   if [[ -d $snapdir ]]; then
      rm -R $snapdir
   fi
   mkdir $snapdir
   dispm='no'
   col_msg "min_line" "53" $snapvalue
   dispm='yes'
   snap $snapvalue -d $snapdir -c
   snap_rc=$?
   if [[ $snap_rc > 0 ]]; then
      col_msg "min_line" "56" $snap_rc
      col_msg "no_line" "57" $snapdir
   else
      collect snap.tar.Z $snapdir
   fi
fi

# iostat      in conjunction with -a
# ipcs -a
# lsps -s a
# exmdbx ?  pid von exm ? -> profile

exmlocout=$workdir/$exmlocale

col_msg "min_line" "46"
echo '================================================' >  $exmlocout
echo '   NLS: System Locale Information               ' >> $exmlocout
echo '================================================' >> $exmlocout
echo '   Command out from : locale                    ' >> $exmlocout
echo '------------------------------------------------' >> $exmlocout
locale                                                  >> $exmlocout
echo '------------------------------------------------' >> $exmlocout
echo '   Command out from : locale -a                 ' >> $exmlocout
echo '------------------------------------------------' >> $exmlocout
locale -a                                               >> $exmlocout
echo '------------------------------------------------' >> $exmlocout
echo '   Command out from : locale -m                 ' >> $exmlocout
echo '------------------------------------------------' >> $exmlocout
locale -m                                               >> $exmlocout
echo '------------------------------------------------' >> $exmlocout
echo '   Command out from : locale charmap            ' >> $exmlocout
echo '------------------------------------------------' >> $exmlocout
locale charmap                                          >> $exmlocout
echo '================================================' >> $exmlocout
echo '   End of system locale information collection  ' >> $exmlocout
echo '================================================' >> $exmlocout

if [[ -f $workdir/$exmlocale ]]; then
    collect $exmlocale $workdir
else
    col_msg "plu_line" "47"
fi

# ========================================================================
#                                                        (end) optarray[9]
# ========================================================================

# ========================================================================
# End: Collecting log file and compress tarfile if required
# ========================================================================
col_msg "bla_line"
collect $logname $resdir
col_msg "bla_line"
echo ' '
echo ${col_exm_msg[2]}
echo ' '
col_msg "equ_line" "30"

if [[ $tar_return_sum > 0 ]]; then
   col_msg "plu_line" "36" $workdir
else
   if [[ ${optarray[10]} = 'yes' ]]; then
      col_msg "no_line" "20" "$tarfile"
      if [[ -f $tarfile\.Z ]]             # Check the tar file
      then
         col_bak $tarfile.Z
      fi
      compress $tarfile
      tarfile=$tarfile.Z
   fi
   rm -R $workdir
   echo ' '
   echo 'The result is stored in the file '$tarfile
fi
exit
