#!/bin/csh -f

# GRAPE-7 neighbor search functionality test program (checknb)
# version 2.1    17-Jan-2008

cat << DESC_END
-----------------------------------------------------------------
 GRAPE-7 neighbor search functionality test program (checknb)

 usage: ./checknb [anim]

 anim: test with animation (not recommended for strict check).
-----------------------------------------------------------------

This script checks the neighbor search function of G5nb backend design
of GRAPE-7. It performs many-body simulations and compare their
results with precalculated ones. it can be used for check with a
single card only.

DESC_END

set cmdname  = $0:t;
set rootpath = $0:h/../..
pushd $rootpath > /dev/null
set rootpath = `pwd`
popd > /dev/null

set ncard = `cat ${rootpath}/.nhib`
set devid = 0
if ( $?GDEVICE ) then
  setenv G5_CARDS "$GDEVICE"
endif
if ( $?G5_CARDS ) then
    # set the device ID of the 1st card to 'devid'.
    set devid = `echo $G5_CARDS | sed 's/\([0-9]\).*$/\1/g'`
endif

set model = `${rootpath}/hibutil/lsgrape -d ${devid} | grep -v devid | sed 's/.*model\([0-9]\+\).*$/\1/g'`
echo model${model}

switch (${model})
  case 100:
  case 300:
  case 600:
    ${rootpath}/g7/test/checknbsub
  breaksw

  case 800:
    if ( $?G5_CARDS ) then
        set cards = ($G5_CARDS)
    else
        set cards = (0 1 2 3)
    endif

    echo test model${model} card\[${cards}\]

  set i = 1
  while (${i} <= ${#cards})
    (setenv G5_CARDS "$cards[$i]" ; setenv GDEVICE "$cards[$i]" ; ${rootpath}/g7/test/checknbsub800 $cards[$i] ${argv}) &
    @ i++
  end # i

  breaksw

endsw

onintr LABEL_HUP

# wait until all sub programs are done.
set nruns = 1
while (${nruns} > 0)
  set nruns = `ps x | pgrep checknbsub | wc | awk '{print $1}'`
  sleep 1
end

exit 0

LABEL_HUP:
killall checknbsub800 directnb_g7 directnba_g7 >& /dev/null
