#!/bin/csh -f

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

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

set tmpdir = ${rootpath}/tmp
set sampledir = ${rootpath}/sample/direct/snapshots
set mkdist = ${rootpath}/sample/direct/mkdist

set logfile = ${rootpath}/check.log
/bin/rm ${logfile} >& /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'`
    set ncard = `echo $G5_CARDS | wc | awk '{print $2}'`
endif

if (${ncard} == 1) then
    goto LABEL_START
endif

cat << MCWARN_END

[For Multiple Cards Users:]
Be sure to set environment variable "G5_CARDS" to specify a single
card. For example, in order to test a card with device ID 2, you
should set the variable as:

  setenv G5_CARDS "2"

The test would fail if multiple cards are specified.

MCWARN_END

echo -n "Continue? (y/n): "
set answer = $<
if ( $answer == "y") then
  goto LABEL_START
else
  exit (1)
endif

LABEL_START:

echo "Start the tests. It may take a few minutes." |& tee -a ${logfile}
echo '' |& tee -a ${logfile}

if ($1 == "anim" || $2 == "anim") then
  set anim = y
  set direct = ${rootpath}/sample/direct/directtesta_g7
else
  set anim = n
  set direct = ${rootpath}/sample/direct/directtest_g7
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:
    set infile = (pl1k pl4k)
    set nbody  = (1024 4095)
  breaksw

  case 300:
    set infile = (pl1k pl12k)
    set nbody  = (1024 12285)
  breaksw

  case 600:
    set infile = (pl1k pl24k)
    set nbody  = (1024 24570)
  breaksw

  case 800:
    if ( $?G5_CARDS ) then
      if ($ncard == 1) then
        set infile = (pl1k pl8k)
        set nbody  = (1024 8190)
      else if ($ncard == 2) then
        set infile = (pl1k pl16k)
        set nbody  = (1024 16380)
      else if ($ncard == 3) then
        set infile = (pl1k pl24k)
        set nbody  = (1024 24570)
      else
        set infile = (pl1k pl32k)
        set nbody  = (1024 32760)
      endif
    else
      set infile = (pl1k pl32k)
      set nbody  = (1024 32760)
    endif
  breaksw

endsw


set i = 1
while (${i} <= ${#infile})
  set endt = 100    
  set snapfile = ${tmpdir}/tmp$$_${infile[${i}]}T${endt}.snap
  set outfile =  ${tmpdir}/tmp$$_${infile[${i}]}T${endt}.log
  echo '---------- in:' ${infile[${i}]}  'out:' ${snapfile} 'endt:' ${endt} '----------'\
      |& tee -a ${logfile}

  if (! -f ${tmpdir}/${infile[${i}]}) then
    ${mkdist} ${nbody[${i}]} 1 ${tmpdir}/${infile[${i}]}
  endif

  ${direct} ${tmpdir}/${infile[${i}]} ${snapfile} ${endt} |& tee ${outfile}
  set s = ${status}
  if (${s} == 1) then
    echo Abort. |& tee -a ${logfile}
    exit 1
  endif

  grep "floating-point" ${outfile}
  set s = ${status}
  if (${s} == 0) then
    set pipetype = fp
  else
    set pipetype = log
  endif

  grep 'g5_nbsearch:available' ${outfile}
  set s = ${status}
  if (${s} == 0) then
    set pipetype = nb
  endif

  set samplefile = ${sampledir}/model${model}${pipetype}${infile[${i}]}T${endt}.snap
  diff ${snapfile} ${samplefile} > /dev/null
  set s = ${status}
  if (${s} == 0) then
    echo OK |& tee -a ${logfile}
    /bin/rm ${snapfile}
    /bin/rm ${outfile}
  else
    echo NG |& tee -a ${logfile}
    echo check failed. |& tee -a ${logfile}
    exit 1
  endif

  @ i++
end # i

echo ""  |& tee -a ${logfile}
echo "Passed all tests." |& tee -a ${logfile}

# chmod a+w ${logfile}

exit 0
