#!/bin/csh -f

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

cat << DESC_END
-----------------------------------------------
 GRAPE reconfiguration program
 usage: ${cmdname} [-n|-noexec] [args...]
-----------------------------------------------
DESC_END

if ("$1" == "-n" || "$1" == "--noexec") then
  shift
  set doexec = 0
else
  set doexec = 1
endif

set devid = 0
if ( $?GDEVICE ) then
    # set the device ID of the 1st card to 'devid'.
    set devid = `echo $GDEVICE | sed 's/\([0-9]\).*$/\1/g'`
endif

set gtype = `${rootpath}/hibutil/lsgrape -d ${devid} | \
             grep -v devid | \
             sed 's/[^a-zA-Z]*\(GRAPE[^(]*\).*$/\1/g' |\
             sed 's/.*\(null\).*$/\1/g'`

echo GRAPE product ID : ${gtype}

switch (${gtype})
  case GRAPE-9
    set exe = ${rootpath}/g9/test/reconfig
    breaksw
  case GRAPE-7
    set exe = ${rootpath}/g7/test/config800
    breaksw
  case *:
    echo "No reconfigurable GRAPE found."
    exit 1
    breaksw
endsw

echo "run ${exe} ${argv}"
if ($doexec == 1) then
  ${exe} ${argv}
endif

exit 0
