#!/bin/csh -f

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

cat <<EOF
-----------------------------------------------------------------
 GRAPE-7 functionality test program (longcheck)

 usage: longcheck [ntry] [direct|tree|toggle]
-----------------------------------------------------------------

This script checks the function of G5PIPE backend design of GRAPE-7.
It repeats many-body simulation ntry times, and compare the result
each other. It can be used for check of a system with multiple cards
of different models (eg. model100x2+model300).

EOF

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/longchecksub ${argv}
  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/longchecksub800 $cards[$i] ${argv}) &
    @ i++
  end # i

  breaksw

endsw

onintr LABEL_HUP

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

exit 0

LABEL_HUP:
killall longchecksub800 directtest_g7 vtc_g7 >& /dev/null
