#!/bin/csh -f

#
# usage: install [gpcie]
#
set pcieip = "plda"
if ($1 == "gpcie") then
  set pcieip = "gpcie"
endif

echo ''
echo '--------------------------------------------'
echo 'GRAPE Software Package Installation Program'
echo '--------------------------------------------'
echo ''

set install_gdr  = 'no'
set install_g9   = 'no'
set install_g7   = 'no'
set install_g6a  = 'no'
set install_g6bx = 'no'
set install_pg5  = 'no'
set install_cuda = 'no'

if (-d cuda) then
  set hascuda = 1
else
  set hascuda = 0
endif

LABEL_RETRY:
echo '  -----------------------------'
echo '                      install?'
echo '  1) GRAPE-9          ' $install_g9
echo '  2) GRAPE-DR         ' $install_gdr
echo '  3) GRAPE-7          ' $install_g7
echo '  4) GRAPE-6A         ' $install_g6a
echo '  5) GRAPE-6BX        ' $install_g6bx
echo '  6) Phantom-GRAPE-5  ' $install_pg5
if ($install_pg5 == 'yes') then
  echo '       CPU arch  : '$march
  echo '       CPU cores : '$maxdev
  echo '       OpenMP    : '$openmp
endif
if ($hascuda) then
echo '  7) CUDA G5/G6       ' $install_cuda
endif
if ($install_cuda == 'yes') then
  echo '       CUDA path : '$cudapath
  echo '       SDK path  : '$cudasdkpath
  if ($?GPUARCH) then
    echo '       GPU arch  : '$GPUARCH
  else
    echo '       GPU arch  : not specified.'
    echo ''
    echo '         For better performance, you may want to specify'
    echo '         the architecture of the GPU via an evironment variable "GPUARCH".'
    echo '         Valid architecuture names include: sm_10, sm_12, sm_13, sm_20 and sm_21.'
    echo '           eg) sm_10:GeForce 8800 GTX'
    echo '               sm_13:GeForce GTX 285, GTX 295'
    echo '               sm_20:GeFroce GTX 480, Tesla C2050'
    echo '               sm_21:GeForce GTX 460'
    echo '         See CUDA documents for the complete list.'
    echo '         You can check the architecture of the board installed in your system by running:'
    echo '         '$cudasdkpath'/C/bin/linux/release/deviceQuery.'
    echo '         Its output contains CUDA capability major & minor revision numbers N & M,'
    echo '         which indicate that the architecture of your board is sm_NM.'
  endif
endif
echo ''
echo '  i) Start installation.'
echo '  q) Quit this program.'
echo '  -----------------------------'
echo ''
echo '  input numbers to select GRAPE device(s) to be installed.'
echo '  (e.g. input 1 3 4 to install GRAPE-DR, GRAPE-6A, and GRAPE-6BX)'
echo '  "i" to start installation.'
echo '  "q" to quit this script.'
echo ''
echo -n 'Input your choice : '
set answers = ($<)

set i = 1

LABEL_MORE:

switch ($answers[$i])
case "1"
  set install_g9 = 'yes'
  breaksw

case "2"
  set install_gdr = 'yes'
  breaksw

case "3"
  set install_g7 = 'yes'
  cat << G7WARN_END

  ======================================================================
  [For GRAPE-7 model300/600 user]
  Configuration files (g7/config/*.ttf) may not included this package.
  Please check http://www.kfcr.jp/grape7.html for the latest .ttf files.
  ======================================================================

G7WARN_END
  breaksw

case "4"
  set install_g6a = 'yes'
  breaksw

case "5"
  set install_g6bx = 'yes'
  breaksw

case "6"
  set install_pg5 = 'yes'
  setenv maxdev 4
  goto LABEL_PG5_CONF
LABEL_PG5_CONF_DONE:
  breaksw

case "7"
  set install_cuda = 'yes'
  goto LABEL_CUDA_CONF
LABEL_CUDA_CONF_DONE:
  breaksw

case "i":
  goto LABEL_CONFIRM
  breaksw

case "q":
  exit 0
  breaksw

default:
  echo 'Answer with number(s), i, or q.'
  echo ''
  goto LABEL_RETRY
  breaksw
endsw

@ i++

if ($i <= ${#answers}) then
  goto LABEL_MORE
else
  goto LABEL_RETRY
endif


LABEL_CONFIRM:
echo ''
echo 'Device to be installed :'
if ($install_g9 == 'yes') then
  echo '    GRAPE-9'
endif
if ($install_gdr == 'yes') then
  echo '    GRAPE-DR'
endif
if ($install_g7 == 'yes') then
  echo '    GRAPE-7'
endif
if ($install_g6a == 'yes') then
  echo '    GRAPE-6A'
endif
if ($install_g6bx == 'yes') then
  echo '    GRAPE-6BX'
endif
if ($install_pg5 == 'yes') then
  echo '    Phantom-GRAPE-5'
  echo '       CPU arch  : '$march
  echo '       CPU cores : '$maxdev
  echo '       OpenMP    : '$openmp
endif
if ($install_cuda == 'yes') then
  echo '    CUDA G5/G6'
  echo '       CUDA path : '$cudapath
  echo '       SDK path  : '$cudasdkpath
  if ($?GPUARCH) then
    echo '       GPU arch  : '$GPUARCH
  else
    echo '       GPU arch  : not specified.'
  endif
endif
echo ''

echo -n "Are they correct? (y/n) : "
set answer = $<
if ( $answer == "n") then
  goto LABEL_RETRY
else if ( $answer != "y") then
  echo ''
  echo "Answer with 'y' or 'n'."
  echo ''
  goto LABEL_START_INSTALLATION
endif

LABEL_START_INSTALLATION:

if ($install_gdr == 'yes' || $install_g7 == 'yes' || $install_g9 == 'yes') then
  goto LABEL_START_DRIVER
endif
LABEL_DRIVER_DONE:

if ($install_g9 == 'yes') then
  goto LABEL_START_G9
endif
LABEL_G9_DONE:

if ($install_gdr == 'yes') then
  goto LABEL_START_GDR
endif
LABEL_GDR_DONE:

if ($install_g7 == 'yes') then
  goto LABEL_START_G7
endif
LABEL_G7_DONE:

if ($install_g6a == 'yes') then
  goto LABEL_START_G6A
endif
LABEL_G6A_DONE:

if ($install_g6bx == 'yes') then
  goto LABEL_START_G6BX
endif
LABEL_G6BX_DONE:

if ($install_pg5 == 'yes') then
  goto LABEL_START_PG5
endif
LABEL_PG5_DONE:

if ($install_cuda == 'yes') then
  goto LABEL_START_CUDA
endif
LABEL_CUDA_DONE:

goto LABEL_START_COMMONUTIL
LABEL_COMMONUTIL_DONE:

echo ''
echo ------------------------------------------------------------
echo All installing processes have been completed successfully.
echo ------------------------------------------------------------
echo ''

exit 0

#
# subroutine for Phantom-GRAPE-5 configuration.
#

LABEL_PG5_CONF:
LABEL_RETRY_PG5_CONF:
echo ''
echo 'Setup Phantom-GRAPE-5 configuration'
echo ''
echo '  Select CPU architecture:'
echo '    1) No specific architecture.'
echo '    2) Intel Core2'
echo '    3) AMD K10'
echo '  "1-3" to select CPU architecture.'
echo ''
echo -n '  Input your choice : '
set answer = $<
switch ($answer)
  case "1"
    set march = 'generic x86'
    set march_extention = 'builtin'
    breaksw
  case "2"
    set march = 'Intel Core2'
    set march_extention = 'core2'
    breaksw
  case "3"
    set march = 'AMD K10'
    set march_extention = 'k10'
    breaksw
  case *
    echo ''
    echo '  Answer with 1-3.'
    echo ''
    goto LABEL_RETRY_PG5_CONF
    breaksw
endsw

LABEL_PG5_CONF2:
LABEL_RETRY_PG5_CONF2:
echo ''
echo -n '  How many CPU cores? : '
set answer = $<
if ( $answer > 0 ) then
  setenv maxdev $answer
  goto LABEL_PG5_CONF3
else
  echo ''
  echo "  Answer with a positive number."
  echo ''
  goto LABEL_PG5_CONF2
endif

LABEL_PG5_CONF3:
LABEL_RETRY_PG5_CONF3:
echo ''
echo -n '  Enable OpenMP? [y/n] : '
set answer = $<
if ( $answer == "n") then
  set openmp = 'disabled'
  setenv ENABLE_OPENMP 'no'
  goto LABEL_PG5_CONF_DONE
else if ( $answer == "y") then
  set openmp = 'enabled'
  setenv ENABLE_OPENMP 'yes'
  goto LABEL_PG5_CONF_DONE
else
  echo ''
  echo "  Answer with 'y' or 'n'."
  echo ''
  goto LABEL_PG5_CONF3
endif

#
# subroutine for CUDA G5/G6 configuration.
#

LABEL_CUDA_CONF:
LABEL_RETRY_CUDA_CONF:
echo ''
echo 'Setup CUDA G5/G6 configuration'
echo ''
if ($?CUDAPATH) then
  set cudapath = $CUDAPATH
else
  echo '  Where is the CUDA Toolkit installed?'
  echo -n '  Input your choice [/usr/local/cuda] : '
  set answer = $<
  if ( $answer != '' ) then
    set cudapath = $answer
  else
   set cudapath = '/usr/local/cuda'
  endif
endif

LABEL_CUDA2_CONF:
LABEL_RETRY_CUDA2_CONF:
if ($?CUDASDKPATH) then
  set cudasdkpath = $CUDASDKPATH
else
  echo ''
  echo '  Where is the CUDA SDK installed?'
  echo -n '  Input your choice [/usr/local/cuda/NVIDIA_GPU_Computing_SDK] : '
  set answer = $<
  if ( $answer != '' ) then
    set cudasdkpath = $answer
  else
    set cudasdkpath = '/usr/local/cuda/NVIDIA_GPU_Computing_SDK'
  endif
endif

set nvccversion = `./script/nvccver`

goto LABEL_CUDA_CONF_DONE

LABEL_START_COMMONUTIL:
echo ''
echo '--------------------------------------------------------------'
echo 'Installing utilities commonly used for all types of GRAPEs...'
echo '--------------------------------------------------------------'
echo ''

#
# mkdist : create initial distribution of particles in NEMO stoa format.
#
cd sample/direct
make mkdist
set s = ${status}
cd ../..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

#
# snapshots used in functionality tests.
#
cd init
set mkdist = ../script/mkdist 

set infile = (pl32 pl64 pl128 pl256 pl512 pl1k pl2k pl4k pl8k pl16k pl32k pl48k pl64k pl80k pl96k pl112k pl128k pl256k)
set nbody  = (32   64   128   256   512   1024 2048 4096 8192 16384 32768 49152 65536 81920 98304 114688 131072 262144)
set i = 1
while (${i} <= ${#infile})
  if (! -f ${infile[${i}]}) then
    ${mkdist} ${nbody[${i}]} 1 ${infile[${i}]}
  endif
  @ i++
end

cd ..

echo ''
echo done
echo ''

goto LABEL_COMMONUTIL_DONE


#
# HIB & DRIVER installation for GRAPE-DR & GRAPE-7
#
LABEL_START_DRIVER:

echo ''
echo '---------------------------------------------------------'
echo 'Installing device driver for GRAPE-DR & GRAPE-7...'
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 ''

goto LABEL_DRIVER_DONE

#
# GRAPE-9 installation
#
LABEL_START_G9:

echo ''
echo --------------------------
echo Installing G96 library...
echo --------------------------
echo ''
cd g9
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 " (individual timestep algorithm in C)"
echo -------------------------------------------------------
echo ''

cd sample/s8
setenv GARCH g9
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s8_${GARCH}
make -f Makefile.generic s8_${GARCH}nnb
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 C)"
echo --------------------------------------
echo ''
cd sample/s9
setenv GARCH g9
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s9_${GARCH}
make -f Makefile.generic s9_${GARCH}6
make -f Makefile.generic s9_${GARCH}6nb
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 "(Pairwise-force accuracy checker.)"
echo  ----------------------------------
echo ''
cd sample/pairwise
setenv GARCH g9
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
make -f Makefile.generic pairwise_g95cutoff
make -f Makefile.generic pairwise_g96
make -f Makefile.generic pairwise_g96cutoff
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 processes have been completed for GRAPE-9.'
echo 'Now you can check the functions of GRAPE-9 card(s)'
echo 'following the procedure below:'
echo ''
echo '(1) [Root Permission Required]'
echo '    Change directory to ./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 ''
echo '    Type'
echo '      ./script/setmtrr'
echo '    to set MTRR (memory type range register) of the host computer,'
echo '    causing significant improvement of data transfer speed.'
echo ''
echo '    *** Note : This procedure can be omitted               ***'
echo '    *** if the Linux kernel version is 2.6.26 or higher,   ***'
echo '    *** and PAT (page attribute table) support is enabled. ***'
echo '    *** Check the Linux header files                       ***'
echo '    ***   e.g. /usr/src/linux/include/linux/autoconf.h     ***'
echo '    *** to see if CONFIG_X86_PAT is defined or not.        ***'
echo ''
echo '(4) To run test programs, type'
echo '      ./script/check.'
echo '    Follow its instruction to see if your card is working correctly or not.'
echo ''
echo ''
echo "NOTE THAT STEPS (1)-(3) ARE NECESSARY EVERYTIME YOU RESTART THE HOST COMPUTER."
echo  -------------------------------------------------------------------------------
echo ''

goto LABEL_G9_DONE


#
# GRAPE-DR installation
#
LABEL_START_GDR:

echo ''
echo -----------------------------------
echo Installing GDR5 and GDR6 library...
echo -----------------------------------
echo ''
cd gdr
make clean >& /dev/null
make libgdr5.a
make libgdr6.a
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 test program...
echo -----------------------------
echo ''
cd gdr/testv
make clean >& /dev/null
make all
cd basic-grape
make clean >& /dev/null
make testvec
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 sample/s9
setenv GARCH gdr
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s9_${GARCH}
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 " (individual timestep algorithm in C)"
echo ----------------------------------------
echo ''
cd sample/s8
setenv GARCH gdr
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s8_${GARCH}
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 " (individual timestep algorithm in Fortran)"
echo ----------------------------------------
echo ''
cd sample/s8f
setenv GARCH gdr
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s8f_${GARCH}
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 sample/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 -------------------------------------------------------
echo "Installing a sample code..."
echo " (direct-summation algorithm in C)"
echo -------------------------------------------------------
echo ''
cd sample/direct
setenv GARCH gdr
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic compatible
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 Fortran)"
echo -------------------------------------------------------
echo ''
cd sample/directf
setenv GARCH gdr
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 sample/vtc
setenv GARCH gdr
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 "(Pairwise-force accuracy checker.)"
echo  ----------------------------------
echo ''
cd sample/pairwise
setenv GARCH gdr
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 processes have been completed for GRAPE-DR.'
echo 'Now you can check the functions of GRAPE-DR card(s)'
echo 'following the procedure below:'
echo ''
echo '(1) [Root Permission Required]'
echo '    Change directory to ./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 ''
echo '    Type'
echo '      ./script/setmtrr'
echo '    to set MTRR (memory type range register) of the host computer,'
echo '    causing significant improvement of data transfer speed.'
echo ''
echo '    *** Note : This procedure can be omitted               ***'
echo '    *** if the Linux kernel version is 2.6.26 or higher,   ***'
echo '    *** and PAT (page attribute table) support is enabled. ***'
echo '    *** Check the Linux header files                       ***'
echo '    ***   e.g. /usr/src/linux/include/linux/autoconf.h     ***'
echo '    *** to see if  CONFIG_X86_PAT  is defined or not.      ***'
echo ''
echo '(3) To set up the board, type'
echo '      ./script/config'
echo ''
echo '(4) To run test programs, type'
echo '      ./script/check'
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 ''

goto LABEL_GDR_DONE


#
# GRAPE-7 installation
#
LABEL_START_G7:

echo ''
echo --------------------------
echo Installing G75 library...
echo --------------------------
echo ''
cd g7
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 sample/direct
setenv GARCH g7
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic compatible
make -f Makefile.generic directtest_$GARCH
make -f Makefile.generic directnb_$GARCH
make -f Makefile.generic directtesta_$GARCH
make -f Makefile.generic directnba_$GARCH
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 Fortran)"
echo -------------------------------------------------------
echo ''
cd sample/directf
setenv GARCH g7
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 sample/vtc
setenv GARCH g7
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 C)"
echo --------------------------------------
echo ''
cd sample/s9
setenv GARCH g7
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s9_${GARCH}
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 "(Pairwise-force accuracy checker.)"
echo  ----------------------------------
echo ''
cd sample/pairwise
setenv GARCH g7
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 processes have been completed for GRAPE-7.'
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 ./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 ''
echo '    Type'
echo '      ./script/setmtrr'
echo '    to set MTRR (memory type range register) of the host computer,'
echo '    causing significant improvement of data transfer speed.'
echo ''
echo '    *** Note : This procedure can be omitted               ***'
echo '    *** if the Linux kernel version is 2.6.26 or higher,   ***'
echo '    *** and PAT (page attribute table) support is enabled. ***'
echo '    *** Check the Linux header files                       ***'
echo '    ***   e.g. /usr/src/linux/include/linux/autoconf.h     ***'
echo '    *** to see if CONFIG_X86_PAT is defined or not.        ***'
echo ''
echo '(3) [Model300/600 Users Only]'
echo '    Type'
echo '      ./script/lsgrape'
echo '    to show all GRAPE add-in cards and their device ID.'
echo '    In order to reconfigure one of them, change directory to'
echo '    ./g7/config, and then type'
echo '      ./config [device ID]'
echo '    This will reconfigure pFPGAs on the device_id-th card.'
echo '    This step is necessary only for GRAPE-7 model300 and 600.'
echo ''
echo '(4) To run test programs, type'
echo '      ./script/check.'
echo '    Follow its instruction to see if your card is working correctly or not.'
echo ''
echo ''
echo "NOTE THAT STEPS (1)-(3) ARE NECESSARY EVERYTIME YOU RESTART THE HOST COMPUTER."
echo  -------------------------------------------------------------------------------
echo ''

goto LABEL_G7_DONE

#
# GRAPE-6A installation
#
LABEL_START_G6A:

echo ''
echo ------------------------------------
echo Installing GRAPE-6A device driver...
echo ------------------------------------
echo ''

cd g6a
/bin/rm pcimem >& /dev/null
ln -s pcimem.kfcr pcimem
cd pcimem

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 G6A and G65 library...
echo -----------------------------------
echo ''
cd g6a/lib
make clean >& /dev/null
make libg6a.a
set s0 = ${status}
make libg65.a
set s1 = ${status}
make g6aconfig
set s2 = ${status}
cd ../..
if (${s0} != 0 || ${s1} != 0 || ${s2} != 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 g6a/s9
cd sample/s9
setenv GARCH g6a
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s9_${GARCH}
set s0 = ${status}
cd ../..
if (${s0} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

echo ''
echo -------------------------------------------------------
echo "Installing a sample code..."
echo " (individual timestep algorithm in C)"
echo -------------------------------------------------------
echo ''
# cd g6a/s8
cd sample/s8
setenv GARCH g6a
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s8_${GARCH}
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 " (individual timestep algorithm in Fortran)"
echo -------------------------------------------------------
echo ''
cd g6a/s8f
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 ''

cat << NOTE_END
-------------------------------------------------------------------------------
All installation processes have been completed for GRAPE-6A.
Note that the device driver resides in grapepkg/g6a/pcimem/
subdirectory, and is not integrated into grapepkg/driver/.
-------------------------------------------------------------------------------

NOTE_END

goto LABEL_G6A_DONE

#
# GRAPE-6BX installation
#
LABEL_START_G6BX:

echo ''
echo ------------------------------------
echo Installing GRAPE-6BX device driver...
echo ------------------------------------
echo ''

cd g6bx/pcixmem
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 g6bx/xhibutil
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 G6BX library...
echo --------------------------
echo ''
cd g6bx/lib
make clean >& /dev/null
make libg6bx.a
set s0 = ${status}
make libg65.a
set s1 = ${status}
make chgchip
set s2 = ${status}
cd ../..
if (${s0} != 0 || ${s1} != 0 || ${s2} != 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 g6bx/s9
cd sample/s9
setenv GARCH g6bx
setenv HIB g6bxhib
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s9_${GARCH}
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 " (individual timestep algorithm in C)"
echo -------------------------------------------------------
echo ''

# cd g6bx/s8
cd sample/s8
setenv GARCH g6bx
setenv HIB g6bxhib
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s8_${GARCH}
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 g6bx/tree
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 C)"
echo -------------------------------------------------------
echo ''
cd sample/direct
setenv GARCH g6bx
setenv HIB g6bxhib
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic compatible
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 "(Pairwise-force accuracy checker.)"
echo  ----------------------------------
echo ''
cd sample/pairwise
setenv GARCH g6bx
setenv HIB g6bxhib
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
set s = ${status}
cd ../..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

echo ''
echo done
echo ''

cat << NOTE_END
-------------------------------------------------------------------------------
All installation processes have been completed for GRAPE-6BX.
Note that the device driver and HIB reside in grapepkg/g6bx/{pcixmem,xhibutil}/
subdirectories, and are not integrated into grapepkg/{driver,hibutil}/.
-------------------------------------------------------------------------------

NOTE_END

goto LABEL_G6BX_DONE


#
# Phantom-GRAPE-5 installation
#
LABEL_START_PG5:

echo ''
echo --------------------------------------
echo Installing Phantom-GRAPE-5 library...
echo --------------------------------------
echo ''

cd pg5
make clean >& /dev/null
make -f Makefile.${march_extention}
set s = ${status}
cd ..
if (${s} != 0) then
  echo ''
  echo ---------------------------------------------
  echo "Installation failed for architecture '${march}'."
  echo 'Try build for another one.'
  echo ---------------------------------------------
  exit 1
endif

echo ''
echo --------------------------------------
echo "Installing a sample code..."
echo " (direct-summation algorithm in C)"
echo --------------------------------------
echo ''
cd sample/s9
setenv GARCH pg5
setenv HIB no
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s9_${GARCH}
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 C)"
echo -------------------------------------------------------
echo ''
cd sample/direct
setenv GARCH pg5
setenv HIB no
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic compatible
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 sample/vtc
setenv GARCH pg5
setenv HIB no
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 "(Pairwise-force accuracy checker.)"
echo  ----------------------------------
echo ''
cd sample/pairwise
setenv GARCH pg5
setenv HIB no
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
set s = ${status}
cd ../..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif
goto LABEL_PG5_DONE
#
# CUDA G5/G6 installation
#
LABEL_START_CUDA:

echo ''
echo --------------------------------------
echo Installing CUDA G5/G6 library...
echo --------------------------------------
echo ''

cd cuda/test
make clean >& /dev/null
make
set st = ${status}
cd ..
make clean >& /dev/null
setenv CUDAPATH ${cudapath}
setenv CUDASDKPATH ${cudasdkpath}
setenv NVCCVERSION ${nvccversion}
make -f Makefile
set s = ${status}
cd ..
if (${s} != 0 || ${st} != 0) then
  echo ''
  echo ---------------------------------------------
  echo "Installation failed."
  echo ---------------------------------------------
  exit 1
endif

echo ''
echo --------------------------------------
echo "Installing a sample code..."
echo " (direct-summation algorithm in C)"
echo --------------------------------------
echo ''
cd sample/s9
setenv GARCH cuda
setenv HIB no
setenv MORELDFLAGS "-L${cudapath}/lib64 -lcudart -lstdc++ "
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s9_${GARCH}
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 " (individual timestep algorithm in C)"
echo ----------------------------------------
echo ''
cd sample/s8
setenv GARCH cuda
setenv HIB no
setenv MORELDFLAGS "-L${cudapath}/lib64 -lcudart -lstdc++ "
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s8_${GARCH}
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 " (individual timestep algorithm in Fortran)"
echo ----------------------------------------
echo ''
cd sample/s8f
setenv GARCH cuda
setenv HIB no
setenv MORELDFLAGS "-L${cudapath}/lib64 -lcudart -lstdc++ "
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic s8f_${GARCH}
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 C)"
echo -------------------------------------------------------
echo ''
cd sample/direct
setenv GARCH cuda
setenv HIB no
setenv MORELDFLAGS "-L${cudapath}/lib64 -lcudart -lstdc++ "
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic compatible
make -f Makefile.generic multiwalktest_cuda
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 Fortran)"
echo -------------------------------------------------------
echo ''
cd sample/directf
setenv GARCH cuda
setenv HIB no
setenv MORELDFLAGS "-L${cudapath}/lib64 -lcudart -lstdc++ "
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 sample/vtc
setenv GARCH cuda
setenv HIB no
setenv MORELDFLAGS "-L${cudapath}/lib64 -lcudart -lstdc++ "
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
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 "(Pairwise-force accuracy checker.)"
echo  ----------------------------------
echo ''
cd sample/pairwise
setenv GARCH cuda
setenv HIB no
setenv MORELDFLAGS "-L${cudapath}/lib64 -lcudart -lstdc++ "
make -f Makefile.generic clean >& /dev/null
make -f Makefile.generic
set s = ${status}
cd ../..
if (${s} != 0) then
  echo ''
  echo -----------------------
  echo 'Installation failed.'
  echo -----------------------
  exit 1
endif

echo ''
echo done
echo ''

goto LABEL_CUDA_DONE
