# Tweak this script as needed and run with tcsh, for example,
#
# tcsh install_npstat_with_prerequisites.csh >& npstat_install.log &
#
# This script does not attempt to install a python distribution which
# is a prerequisite for building NPStat python API. You can get a good
# one from https://www.anaconda.com/download/
#
# Toplevel directory into which all software packages will be installed.
# You can change it to something else if you don't like the location
# defined here.
set local_dir = ${HOME}/local

# Number of jobs "make" will run simultaneously
set n_make_jobs = 5

# Set up compilers and build tools needed. Make sure that you have
# a compiler which supports C++11 (e.g., g++ version 7 or later). If you
# have to install LAPACK/BLAS yourself, you will need gfortran and cmake.
# If "cmake" is not installed in a standard location on your machine,
# you might need to set the CMAKE_LIBRARY_PATH variable.
#
# Here, for example, how this setup is performed on the TTU HPCC machines:
# module load gnu7/7.3.0
# module load cmake/3.10.3
# setenv CMAKE_LIBRARY_PATH /opt/ohpc/pub/compiler/gcc/7.3.0/lib64

# Define which packages to install
set install_lapack_blas = 1
set lapack_version = 3.8.0
set blas_version = 3.8.0

set install_fftw = 1
set fftw_version = 3.3.8

set install_bzip2 = 1
set bzip2_version = 1.0.6

set install_kstest = 1
set kstest_version = 2.0.0

set install_geners = 1
set geners_version = 1.12.0

set install_npstat = 1
set install_npstat_python_api = 1
set npstat_version = 5.1.0

# Typical compilation flags for C++ packages
set normal_cxx_flags = "-std=c++11 -O3 -Wall -W -Werror"

# Create some necessary directories
mkdir -p ${local_dir}/src
mkdir -p ${local_dir}/lib
mkdir -p ${local_dir}/include

# Set PATH
mkdir -p ${local_dir}/bin
setenv PATH $local_dir/bin:$PATH
rehash

# Set LD_LIBRARY_PATH
if ( ${?LD_LIBRARY_PATH} ) then
    setenv LD_LIBRARY_PATH $local_dir/lib:${LD_LIBRARY_PATH}
endif
if ( ! ${?LD_LIBRARY_PATH} ) setenv LD_LIBRARY_PATH $local_dir/lib:/lib64:/usr/lib64

# Set PKG_CONFIG_PATH
setenv PKG_CONFIG_PATH $local_dir/lib/pkgconfig
mkdir -p $PKG_CONFIG_PATH

# Install LAPACK and BLAS
if ($install_lapack_blas) then
    #
    # Install BLAS
    cd ${local_dir}/src
    rm -f blas-${blas_version}.tgz
    wget http://www.netlib.org/blas/blas-${blas_version}.tgz
    rm -fr BLAS-${blas_version}
    tar -xzf blas-${blas_version}.tgz
    rm -f blas-${blas_version}.tgz
    cd BLAS-${blas_version}
    gfortran -fPIC -shared -O3 *.f -o libblas.so
    mv -f libblas.so ${local_dir}/lib/libblas.so.${blas_version}
    cd ${local_dir}/lib
    set blas_major_version = `echo $blas_version | cut -f1 -d"."`
    rm -f libblas.so libblas.so.$blas_major_version
    ln -s libblas.so.${blas_version} libblas.so
    ln -s libblas.so.${blas_version} libblas.so.$blas_major_version
    #
    # Install LAPACK
    cd ${local_dir}/src
    rm -f lapack-${lapack_version}.tar.gz
    wget http://www.netlib.org/lapack/lapack-${lapack_version}.tar.gz
    rm -fr lapack-${lapack_version}
    tar -xzf lapack-${lapack_version}.tar.gz
    rm -f lapack-${lapack_version}.tar.gz
    rm -fr lapack_build
    mkdir lapack_build
    cd lapack_build
    setenv FC `which gfortran`
    cmake -DCMAKE_INSTALL_PREFIX=$local_dir -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_SHARED_LIBS=ON -DBLAS_LIBRARIES=${local_dir}/lib/libblas.so ../lapack-$lapack_version
    unsetenv FC
    unsetenv CMAKE_LIBRARY_PATH
    make -j $n_make_jobs
    mv -f lib/liblapack.so.$lapack_version ${local_dir}/lib
    cd ${local_dir}/lib
    set lapack_major_version = `echo $lapack_version | cut -f1 -d"."`
    rm -f liblapack.so liblapack.so.$lapack_major_version
    ln -s liblapack.so.$lapack_version liblapack.so
    ln -s liblapack.so.$lapack_version liblapack.so.$lapack_major_version
    cd ${local_dir}/src
    rm -fr lapack_build
    echo Installed LAPACK and BLAS
endif

# Install fftw
if ($install_fftw) then
    cd ${local_dir}/src
    rm -f fftw-${fftw_version}.tar.gz
    wget --no-check-certificate http://www.fftw.org/fftw-${fftw_version}.tar.gz
    rm -fr fftw-${fftw_version}
    tar -xzf fftw-${fftw_version}.tar.gz
    rm -f fftw-${fftw_version}.tar.gz
    cd fftw-${fftw_version}
    ./configure --with-pic --prefix=$local_dir --enable-shared --enable-sse2 --disable-fortran --disable-openmp --disable-mpi
    make -j $n_make_jobs
    make install
    echo Installed fftw
endif

# Install bzip2
if ($install_bzip2) then
    cd $local_dir/src
    rm -f bzip2-${bzip2_version}.tar.gz
    wget http://www.phys.ttu.edu/~igvol/bzip2-${bzip2_version}.tar.gz
    rm -fr bzip2-${bzip2_version}
    tar -xzf bzip2-${bzip2_version}.tar.gz
    rm -f bzip2-${bzip2_version}.tar.gz
    cd bzip2-${bzip2_version}
    make -f Makefile-libbz2_so
    cp bzlib.h $local_dir/include
    cp libbz2.so.${bzip2_version} $local_dir/lib
    cd $local_dir/lib
    ln -s libbz2.so.${bzip2_version} libbz2.so
    echo Installed bzip2
endif

# Install Geners
if ($install_geners) then
    cd ${local_dir}/src
    rm -f geners-${geners_version}.tar.gz
    wget --no-check-certificate http://www.hepforge.org/archive/geners/geners-${geners_version}.tar.gz
    rm -fr geners-${geners_version}
    tar -xzf geners-${geners_version}.tar.gz
    rm -f geners-${geners_version}.tar.gz
    cd geners-${geners_version}
    setenv CXXFLAGS "$normal_cxx_flags"
    ./configure --with-pic --with-bzip2-include-path=$local_dir/include --with-bzip2-lib-path=$local_dir/lib --prefix=$local_dir
    unsetenv CXXFLAGS
    make -j $n_make_jobs
    make install
    make check
    echo Installed Geners
endif

# Install KSTest
if ($install_kstest) then
    cd ${local_dir}/src
    rm -f kstest-${kstest_version}.tar.gz
    wget --no-check-certificate http://www.hepforge.org/archive/kstest/kstest-${kstest_version}.tar.gz
    rm -fr kstest-${kstest_version}
    tar -xzf kstest-${kstest_version}.tar.gz
    rm -f kstest-${kstest_version}.tar.gz
    cd kstest-${kstest_version}
    setenv CXXFLAGS "$normal_cxx_flags"
    ./configure --with-pic --prefix=$local_dir
    unsetenv CXXFLAGS
    make
    make install
    make check
    echo Installed KSTest
endif

# Now that we have all the prerequisites, we can install NPStat
if ($install_npstat) then
    cd ${local_dir}/src
    rm -f npstat-${npstat_version}.tar.gz
    wget --no-check-certificate http://www.hepforge.org/archive/npstat/npstat-${npstat_version}.tar.gz
    rm -fr npstat-${npstat_version}
    tar -xzf npstat-${npstat_version}.tar.gz
    rm -f npstat-${npstat_version}.tar.gz
    cd npstat-${npstat_version}
    setenv CXXFLAGS "$normal_cxx_flags"
    ./configure --disable-static --with-pic --prefix=$local_dir
    unsetenv CXXFLAGS
    make -j $n_make_jobs
    make install
    make check
    echo Installed NPStat
    if ($install_npstat_python_api) then
        make python
        echo Compiled NPStat Python API. Do not forget to include `pwd`/npstat/swig in the PYTHONPATH.
    endif
endif
