#!/bin/bash

# NOTES
#  @perlmutter: for the first-time setup, run this on login nodes for
#  ./autogen.sh to complete without warning
#

scriptdir=/global/homes/r/roguzsel/gcp/script
codedir=/global/homes/r/roguzsel/gcp/code-v01

# hadron compilation needs it
module load cudatoolkit/12.4
module load gcc-native/13.2


# adat/devel
cd ${codedir}
git clone --recursive git@github.com:JeffersonLab/adat.git
cd adat
git fetch
git checkout devel
mkdir adat
./autogen.sh
./configure --prefix=${codedir}/adat/adat CC=gcc CXX=g++ F77=gfortran-7 CFLAGS="-O3 -fopenmp" CXXFLAGS="-O3 -fopenmp -std=c++20" FFLAGS="-O3 -fopenmp" LIBS=
git submodule update --init --recursive
make -j23
make -j23 install

# tensor/master
cd ${codedir}
git clone git@github.com:JeffersonLab/tensor.git
cd tensor
mkdir tensor
./autogen.sh
./configure --prefix=${codedir}/tensor/tensor --enable-threadsafe-deathtest --with-openblas=${HOME}/local/openblas CC=gcc CXX=g++ F77=gfortran-7 CFLAGS="-O3 -fopenmp -I${HOME}/local/openblas" CXXFLAGS="-O3 -fopenmp -std=c++20 -I${HOME}/local/openblas"  FFLAGS="-O3 -fopenmp -I${HOME}/local/openblas" LIBS=
make -j23
make -j23 install

# hadron/devel
cd ${codedir}
git clone git@github.com:JeffersonLab/hadron.git
cd hadron
git fetch
git checkout devel
mkdir hadron
./autogen.sh
./configure --enable-cuda --with-cuda=${CUDA_HOME} --with-cudasm=80 --with-cublas=/opt/nvidia/hpc_sdk/Linux_x86_64/24.5/math_libs/12.4 --with-tensor=${codedir}/tensor/tensor --prefix=${codedir}/hadron/hadron CC=gcc CXX=g++ F77=gfortran-7 CXXFLAGS="-D_USE_CONTIGUOUS_HOST_MEM"
make -j23
make -j23 install

# colorvec/devel
cd ${codedir}
git clone git@github.com:JeffersonLab/colorvec.git
cd colorvec
git fetch
git checkout devel
mkdir colorvec
./autogen.sh
./configure --prefix=${codedir}/colorvec/colorvec --with-adat=${codedir}/adat/adat --with-hadron=${codedir}/hadron/hadron CC=gcc CXX=g++ F77=gfortran-7 CFLAGS="-L${HOME}/local/openblas" CXXFLAGS="-L${HOME}/local/openblas -std=c++20" FFLAGS="-L${HOME}/local/openblas"
make -j23
make -j23 install

# redstar/devel
cd ${codedir}
git clone git@github.com:JeffersonLab/redstar.git
cd redstar
git fetch
git checkout devel
mkdir redstar
./autogen.sh
# dir below causes error if not renamed, it is apparently not used anywhere
mv src/generate_all_3pt_spectrum src/generate_all_3pt_spectrum_tmp
./configure --prefix=${codedir}/redstar/redstar --with-colorvec=${codedir}/colorvec/colorvec CC=gcc CXX=g++ F77=gfortran-7 LIBS="-lgmp"
make -j23
make -j23 install

# redstar_kanoni/devel
cd ${codedir}
git clone git@github.com:JeffersonLab/redstar_kanoni.git
cd redstar_kanoni
git fetch
git checkout devel


cd ${scriptdir}


exit 0
