#!/bin/csh -f

# GRAPE-7 functionality test program (check)
# version 2.1    18-Jan-2008

cat << DESC_END
-----------------------------------------------------------------
 GRAPE-7 functionality test program (check)

 usage: check [anim]

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

This script checks the function of G5 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 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/checksub
  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/checksub800 $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 checksub | wc | awk '{print $1}'`
  sleep 1
end

exit 0

LABEL_HUP:
killall checksub800 directtest_g7 directtesta_g7 >& /dev/null
