#!/bin/csh -f

# GRAPE-9 functionality test program (check)

cat << DESC_END
-----------------------------------------------------------------
 GRAPE-9 functionality test program for G5PIPE
   usage: check_g5
-----------------------------------------------------------------
DESC_END

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

if ( $?GDEVICE ) then
    # set the device ID of the 1st card to 'devid'.
    set devid = `echo $GDEVICE | sed 's/\([0-9]\).*$/\1/g'`
    set devs = ($GDEVICE)
else
    set devid = 0
    set ndev = `${rootpath}/hibutil/lsgrape | wc -l`  # num of dev + 1
    @ ndev--
    set devs = ()
    set i = 0
    while (${i} < ${ndev})
        set devs = (${devs} ${i})
        @ i++
    end
endif

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

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

set s9bin = ./s9_g9

# now the following variables are set.
#   rev   : The iFPGA revision of the 1st device in use.
#   devs  : A list of device IDs of devices in use.

echo "Start check for ${#devs} device(s) (revision: ${rev}, device IDs: ${devs})..."

#
# tests on each device.
#
set tmpdir = ${rootpath}/tmp
set initdir = ${rootpath}/init
set i = 1
while (${i} <= ${#devs})
    set okfile = ${tmpdir}/device$devs[$i].ok
    set ngfile = ${tmpdir}/device$devs[$i].ng
    (setenv GDEVICE "$devs[$i]" ; \
     setenv GREVISION "${rev}" ; \
     setenv GNDEVICE ${#devs} ; \
     rm ${okfile} ${ngfile} >& /dev/null ; \
     ${rootpath}/g9/test/checksub_g5 ${argv}) &
    sleep 1
    @ i++
end

onintr LABEL_HUP

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

set allok = 1
set i = 1
while (${i} <= ${#devs})
    set okfile = ${tmpdir}/device$devs[$i].ok
    set ngfile = ${tmpdir}/device$devs[$i].ng
    if ( -f ${ngfile}) then
        set allok = 0
    else if (! -f ${okfile}) then
        set allok = 0
    endif
    @ i++
end

if (${allok} == 0) then
    echo ""
    echo "Check failed."
    echo ""
    cd ${rootpath}
    exit 1
endif

#
# Tests using all devices.
#
if (${#devs} == 1) then
    # do not perform this test for a single device system.
    cd ${rootpath}
    exit 0
endif

echo ""
echo "Start tests using all devices."
echo ""

setenv GDEVICE "$devs"
setenv GREVISION "${rev}"
set tmpdir = ${rootpath}/tmp
set tmpout = ${tmpdir}/tmpouts9.all
cd ${rootpath}/sample/s9

@ ndevMaxN = 16 * ${#devs}
set ndevMaxN = ${ndevMaxN}k

foreach n (1k ${ndevMaxN})
# foreach n (1k)
  set ntry = 0
  while (${ntry} < 3)
    echo "Start ${s9bin} ${n} try${ntry} devices: ${devs}..."
    set infile=${initdir}/pl${n}
    if (! -f ${infile}) then
      echo ""
      echo "file ${infile} not found."
      echo "in order to generate it, run ${rootpath}/script/install."
      echo ""
      echo "Check failed."
      echo ""
      exit 1
    endif

    switch (${n})
     case "1k":
      echo 128.0 256.0 10.0 1.0 ${initdir}/pl1k 2 8.0 7 > ${tmpdir}/inputpara9.all
      breaksw
     case *:
      echo 128.0 256.0 1.0 0.1 ${initdir}/pl${n} 2 8.0 7 > ${tmpdir}/inputpara9.all
      breaksw
    endsw

    ${s9bin} ${tmpdir}/inputpara9.all |& tee ${tmpout}.try${ntry} | grep 'time'
    grep -v -E "cputime|nplus|closed|opened|devid|temp|g5" ${tmpout}.try${ntry} > ${tmpout}.try${ntry}striped
    if (${ntry} != 0) then
      diff -w ${tmpout}.try0striped ${tmpout}.try${ntry}striped
      set s = ${status}
      if (${s} == 0) then
        echo Devices:${devs}    ${s9bin} ${n} try${ntry} OK
      else
        echo Devices:${devs}    ${s9bin} ${n} try${ntry} NG
        echo ""
        echo "Check failed."
        echo ""
        cd ${rootpath}
        exit 1
      endif
    endif

    @ ntry++
  end
end

echo ""
echo "All devices passed all tests."
echo ""
cd ${rootpath}
exit 0

LABEL_HUP:
killall checksub_g5 s9_g9 >& /dev/null
