#!/bin/csh -f

echo ''
echo -----------------------------------------------
echo  PGPG2 software package installation program
# echo  version 0.0    17-Dec-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_NCARDS:
echo ''
echo -n "How many GRAPE-7 cards are you installing?: "
set ncards = $<
if (${ncards} < 1) then
  echo incorrect number ${ncards}
  goto LABEL_NCARDS
endif

echo ${ncards} > nhib
cp nhib nxhib
echo 0 > nehib

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 cards: ${ncards}"
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 ${ncards}
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 PGPG2 utilities...
echo -----------------------------
echo ''
cd src
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 install a GRAPE-7 device driver'
echo 'following the procedure below:'
echo ''
echo '(1) [Root Permission Required]'
echo '    Change directory to pg2pkg/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 pg2pkg/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 "NOTE THAT THIS PROCEDURE IS NECESSARY EVERYTIME YOU RESTART THE HOST COMPUTER."
echo  -------------------------------------------------------------------------------
echo ''
