#!/bin/csh -f

echo ''
echo -----------------------------------------------
echo  GRAPE-7 software package installation program
# echo  version 0.4    19-Apr-2007
echo -----------------------------------------------

LABEL_RETRY:
echo ''
echo -n "Using Linux kernel version 2.6 or later? (y/n): "
set answer = $<
if ( $answer == "y") then
  set kernelver = 26
else if ( $answer == "n") then
  set kernelver = 24
else
  goto LABEL_RETRY
endif

LABEL_NXCARDS:
echo ''
echo -n "How many GRAPE-7X cards are you installing?: "
set nxhib = $<
if (${nxhib} < 0) then
  echo incorrect number ${nxhib}
  goto LABEL_NXCARDS
endif
echo ${nxhib} > nxhib

LABEL_NECARDS:
echo ''
echo -n "How many GRAPE-7E cards are you installing?: "
set nehib = $<
if (${nehib} < 0) then
  echo incorrect number ${nehib}
  goto LABEL_NXCARDS
endif
echo ${nehib} > nehib

set nhib = ${nxhib}
@ nhib +=  ${nehib}
if (${nhib} < 1) then
  echo incorrect number ${nhib}
  goto LABEL_NXCARDS
endif
echo ${nhib} > nhib

echo ''
echo "Confirm your choice."
if (${kernelver} == 26) then
  echo "  Linux kernel version: 2.6 or later"
else
  echo "  Linux kernel version: 2.5 or earlier"
endif
echo "  number of GRAPE-7X cards: ${nxhib}"
echo "  number of GRAPE-7E cards: ${nehib}"
echo "  number of cards in total: ${nhib}"
echo -n "Are they correct? (y/n): "
set answer = $<
if ( $answer != "y") then
  goto LABEL_RETRY
endif

echo ''
echo -------------------------------
echo Preparing for installation...
echo -------------------------------

cd driver;
./nhib.csh ${nxhib} ${nehib}
if (${kernelver} == 26) then
  cp Makefile.2.6 Makefile
else
  cp Makefile.2.4 Makefile
endif
cd ..

echo ''
echo done


echo ''
echo ------------------------------------
echo Installing GRAPE-7 device driver...
echo ------------------------------------
echo ''

cd driver
make clean >& /dev/null
make
set s = ${status}
cd ..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

echo ''
echo done
echo ''

echo ''
echo --------------------------------------------
echo Installing Host Interface Bridge library...
echo --------------------------------------------
echo ''
cd hibutil
make clean >& /dev/null
make
set s = ${status}
cd ..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

echo ''
echo done
echo ''

echo ''
echo -----------------------------
echo Installing G5 library...
echo -----------------------------
echo ''
cd g5util
make clean >& /dev/null
make
set s = ${status}
cd ..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

echo ''
echo done
echo ''

echo ''
echo -------------------------------------------------------
echo "Installing a sample code..."
echo " (direct-summation algorithm in C)"
echo -------------------------------------------------------
echo ''
cd direct
make clean >& /dev/null
make
set s = ${status}
cd ..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

#
echo ''
echo -------------------------------------------------------
echo "Installing a sample code..."
echo " (direct-summation algorithm in Fortran77)"
echo -------------------------------------------------------
echo ''
cd directf77
make clean >& /dev/null
make
set s = ${status}
cd ..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

#
echo ''
echo  ----------------------------------
echo "Installing a sample code..."
echo "(Barnes-Hut tree algorithm in C)"
echo  ----------------------------------
echo ''
cd vtc
make clean >& /dev/null
make
set s = ${status}
cd ..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

#
echo ''
echo done
echo ''
echo  -------------------------------------------------------------------------------
echo 'All installation process has been completed.'
echo 'Now you can check the functions of GRAPE-7 card(s)'
echo 'following the procedure below:'
echo ''
echo '(1) [Root Permission Required]'
echo '    Change directory to g7pkg/driver, and then type'
echo '      make installmodule'
echo '    This will plug in the device driver into the Linux kernel.'
echo ''
echo '(2) [Root Permission Required]'
echo '    Change directory to g7pkg/hibutil, and then type'
echo '      ./setmtrr.csh'
echo '    This will set MTRR (memory type range register) of the host'
echo '    computer, causing significant improvement of data transfer speed.'
echo ''
echo '(3) [Model300/600 Users Only]'
echo '    Change directory to g7pkg/hibutil, and then type'
echo '      ./lsgrape'
echo '    This will show all GRAPE add-in cards and their device ID.'
echo '    To reconfigure one of them, Change directory to g7pkg/g5util/config,'
echo '    and then type'
echo '      ./config [device ID]'
echo '    This will reconfigure pFPGAs on the device_id-th card.'
echo '    This step is not necessary for GRAPE-7 model100.'
echo ''
echo '(4) Change directory to g7pkg, and then type'
echo '      ./script/check.csh'
echo '    This will run a test program.'
echo '    Follow its instruction to see if your card is working correctly or not.'
echo ''
echo ''
echo "NOTE THAT STESP (1)-(3) ARE NECESSARY EVERYTIME YOU RESTART THE HOST COMPUTER."
echo  -------------------------------------------------------------------------------
echo ''
