#!/bin/ksh
#
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Shannon Davidson
#                                                 Raytheon
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__

set +u

usage() {
   print -u2 " "
   print -u2 "usage: gmps [-a] [-h] [-k] [-v] [-p port]"
   print -u2 " "
   print -u2 "  -a   Get info for all hosts"
   print -u2 "  -h   Display this usage information"
   print -u2 "  -k   Kill processes"
   print -u2 "  -v   Verbose output (pid and port)"
   print -u2 "  -p   Query for this port number"
   print -u2 " "
}

gethosts() {
   $gm_board_info | grep "^  [ 0-9][0-9] " | awk '{ print $3 }' | sort
}

gm_board_info=$(whence gm_board_info)
if [ "$gm_board_info" = "" ]; then
   print -u2 gm_board_info is not in your path
   exit 1
fi

gmport="[24567]"
verbose=0
kill=0
allhosts=0
portopt=0
while [ "$1" != "" ]; do

   case "$1" in
      -v) verbose=1;;
      -k) kill=1;;
      -a) allhosts=1;;
      -p) portopt=1;gmport=$2;shift;;
      -h) usage; exit 0;;
      *) print -u2 gmps: Unknown option - $1; usage; exit 1;;
   esac

   shift
done

if [ $allhosts -eq 1 ]; then
   hosts=$(gethosts)
   gmps=$(whence gmps)
   if [ "$gmps" = "" ]; then
      print -u2 gmps is not in your PATH
      exit 2
   fi
   gmpath=$(dirname $gm_board_info)
   gmopts=
   [ $verbose -eq 1 ] && gmopts="$gmopts -v"
   [ $kill -eq 1 ] && gmopts="$gmopts -k"
   [ $portopt -eq 1 ] && gmopts="$gmopts -p $gmport"
   for h in $hosts; do
      print "$h:"
      rsh $h "PATH=$PATH:$gmpath; export PATH; $gmps $gmopts" 2>/dev/null
   done
   exit 0
fi

pids="$($gm_board_info | egrep "$gmport: *BUSY" | awk '{ print $3; }')"

if [ $kill -eq 1 ]; then
   for a in $pids; do
      kill $a
   done
fi

if [ "$pids" != "" ]; then
   if [ $verbose -eq 1 ]; then
      print "   PID PORT"
      $gm_board_info | egrep "$gmport: *BUSY" | awk '{ printf("%6d %s\n", $3, substr($1, 0, 1)); }'
   else
      print -- "$pids"
   fi
fi


