#!/bin/csh -f

set devid = $1
set ncard = 1

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.devid${devid}.log
/bin/rm ${logfile} >& /dev/null

echo "Start the tests for devid${devid}. 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 = 800
set infile = (pl1k pl8k)
set nbody  = (1024 8190)

set i = 1
while (${i} <= ${#infile})
  set endt = 100    
  set snapfile = ${tmpdir}/tmp$$_${infile[${i}]}T${endt}.devid${devid}.snap
  set outfile =  ${tmpdir}/tmp$$_${infile[${i}]}T${endt}.devid${devid}.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 devid ${devid} OK |& tee -a ${logfile}
    /bin/rm ${snapfile}
    /bin/rm ${outfile}
  else
    echo devid ${devid} NG |& tee -a ${logfile}
    echo check failed. |& tee -a ${logfile}
    exit 1
  endif

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

  @ i++
end # i

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

# chmod a+w ${logfile}

exit 0
