#! /bin/sh
#
# @(#)nsr_support.sh 6.1 95/12/15 Copyright (c) 1993-1995, Legato Systems, Inc.
#
# All rights reserved.
#
:

RELEASE=4.2

PATH=$PATH:/usr/ucb:/bin:/usr/bin:/usr/etc:/etc:.:/usr/local/bin:/usr/sbin:/opt/SUNWspro/bin  
export PATH

version=${RELEASE}.0
LANG=C; export LANG

# default values for arch dependent stuff
PS=ps
PS_BIG=axlww
PS_SMALL=ax

TRACE=null
DMESG=null
SWAP_INFO_COMM=null

system=`uname`
host=`hostname`

if [ X${host} = X ]; then
	host=`uname -n`
fi

if [ "$system" = "SunOS" ]; then
	
	# System V release 4 on SPARC, a.k.a. SunOS 5.0.x
	osrel=`uname -r`
        if [ `expr "X${osrel}" : "X5."` -eq 3 ]; then
		myarch=solaris
		TRACE="truss -p"
		if [ -x /usr/ucb/ps ]; then
			PS="/usr/ucb/ps"
		else
			PS="/bin/ps"
			PS_BIG="-ef"
			PS_SMALL="-e"
		fi
		DMESG=dmesg
		SWAP_INFO_COMM="swap -s"

	else 
		myarch=bsdsunos
		TRACE="trace -t -p"
		PS="ps"
		DMESG="dmesg"
		SWAP_INFO_COMM="pstat -s"
	fi

else if [ "$system" = "AIX" ]; then
	myarch=ibmrs6000
	TRACE=null
	DMESG="errpt -a"
	SWAP_INFO_COMM="lsps -a"

else if [ -x /usr/bin/scosh ]; then
	myarch=sco
	TRACE=null
	PS="ps"
	PS_BIG="-ef"
	PS_SMALL="-e"
	DMESG=dmesg
	SWAP_INFO_COMM="swap -l"
	
else
	echo "Cannot determine base machine type for \`${host}'."

      fi	# sco
      fi	# aix
      fi	# sun

verbose=""
tempout=/nsr/tmp/n_sup.$$
rm -f $tempout.* > /dev/null 2>&1

# where to send stuff
# default to just write to stdout, dont save tmpfile, don't mail
mail="no"
save="no"
sendto=""

# What information to print
cores="yes"	# list core directory, adb, dbx of corefiles
dmesg="yes"	# do dmesg
grep="yes"	# grep for strings in log files
indexes="yes"	# location, disk space, nsrls of indexes
logs="yes"	# tail logfiles
mm="no"		# do "mminfo -avV"
nsrfiles="yes"	# tail nsr logs, resources 
ckpath="yes"	# check path for nsr executables
ps="yes"	# ps commands
rap="no"	# tail rap logs, resources
res="yes"	# get res files
trace="no"	# trace running daemons for a while
dbx="yes"	# run dbx on nsr daemons
netstat="yes"	# get netstat -m & -f inet info
vmstatinfo="yes"	# get vmstat 5 10 output
swapinfo="yes"	# get swap space available
compress="no"	# default is no compression
mail="no"	# default is don't mail
mminfomv="no"   # do a "mminfo  -mvV "

#cores=no;  dmesg=no; grep=no; indexes=no; logs=no; mm=no; nsrfiles=no; ckpath=no; ps=no;
#  rap=no; res=no; trace=no


##
# Deal with command line arguments
# in the simplest case: if there are no arguments, go with the defaults. . .
if [ "$#" = "0" ]; then
  of=""
fi

first=yes
savecommandline="$*"
while [ $# -gt 0 ]; do
	if  expr $1 : '^-[A-Z].*' >/dev/null 2>&1; then
		# UPPERCASE
		if [ $first = yes ]; then
		first=no
		if  expr $1 : '^-[^SFM].*' >/dev/null 2>&1; then
			# not the s,f,m option
			# turn off all of the options
			cores=no; ps=no; dmesg=no; grep=no; indexes=no; logs=no; 
			mm=no; nsrfiles=no; ckpath=no; rap=no; res=no; trace=no
		fi
		fi 
	fi
	case "$1" in
	# mail stuff to support@vendor.com
	#-s)     sendto="$sendto support@vendor.com" 
	#	mail="yes" 
	#	shift
	#	;;

	-f)	if [ "$#" -lt "2" ]; then
			echo "missing outputfile" 1>&2
			echo "saving output in $tempout" 1>&2
		else
			if [ -f "$2" ]; then 
				echo "appending output to file $2" 1>&2
			else
				echo "putting results in file $2" 1>&2
			fi
			of="$2"
			shift
		fi
		save=true
		shift 
                ;;
		
	-m)	# get the list of people to mail to
		shift
		while expr $1 : '^[^-]*' >/dev/null 2>&1; do
			sendto="$sendto $1"
			shift
		done
		mail=yes
		compress=yes  # if it's gonna be mailed ,then compress, uuenco
		;;
	-[eE]*)	 :		# Set everything on 
			cores=yes; ps=yes; dmesg=yes; grep=yes; indexes=yes; 
			logs=yes; mm=yes; nsrfiles=yes; ckpath=yes; 
			rap=yes; res=yes; dbx=yes;
			if [ "$myarch" = "solaris" -o "$myarch" = "bsdsunos" ]; then
                       		trace="yes"
			else
				trace="no"
			fi
        
		shift
		;;
	-[zZ])	compress=yes    # compress with compress and uuencode if poss.
		shift
		;;

        -[Cc]*)	cores="yes"	# list core directory, adb, dbx of corefiles
		shift
		;;

	-[dD]*) dmesg="yes" # do dmesg
		shift
		;;

	-[Gg]*) grep="yes"	# Grep for strings in logfiles
		shift
		# subsequent command line args are treated as strings
		while expr "$1" : '^[^-]*' >/dev/null 2>&1; do
			if [ ! -n "$greplist" ]; then 
				greplist="mbuf $1" 
               	 	else	
				greplist="$greplist $1"
			fi
		shift
		done
		;;
 
	-[Ii])	indexes="yes"	# location, disk space, nsrls of indexes
		shift
		;;
	-[Ll]*)	logs="yes"	# tail logfiles
		shift
		;;
	-[Mm][mM]*)	mm="yes"	# do "mminfo -aV"
		shift
		;;
	-[Mm][vV]*)	mminfomv="yes"	# do "mminfo -mvV"
		shift
		;;
	-[Pp]*)	ckpath="yes"  # check for presence of nsr executables
		shift
		;;
	-[Pp][Ss]*)	ps="yes"	# ps commands
                shift
		;;
	-[Nn]*)	nsrfiles="yes"	# tail nsr logs, resources 
		shift
		;;
	-[r][Aa]*)	rapfiles="yes"	# tail rap logs, resources
		shift
                ;;
	-[Rr][Ee]*)	res="yes"	# get res files
		shift
		;;
	-[Tt]*) shift
		if [ "$myarch" = "solaris" -o "$myarch" = "bsdsunos" ]; then
			trace="yes"	# trace running daemons for a while
		 	while expr "$1" : '^[^-]*' >/dev/null 2>&1; do
		       		if  expr "$1" : '[0-9][0-9]*' > /dev/null 2>&1; then
					tracetime="$1"
				else 
					if [ ! -n "$proclist" ]; then 
						proclist="$1" 
               			 	else	
						proclist="$proclist $1"
					fi
				fi
				shift
			done
		 else 
			echo "Not a valid option for this platform"
		fi
		;;
	-[vV]*)	 :		# Set everything on 
			cores=yes; ps=yes; dmesg=yes; grep=yes; indexes=yes; 
			logs=yes; mm=yes; nsrfiles=yes; ckpath=yes; 
			rap=yes; res=yes;dbx=yes; 
                                # trace=yes	
		shift 
		;;
                 
	*)	echo "Trouble with command line arg: \"$1\""
		shift
		;;
	esac
done


# function to check for blank responses
noblank ()
{

        while true
        do
        read ans
        # cannot do test with more than one word in variable
        # so grab first field with awk and then check if it is empty
        firstfield=`echo $ans | awk ' { print $1 }'`
        if [ -z "$firstfield" ]; then
           echo "$prompt" 1>&2
        else
           echo "$heading  $ans" >> $of
           break
        fi
        done

} # end noblank


newincident="no"
echo " Is this a new incident[Y/N]? "  1>&2
while true
do
read ans
if [ X${ans} = X ]; then
   result="${default}"
else
   result="${ans}"
fi

if [ `expr X${result} : 'X[yY]'` -ne 0 ]; then
   newincident="yes"
   break
elif [ `expr X${result} : 'X[nN]'` -ne 0 ]; then
   break
else
   echo "Please respond \`yes' or \`no'"  1>&2
fi
done


if [ "$newincident" = "no" ]; then
    echo "Enter the incident number:" 1>&2
    prompt="Please enter a incident number" 
    noblank
    incidentnum="$ans"
else
    incidentnum="new incident"
fi


if [ "$mail" = "yes" ]; then
  if [ ! -n "$sendto" ]; then
    echo "blank mailing list:" 1>&2
    if [ "save" = "no" ]; then
      echo saving to file $tempout 1>&2
      save=yes
      of=$tempout
    else
      echo "not mailing" 1>&2
    fi
  else
    echo "mailing output to$sendto" 1>&2
  fi
else if [ "$compress" = "yes" ]; then
	of=$tempout
     fi
fi



# make a tmp file if output is not to be saved to a file of a different name , 
# yet needs to be mailed
[ "$save" = "no" ] && [ "$mail" = "yes" ] && of=$tempout
[ -n "$of" ] && touch $of

if [ "$verbose" = "yes" ]; then
  echo "mail=\"$mail\",save=\"$save\",of=\"$of\",sendto=\"$sendto\""
fi


# 
#
echo "Please enter your name: " 1>&2
prompt="Enter your name"
heading="Name:"
noblank
echo "Please enter your company name: " 1>&2 
prompt="Enter your company name "
heading="Company:"
noblank
echo "Please enter your phone number " 1>&2
prompt="Enter your phone number " 
heading="Phone:"
noblank
echo "Please enter your email address. If none, enter "none" " 1>&2 
prompt="Enter your email address.  If none, enter "none" " 1>&2  
heading="Email:"
noblank
echo 
echo "This may take a while ....."  1>&2

#############
## Set up some Defaults
#
if [ ! -n "$greplist" ]; then 
  greplist="mbuf $1" 
else	
  greplist="$greplist $1"
fi

coredir="/nsr/cores"			# where cores are kept
execdirs="/usr/etc"			# where executables are kept
					# is overridden by cores section

#
#  dbx defaults 
#
dbxfound=""
if [ $myarch = "sco" ]; then
        dbxexec="dbxtra"
else dbxexec="dbx"
fi


# resource files
nsrres="/nsr/res/*.res" 
rapres="/etc/la.res /var/rap/*.res"    
if [ "$nsrfiles" != "no" ]; then
  resfiles="$resfiles $nsrres"
fi
if [ "$rap" != "no" ]; then
  resfiles="$resfiles $rapres"
fi


# logfiles
syslogs="/usr/adm/messages"
nsrlogs="/nsr/logs/messages /nsr/logs/summary"
raplogs="/var/rap/check.log /var/rap/serv.log"
nsrdlog="/nsr/logs/daemon.log"
if [ "$nsrfiles" != "no" ]; then
  logfiles="$nsrlogs"
fi
if [ "$rap" != "no" ]; then
  logfiles="$logfiles $raplogs"
fi
logfiles="$syslogs $logfiles"
logfiles="$nsrdlog $logfiles"

howmuch=300                 	# how many lines to tail off the logfiles
## formatting
sep="------------------"
#awk prog to FOLD long lines

FOLD='eval awk "BEGIN { max=255 }
{ if (length(\$0) > max) {
        left=\$0 ;
        printf(\"%s\n\", substr(\$0 , 1, max));
                left=substr(left, max + 1);
        while (length(left) > 0) {
                printf(\"%s\n\", substr(left , 1, max));
                left=substr(left, max + 1);
        }
} else
        print \$0;
}"'


# get out now if we are not root
if [ -f /bin/id ]; then
  id | grep root > /dev/null 2>&1
  if [ $? != 0 ]; then
    echo "$0: can only be run as root" 1>&2 
    exit 3
  fi
fi

#  Get basic info on machine
{ 
  echo
  echo "nsrsupport version" $version
  echo "NetWorker Support Info."
  echo "Hostname ${host}"
  echo "Dommainname `domainname`"
  echo "On: `date`" 
  echo "uname -a: "  `uname -a`
  if [ "${myarch}" = "sco" ]; then
	echo "uname -X "  `uname -X`	
  fi
  echo; echo
}>> $of

 
#                  
#####
## The following will work on a client or a server . . .
#####
# 
#  ############
  # trace those processies - the default is to trace ALL nsr, rap
if [ "$trace" !=  "no" ]; then
  {
  # set these to something sane
    tracetime="${tracetime-10}"
    proclist="${proclist-nsr rap save asm}"

    # get a listing of procids, procnames 
    if [ "$PS_SMALL" = "-e" ]; then
      # Sys5 style ps, command in column 4, PID in 1
      prog="`echo $proclist | tr ' ' '\012' | awk '{ print \"$4 ~/\" $1 \"/ { print $1 \\"=\\" $4 }\" } '`"
    else
      # bsd style ps, command in column 5, PID in 1
      prog="`echo $proclist | tr ' ' '\012' | awk '{ print \"$5 ~/\" $1 \"/ { print $1 \\"=\\" $5 }\" } '`"
    fi

    ptab="`$PS $PS_SMALL | nawk "$prog" | sort -u | tr '\012' ' '`"
    traceids=""
    proctab=""

    echo "$sep"
    echo Tracing:
    echo $ptab | tr '=' ':' 2>&1 | $FOLD
    echo "$sep"
      
    for proc in $ptab; do
      procid="`expr $proc : '^\([^=][^=]*\)='`"
      if [ $? != 0 ]; then
        echo "failed to get procid" 1>&2
        continue 
      fi
      procname="`expr $proc : '.*=\([^=][^=]*\)$'`"
      if [ $? != 0 ]; then
        echo "failed to get procname" 1>&2
        continue 
      fi
      traceinfo=$tempout.$procid.trc
      # put trace in the background in order to time it 
      $TRACE $procid > $traceinfo 2>&1 &
      child=$!
      # add to list of traceids
      traceids="$traceids $child"
      proctab="$proctab $proc=$child"
    done
    
    # wait a while, now that traces are started ...
    sleep $tracetime
   
    # try to kill all of the traces 
    for traceid in $traceids; do
      kill -TERM $traceid 
    done
    
    # insure that each one is dead . . .
    for proc in $proctab; do
      # check that each proc is dead: if not 
      dead=1
      procid="`expr $proc : '^\([^=][^=]*\)='`"
      if [ $? != 0 ]; then
        echo "failed to get procid" 1>&2
        continue 
      fi
      procname="`expr $proc : '[^=][^=]*=\([^=][^=]*\)='`"
      if [ $? != 0 ]; then
        echo "failed to get procname" 1>&2
        continue 
      fi
      traceinfo=$tempout.$procid.trc
      traceid="`expr $proc : '[^=][^=]*=[^=][^=]*=\([^=][^=]*\)$'`"
      if [ $? != 0 ]; then
        echo "failed to get procname" 1>&2
        continue 
      fi
      $PS $PS_SMALL | awk '{print $1}' | grep $traceid > /dev/null 2>&1
      if [ $? = 0 ]; then
        for i in $traceid $traceid; do 
          kill -TERM $traceid
          $PS $PS_SMALL | awk '{print $1}' | grep $traceid > /dev/null 2>&1
          dead=$?
          echo "reached inside of for loop due to unstop. $dead"
          if [ $dead != 0 ];then
            break
          fi
        done
      fi
      if [ $dead = 0 ];then
        kill -KILL $traceid
        $PS $PS_SMALL | awk '{print $1}' | grep $traceid > /dev/null 2>&1
        dead=$?
      fi
      if [ $dead = 0 ]; then
        echo "unable to kill process $traceid, watch file $traceinfo" 1>&2
      else
        # make a neat little formatted entry in outputfile
        echo "$sep"
        echo "trace of $procname. id: $procid"
        cat $traceinfo
        echo "$sep"
        echo
	if [ -f "$traceinfo" ]; then
        	rm $traceinfo
	fi
      fi
    done      
    # cleanup just in case

    for i in $tempout.[0-9]*.trc; do
      if [  $i -gt $$ ]; then
        kill -TERM `expr \"$i\" : $tempout'.\([^.][^.]*\)'`
      fi
    done
    rm $tempout.[0-9]*.trc > /dev/null 2>&1
  
 }  >> $of
fi 


# do the "ps" command
if [ "$ps" != "no" ]; then
{
 # options  without dashes for RS6000

  echo "$sep"
  echo "$PS $PS_BIG"
  echo "$sep"
  $PS $PS_BIG 2>&1
  echo "$sep"
  echo
	
  if [ "$myarch" = "bsdsunos" ]; then
  echo "$sep"
  echo "ps auxww"
  echo "$sep"
  ps auxww 2>&1
  echo "$sep"
  echo
  fi
} >> $of
fi

#
#  get netstat -m and netstat -f inet output
#
if [ "$netstat" != "no" ]; then
{
	echo "$sep" 
	echo "netstat -m"
	echo "$sep"
	netstat -m 2>&1
	echo "$sep"
	echo 
	echo "$sep"
	echo "netstat -f inet"
	echo "$sep" 
	netstat -f inet 2>&1	
	echo "$sep"
} >> $of
fi

#
#  run swap info 
#
if [ "$swapinfo" != "no" ]; then
{
	echo
	echo "$sep"
  	echo "$SWAP_INFO_COMM"
	echo "$sep"
	echo `$SWAP_INFO_COMM`
	echo "$sep"
} >> $of
fi
#
#  get vmstat 5 10 info
#

if [ "$vmstat" != "no" ]; then
{
	echo
        echo "$sep"
        echo "vmstat 5 10"
        echo "$sep"
        vmstat 5 10
        echo "$sep"
} >> $of
fi
	
#
#  get a list of all networker servers
#

echo  >> $of
echo "$sep"  >> $of
echo "rpcinfo -b 390109 2 | sort -u "  >> $of
echo "$sep"  >> $of 
rpcinfo -b 390109 2 | sort -u  >> $of
echo "$sep" >> $of
echo >> $of

 
# Parse the command search path, do a "sum" on a few representative
# commands
currentdir="`pwd`"
if [ \( "$ckpath" != "no" \) -o \( "$cores" != "no" \)  ]; then
    nsrdirs=""
    chekthese="nsrd save savefs savegroup"
    echo >> $of
    echo "$sep" >> $of
    echo searching '$PATH' for commands: >> $of
    echo "$chekthese">> $of
    echo "$sep" >> $of
    # rcdpath becomes blank separated list of path directories
    rcmdpath="`echo 'echo $PATH' | /bin/csh -t | tr ':' ' '`">> $of
    anyfound=""
    
    for i in $rcmdpath; do
      # Run through each element of the pathname
      nsrsfound=""
      if [ ! -d $i ]; then
        echo no such directory as $i          >> $of
        continue
      fi
      # add this directory to the search path, if it is not already there
      messy=`echo $PATH | grep ':'$i':'2> /dev/null`
      if [ ! -n "$messy"  ]; then
        PATH="$i:$PATH" 
      fi
      cd $i
      for j in $chekthese; do   
        if [ -f "$j" ]; then
          nsrsfound="$nsrsfound $j"

	  #
	  # SCO doesn't have a which command which means that 
	  # the location of nsr daemons will have to be found
	  # by looking at the directories $PATH.  We may as well
	  # use the same method for sparc and aix
	  # 
	  if [ "$j" = "nsrd" -a -z "$nsrhome" ]; then
	 	nsrhome="$i"
	  fi
        fi
      done 
      if [ -n "$nsrsfound" ]; then
        anyfound="yes"
        echo "$sep" >> $of
        echo "NSR executables in $i:">> $of
        nsrdirs="$nsrdirs $i"
        for k in $nsrsfound; do   
          /bin/sum $k | awk '{ print "'$k'",$0}' >> $of
        done
        if [ -f $i/nsr_ize ]; then
          n_v=:"`grep '^RELEASE=' $i/nsr_ize | awk -F= '{ print $2 }'`">> $of
          echo "Nsr_ize version: $n_v" >> $of
        else 
          echo "Nsr_ize not found.">> $of
        fi
        echo "$sep" >>  $of
      else
        :
        echo "no nsr stuff found in $i">> $of
      fi 
      if [ -f "$dbxexec" -a -z "$dbxfound" ]; then
		dbxfound="true"
		if [ "$myarch" = "solaris" ]; then 
		   if [ "$i" = "/usr/ucb" ]; then
			dbxfound=""
		   fi
		fi
	        dbxdir="$i"
      fi
    done
    if [ ! -n "$anyfound" ]; then
      :
      # echo no nsr executables found in \"$rcmdpath\" | fmt>> $of
    else
      execdirs=$nsrdirs
    fi
    echo "$sep"  >> $of
    echo >> $of
cd $currentdir
fi

echo "$sep"  >> $of
echo "Using \"$PATH\"" | $FOLD >> $of
echo "as command search path" >> $of
export PATH
echo "$sep"  >> $of

# Dmesg 
if [ "$dmesg" != "no" ]; then
{
  echo "$sep"
  echo "Dmesg:"
  echo "$sep"
  ( $DMESG  | head -300) 2>&1
  echo "$sep"
  echo
} >>$of
fi

# Grep for interesting strings in $logfiles
if [ "$grep" != "no" ]; then
  {
    # awk program to grep for strings in logfiles
    prog="`echo $greplist | tr ' ' '\012' | awk '{ print \"$0 ~/\" $1 \"/ { print $0 }\" } '`"
    echo "$sep"
    echo "Logfile greps:"
    echo "Looking for : $greplist"
    echo "$sep"
    echo
    for file in $logfiles /nsr/logs/messages; do
      echo "$sep"
      if [ -f "$file" ]; then
        echo "$file:"
        echo "$sep"
        awk "$prog" $file | awk '{ print "FOUND:" $0 }' 2>&1  | $FOLD
        echo "$sep"
      else
        echo "$sep"
        echo "! $file MISSING"
        echo "$sep"
      fi
      echo
    done     

    # do dmesg here too
    echo "$sep"
    echo "dmesg output:"
    echo "$sep"
    ( dmesg ) 2>&1 | awk "$prog" |  awk '{ print "FOUND:" $0 }' 2>&1 | $FOLD 
    echo "$sep"
    echo

  } >> $of
fi

# next, tail the logfiles
if [ "$logs" != "no" ]; then
  {
    echo "$sep"
    echo "log Files:"
    echo "$sep"
    echo
    for file in $logfiles; do
      echo "$sep"
      if [ -f "$file" ]; then
        echo "$file:"
        echo "$sep"
	if [ $howmuch -eq 0 ]; then
		cat $file | $FOLD
	else 
        	tail -$howmuch $file | $FOLD
	fi
        echo "$sep"
      else
        echo "$sep"
        echo "! $file MISSING"
        echo "$sep"
      fi
    echo
    done     
  } >> $of
fi

# These will work only on the server.
# do a quick check for serverdom
if [ ! -d $coredir ]; then
  echo "***** No cores directory found, this is a client"  >> $of
 
else  # Note, this is the begining of the server stuff

  if [ "$mm" != "no" ]; then
    {
    echo "$sep"
    echo "mminfo -avV" 
    echo "$sep"
    mminfo -avV
    echo "$sep"
    echo
    } >> $of
  fi
    # mminfo verbose output
  if [ "$mminfomv" = "yes" ]; then
    {
    echo "$sep"
    echo "mminfo -mvV" 
    echo "$sep"
    mminfo -mvV
    echo "$sep"
    echo
    } >> $of
  fi
  #  Print info which may be useful reguarding size, location of indexes
  if [ "$indexes" != "no" ]; then
    {
    echo "$sep"
    echo "/nsr/index location, space info" 
    echo "$sep"
    ls -ld /nsr
    ls -ld /nsr/index
    ls -ld /nsr/index/*
    echo "------------"
    echo "df"
    if [ "$myarch" = "solaris" ]; then
	df -k
    else  if [ "$myarch" = "sco" ]; then
	  df -v
    else
	  df
	  fi
    fi
    echo "$sep"
    echo
    } >> $of 
    
    #  Nsrls . . .
    {
    echo "$sep"
    echo "Nsrls output:"
    echo "$sep"
    nsrls
    echo "$sep"
    echo
    nsrls -f /nsr/index/*/db /nsr/mm/mmvolume
    }>> $of  
  fi 
  
  # check core files
  if [ "$cores" != "no" ]; then
    {
      if [ ! -n "$execdirs" ]; then
        # execdir not set
        execdirs=/usr/etc
      fi

      if [ ! -n "$maybecores" ]; then
         maybecores="$coredir/*/* /core /var/rap/core"
      fi

      echo "$sep"
      echo "Listing of core files:" 
      echo "$sep"
      test="`/bin/ls -d  $maybecores 2> /dev/null`"
      if [ ! -n "$test"  ]; then
        echo "no cores found in $maybecores"
        coresfound="no"
      else
        ls -ld $maybecores 2>/dev/null
        echo 
        echo "$sep"
        echo "Core Dumps:"
        echo "$sep"
        echo
        
        corefiles="`/bin/ls $maybecores 2> /dev/null |tr '\012' ' '`" 
        for corefile in  $corefiles; do 
          for dir in $execdirs; do
            # take out daemon name ( need this for adb )
            fileo="`file $corefile | grep ':.*core'`" 2>&1
            if [ ! -n "$fileo" ]; then
              file $corefile | grep ':.*data' > /dev/null 2>&1
              # this might be the rs6000 use the other way to get daemon name
              daemon="`expr $corefile : '/nsr/cores//*\([^/][^/]*\)/'`"
              if [ ! -n "$daemon" ]; then
                echo "$corefile not a corefile, or unable to get daemon name"
                file $corefile 2>&1
                 echo "$sep"
                continue
              fi 
            else 
              daemon=`expr "$fileo" : '.*'\''\(.*\)'\'`  2>&1
            fi

            if [ ! -f "$dir/$daemon" ]; then
                echo cannot find executable $dir/$daemon for file $corefile
                echo "$sep"
                continue  
            fi 

            echo "using $dir/$daemon for file $corefile" 2>&1
            echo '$c;$q' | adb $dir/$daemon $corefile  2>&1
            echo "$sep"
          done
        done  
  
        echo "$sep"
        echo; echo; 
      fi
    } >> $of
  fi
  
  # next, get the resfiles
  if [ "$res" != "no" ]; then
    {
      echo "$sep"
      echo "Resource Files:"
      echo "$sep"
      echo
      
      echo "Dynamic Attributes"  
      echo "$sep"
      echo

       (echo option hidden; echo option dynamic; echo print; echo quit) | nsradmin \
 -i  - 2>&1  &
      nsradminpid=$! 	
      count=1
      maxcount=20
      while [ "$count" -le "$maxcount" ]; do
	sleep 3
     	adminhung="`$PS $PS_SMALL | awk ' $1~ /'$nsradminpid'/ {print $1}'`"
	if [ -z "$adminhung" ]; then
		break
	fi
	count="`expr $count + 1`"
      done
      if [ -n "$adminhung"  ]; then
   	    kill -9 $nsradminpid	
      fi
		
      echo "$sep"
      echo

      for file in $resfiles; do
        echo "$sep"
        if [ -f "$file" ]; then
          echo "$file:"
          echo "$sep"
          cat $file | $FOLD 
        else
          echo "! $file MISSING"
        fi
        echo "$sep"
        echo
      done
    } >> $of  
  fi

#
#  run dbx on the nsr daemons.  the commands will be "where detach quit"
#
if [ "$dbx" != "no" ]; then
 if [ -n "$dbxfound" ]; then
   dbx_commands="where;detach;quit"
   if [ $myarch = "sco" ]; then
 	dbxexec="dbxtra"
	dbx_commands="dump;detach;quit"
   else if [ $myarch = "ibmrs6000" ]; then
	dbxexec="dbx"
   else dbxexec="dbx"
	fi
   fi		
dbxids=""
nsrexecs="nsrd nsrmmd nsrmmdbd nsrindexd"
  {
  # set these to something sane
    proclist="${proclist-nsr}"

    # get a listing of procids, procnames 
    if [ "$PS_SMALL" = "-e" ]; then
      # Sys5 style ps, command in column 4, PID in 1
      prog="`echo $proclist | tr ' ' '\012' | awk '{ print \"$4 ~/\" $1 \"/ { print $1 \\"=\\" $4 }\" } '`"
    else
      # bsd style ps, command in column 5, PID in 1
      prog="`echo $proclist | tr ' ' '\012' | awk '{ print \"$5 ~/\" $1 \"/ { print $1 \\"=\\" $5 }\" } '`"
    fi

    ptab="`$PS $PS_SMALL | awk "$prog" | sort -u | tr '\012' ' '`"
    dbxids=""
    proctab=""

    echo "$sep" 
    echo "Running $dbxdir/$dbxexec on ..... "
    echo $ptab | tr '=' ':' 2>&1 | $FOLD
    echo "$sep"
      
    for proc in $ptab; do
      procid="`expr $proc : '^\([^=][^=]*\)='`"
      if [ $? != 0 ]; then
        echo "failed to get procid" 1>&2
        continue 
      fi
      procname="`expr $proc : '.*=\([^=][^=]*\)$'`"
      if [ $? != 0 ]; then
        echo "failed to get procname"  1>&2
        continue 
      fi

      # put dbx in the background; just in case it hangs
       
      echo "$procname:$procid"
      dbxproc=""
       
      # this for instances where the full pathname appears in procname 
      # if it does appear then $nsrhome/$procname will not work
      # instead of special casing this I have decided to grep for the nsr daemon
       
      procis=$procname
      for i in $nsrexecs
      do
	    procis="`echo $procname | grep -i $i`"
	    if [ $procis ]; then
		dbxproc=$i
	        break
	    fi
      done
      if [ "$myarch"  = "ibmrs6000" ]; then
	echo "$dbx_commands" | "$dbxexec" -a "$procid" 2>&1  &
      else
	if [ "$myarch" = "sco"  ]; then
	echo "$dbx_commands" | "$dbxexec" -s  "$nsrhome"/"$dbxproc" "$procid" 2>&1  &
      else
      echo "$dbx_commands" | "$dbxexec" "$nsrhome"/"$dbxproc" "$procid" 2>&1  &
       fi
	fi
      child=$!
      # add to list of dbxids
      dbxids="$dbxids $child"
      proctab="$proctab $proc=$child"
      sleep 12 
      echo ""
    done
    
    # insure that each one is dead . . .
    for proc in $proctab; do
      # check that each proc is dead: if not 
      dead=1
      procid="`expr $proc : '^\([^=][^=]*\)='`"
      if [ $? != 0 ]; then
        echo "failed to get procid" 1>&2
        continue 
      fi
      procname="`expr $proc : '[^=][^=]*=\([^=][^=]*\)='`"
      if [ $? != 0 ]; then
        echo "failed to get procname" 1>&2
        continue 
      fi
      dbxid="`expr $proc : '[^=][^=]*=[^=][^=]*=\([^=][^=]*\)$'`"
      if [ $? != 0 ]; then
        echo "failed to get procname" 1>&2
        continue 
      fi
      $PS $PS_SMALL | awk '{print $1}' | grep $dbxid > /dev/null 2>&1
      if [ $? = 0 ]; then
        for i in $dbxid; do 
          kill -TERM $dbxid
          $PS $PS_SMALL | awk '{print $1}' | grep $dbxid > /dev/null 2>&1
          dead=$?
          echo "reached inside of for loop due to unstop. $dead"
          if [ $dead != 0 ];then
            break
          fi
        done
      fi
      if [ $dead = 0 ];then
        kill -KILL $dbxid
        $PS $PS_SMALL | awk '{print $1}' | grep $dbxid > /dev/null 2>&1
        dead=$?
      fi
      if [ $dead = 0 ]; then
        echo "unable to kill process $dbxid" 1>&2
      fi
    done      
	echo "$sep"
	echo 
 } >> $of

else
	echo "No $dbxexec found in PATH" >> $of

fi  # end of $dbxfound 

fi  # end of $dbx != no 

#
#   if there is a jukebox then get some info
#
echo "$sep" >> $of
echo "Jukebox information " >> $of
echo "$sep"  >> $of
jukeboxes="no"
for i in 0 1 2 3 4 5 6 7 
do 
     
     if [ -c /dev/psc$i ]; then
	jukeboxes="yes"
	break
     fi
done
if [ $jukeboxes = "yes" ]; then
   echo "ls -l /dev/psc*"   >> $of		
   echo  >> $of
   ls -l /dev/psc* >> $of  2>&1 
   echo >> $of
   echo "ls -l /dev/sji*"  >> $of	
   echo  >> $of
   ls -l /dev/sji*   >> $of  2>&1
   echo "$sep" >> $of
   echo  >> $of 
   if [ "$myarch" = "bsdsunos" ]; then
	echo "$sep"  >> $of
        echo "modstat " >> $of
	echo "$sep"  >> $of
	modstat >> $of  2>&1
	echo "$sep" >> $of
	echo 
   else 
	if [ "$myarch" = "ibmrs6000" ]; then
	   echo "lsdev -C -s scsi"  >> $of
	   lsdev -C -s scsi  >> $of  2>&1
	   echo >> $of

	else if [ "$myarch" = "solaris" ]; then
	     echo "modinfo "  >> $of
	     modinfo | egrep 'ato|psc'  >> $of
	     echo >> $of
	      fi
	fi
   fi 
   
   echo "$sep" >> $of
   echo "pscinfo -c INQUIRY /dev/psc*" >> $of
   echo "$sep" >> $of
   for i in 0 1 2 3 4 5 6 7 
   do
	pscinfo -c INQUIRY /dev/psc$i  >> $of  2>&1
   done
   echo "$sep" >> $of
   echo  >> $of
	
else echo "No jukebox found"  >> $of

fi # if jukebox


fi  # note this is the end of the server stuff


# Lastly, do some cleanup, mail if necessary

if [ "$compress" = "yes" -o "$mail" = "yes" ]; then
         compress -c $tempout | uuencode nsrsupport.Z > $of.Z.uu
	 of="$of.Z.uu"
fi
  
if [ "$mail" = "yes" ]; then
  :
  /usr/ucb/mail -s "Incident $incidentnum" $sendto < $of
fi

if [ "$save" = "no" -a -n "$of" ]; then
   if [ "$compress" = "no" -a "$mail" != "yes" ]; then
       /bin/rm -f $of
    else echo "The compressed and uuencoded file is in $of"
    fi
fi
