#!/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}/checknb.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/directnba_g7
else
  set anim = n
  set direct = ${rootpath}/sample/direct/directnb_g7
endif

set rad = 0.2

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

set model = 800

set infile = (pl1k pl4k)
set nbody  = (1024 4095)

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

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

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

  grep 'g5_nbsearch:available' ${outfile}
  set s = ${status}
  if (${s} == 0) then
    set pipetype = nb
  else
    echo NG |& tee -a ${logfile}
    echo neighbor search function not available. |& tee -a ${logfile}
    echo check failed. |& tee -a ${logfile}
    exit 1
  endif

  mv ${tmpname} ${snapfile}
  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} Force calculation: OK |& tee -a ${logfile}
  else
    echo devid ${devid} Force calculation: NG |& tee -a ${logfile}
    echo check failed. |& tee -a ${logfile}
    exit 1
  endif

  mv ${tmpname}.nb ${nbsnapfile}
  set samplefile = ${sampledir}/model${model}x${ncard}${pipetype}${infile[${i}]}T${endt}.nb
  diff ${nbsnapfile} ${samplefile} > /dev/null
  set s = ${status}
  if (${s} == 0) then
    echo devid ${devid} Neighbor search: OK |& tee -a ${logfile}
    /bin/rm ${snapfile}
    /bin/rm ${nbsnapfile}
    /bin/rm ${outfile}
  else
    echo devid ${devid} Neighbor search: 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}

exit 0
