#!/bin/sh
# qview: view the queuing system environment
# could be far more efficient
# kh	version 2.0	 6 Aug 1993
# kh	version 2.1	14 Dec 1993
# known bug: ps -elf only shows 132 chars per line
#   thus a long pathname breaks qview
#   this is bad because qstart -g will now start up
#   a new qxd every time

usage='qview: usage: qview [-h] [ queue(s) ]'
header=0

# bin, queue and spool directory

qx_dir=/bg/gen/local/bin 
queue_dir=/bg/gen/local/dat
spool_dir=/tmp/spool

# test arguments

if [ "$1" = -h ]; then header=1; shift 1; fi

if [ "$1" = "?" ]; then echo $usage; exit 2; fi

if [ $# -eq 0 ]
    then for queue in $queue_dir/*.dat; do
    	    queue=`basename $queue`
	    queue=`echo $queue | cut -d'.' -f1`
	    if [ "$queue" = "*" ]; then break; fi
	    all=$all' '"$queue"
	done
fi

# how to investigate qx daemons

psflags=-elf
if [ -x sun ]; then if sun; then psflags=-aux; fi; fi

qxd_ps() {
	it=`ps $psflags | grep -v grep | grep qxd | grep "\-q $1 "`
	it2=`echo "$it" | cut -c4`
	# join letters on one line
        echo $it2 | nawk ' { printf "%s", $0 }' | tr -d " " | tr -d " \t"
}

qxd_qstat() {
	total=`$qx_dir/qstat $1 | grep \^total | cut -d' ' -f3`
	nents=`expr $total \- 1`
	started=`$qx_dir/qstat $1 | grep \^initialized | cut -d' ' -f2-`
}

# do your job

job() {
    if [ -f $queue_dir/$1.dat ]; then
      status=`qxd_ps $1`
      if [ -z "$status" ] ; then status='?' ; fi
      qxd_qstat $1
      dat=`ls -l $queue_dir/$1.dat | cut -c31-41`
      idx=`ls -l $queue_dir/$1.idx | cut -c31-41`
      echo "$1\t$status\t$nents\t$dat  $idx  $started"
    else continue
    fi
}

# looping if required

test $header -ne 0 && echo "QUEUE\tQXDs\tNENTS\t       DAT          IDX   STARTED"

if [ -n "$all" ]; then
  for queue in $all; do job $queue; done
else
  for i do job $i; done
fi

exit 0
