#!/bin/bash -eu
# Testing that find_package works.
# Note: this script must not be added to the test suite as it will change cmake options.
echo "***************************************************************************************"

# 1. Define temporary directory.

export TMP_DIR="/tmp/tstCMakeInstall"
rm -fr "${TMP_DIR}" # Make sure we restart from scratch.
mkdir -p "${TMP_DIR}"

# 2. Rerun cmake with prefix, install arpack-ng.
#    Note: this script must not be added to the test suite as it will change cmake options.

export TMP_PREFIX="${TMP_DIR}/local"
export LIBSUFFIX="${1%-*}"
echo "LIBSUFFIX: $LIBSUFFIX"
export ITF64SUFFIX="${1#*-}"
echo "ITF64SUFFIX: $ITF64SUFFIX"
cmake -DCMAKE_INSTALL_PREFIX="${TMP_PREFIX}" -DMPI=ON -DICB=ON -DEIGEN=ON -DLIBSUFFIX="${LIBSUFFIX}" -DITF64SUFFIX="${ITF64SUFFIX}" .
make all install
tree "${TMP_PREFIX}"

# 3. Setup environment for find_package to work (what you typically in module-environment files).

cd "${TMP_DIR}"
export PKG_CONFIG_PATH="$(find ${TMP_PREFIX} -name arpack${LIBSUFFIX}${ITF64SUFFIX}.pc)"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH%/*}" # Same as dirname.
echo "PKG_CONFIG_PATH: $PKG_CONFIG_PATH"
# find the prefix directory where CMake files are installed
# this way we test find_package(arpackng) finds arpack in standard locations
arpack_cmake_dir="$(find ${TMP_PREFIX} -name arpackng-config.cmake)"
arpack_cmake_dir="${arpack_cmake_dir%/*}" # Same as dirname
CMAKE_PREFIX_PATH="${arpack_cmake_dir%/*}" # cmake directory

# 4. Create new cmake project, in temporary directory, with files from arpack-ng.
#    The first targets are compiled with *.cmake files generated by cmake.
#    The second targets are compiled with *.pc files generated by cmake.

mkdir -p tstCMakeInstall
cd tstCMakeInstall

cp "@PROJECT_SOURCE_DIR@/EXAMPLES/BAND/dnband.f" .
cp "@PROJECT_SOURCE_DIR@/EXAMPLES/BAND/dnbdr1.f" .
cp "@PROJECT_SOURCE_DIR@/PARPACK/EXAMPLES/MPI/pdndrv1.f" .
cp "@PROJECT_SOURCE_DIR@/TESTS/icb_arpack_c.c" .
cp "@PROJECT_SOURCE_DIR@/TESTS/icb_arpack_cpp.cpp" .
cp "@PROJECT_SOURCE_DIR@/PARPACK/TESTS/MPI/icb_parpack_c.c" .
cp "@PROJECT_SOURCE_DIR@/PARPACK/TESTS/MPI/icb_parpack_cpp.cpp" .
cp "@PROJECT_SOURCE_DIR@/EXAMPLES/MATRIX_MARKET/arpackmm.cpp" .

echo "cmake_minimum_required(VERSION 3.5)"                               > CMakeLists.txt
echo ""                                                                 >> CMakeLists.txt
echo "project(tstCMakeInstall Fortran C CXX)"                           >> CMakeLists.txt
echo ""                                                                 >> CMakeLists.txt
echo "# Targets compiled with *.cmake files generated by cmake"         >> CMakeLists.txt
echo ""                                                                 >> CMakeLists.txt
echo "find_package(BLAS REQUIRED)"                                      >> CMakeLists.txt
echo "find_package(LAPACK REQUIRED)"                                    >> CMakeLists.txt
echo "find_package(MPI REQUIRED COMPONENTS Fortran C CXX)"              >> CMakeLists.txt
echo "find_package(arpackng REQUIRED)"                                  >> CMakeLists.txt
echo "find_package(Eigen3 3.3 REQUIRED)"                                >> CMakeLists.txt
echo ""                                                                 >> CMakeLists.txt
echo "add_executable(dnbdr1 dnband.f dnbdr1.f)"                         >> CMakeLists.txt
echo "target_include_directories(dnbdr1 INTERFACE BLAS::BLAS)"          >> CMakeLists.txt
echo "target_link_libraries(dnbdr1 BLAS::BLAS)"                         >> CMakeLists.txt
echo "target_include_directories(dnbdr1 INTERFACE LAPACK::LAPACK)"      >> CMakeLists.txt
echo "target_link_libraries(dnbdr1 LAPACK::LAPACK)"                     >> CMakeLists.txt
echo "target_include_directories(dnbdr1 INTERFACE ARPACK::ARPACK)"      >> CMakeLists.txt
echo "target_link_libraries(dnbdr1 ARPACK::ARPACK)"                     >> CMakeLists.txt
echo ""                                                                  >> CMakeLists.txt
echo "add_executable(icb_arpack_c icb_arpack_c.c)"                       >> CMakeLists.txt
echo "target_include_directories(icb_arpack_c INTERFACE BLAS::BLAS)"     >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_c BLAS::BLAS)"                    >> CMakeLists.txt
echo "target_include_directories(icb_arpack_c INTERFACE LAPACK::LAPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_c LAPACK::LAPACK)"                >> CMakeLists.txt
echo "target_include_directories(icb_arpack_c INTERFACE ARPACK::ARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_c ARPACK::ARPACK)"                >> CMakeLists.txt
echo ""                                                                    >> CMakeLists.txt
echo "add_executable(icb_arpack_cpp icb_arpack_cpp.cpp)"                   >> CMakeLists.txt
echo "target_include_directories(icb_arpack_cpp INTERFACE BLAS::BLAS)"     >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_cpp BLAS::BLAS)"                    >> CMakeLists.txt
echo "target_include_directories(icb_arpack_cpp INTERFACE LAPACK::LAPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_cpp LAPACK::LAPACK)"                >> CMakeLists.txt
echo "target_include_directories(icb_arpack_cpp INTERFACE ARPACK::ARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_cpp ARPACK::ARPACK)"                >> CMakeLists.txt
echo ""                                                              >> CMakeLists.txt
echo "add_executable(arpackmm arpackmm.cpp)"                         >> CMakeLists.txt
echo "target_include_directories(arpackmm INTERFACE BLAS::BLAS)"     >> CMakeLists.txt
echo "target_link_libraries(arpackmm BLAS::BLAS)"                    >> CMakeLists.txt
echo "target_include_directories(arpackmm INTERFACE LAPACK::LAPACK)" >> CMakeLists.txt
echo "target_link_libraries(arpackmm LAPACK::LAPACK)"                >> CMakeLists.txt
echo "target_include_directories(arpackmm INTERFACE ARPACK::ARPACK)" >> CMakeLists.txt
echo "target_link_libraries(arpackmm ARPACK::ARPACK)"                >> CMakeLists.txt
echo "target_include_directories(arpackmm INTERFACE Eigen3::Eigen)"  >> CMakeLists.txt
echo "target_link_libraries(arpackmm Eigen3::Eigen)"                 >> CMakeLists.txt
echo ""                                                                 >> CMakeLists.txt
echo "add_executable(pdndrv1 dnband.f pdndrv1.f)"                       >> CMakeLists.txt
echo "target_include_directories(pdndrv1 INTERFACE BLAS::BLAS)"         >> CMakeLists.txt
echo "target_link_libraries(pdndrv1 BLAS::BLAS)"                        >> CMakeLists.txt
echo "target_include_directories(pdndrv1 INTERFACE LAPACK::LAPACK)"     >> CMakeLists.txt
echo "target_link_libraries(pdndrv1 LAPACK::LAPACK)"                    >> CMakeLists.txt
echo "target_include_directories(pdndrv1 INTERFACE MPI::MPI_Fortran)"   >> CMakeLists.txt
echo "target_link_libraries(pdndrv1 MPI::MPI_Fortran)"                  >> CMakeLists.txt
echo "target_include_directories(pdndrv1 INTERFACE ARPACK::ARPACK)"     >> CMakeLists.txt
echo "target_link_libraries(pdndrv1 ARPACK::ARPACK)"                    >> CMakeLists.txt
echo "target_include_directories(pdndrv1 INTERFACE PARPACK::PARPACK)"   >> CMakeLists.txt
echo "target_link_libraries(pdndrv1 PARPACK::PARPACK)"                  >> CMakeLists.txt
echo ""                                                                     >> CMakeLists.txt
echo "add_executable(icb_parpack_c icb_parpack_c.c)"                        >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c INTERFACE BLAS::BLAS)"       >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c BLAS::BLAS)"                      >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c INTERFACE LAPACK::LAPACK)"   >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c LAPACK::LAPACK)"                  >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c INTERFACE MPI::MPI_C)"       >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c MPI::MPI_C)"                      >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c INTERFACE ARPACK::ARPACK)"   >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c ARPACK::ARPACK)"                  >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c INTERFACE PARPACK::PARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c PARPACK::PARPACK)"                >> CMakeLists.txt
echo ""                                                                       >> CMakeLists.txt
echo "add_executable(icb_parpack_cpp icb_parpack_cpp.cpp)"                    >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp INTERFACE BLAS::BLAS)"       >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp BLAS::BLAS)"                      >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp INTERFACE LAPACK::LAPACK)"   >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp LAPACK::LAPACK)"                  >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp INTERFACE MPI::MPI_CXX)"     >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp MPI::MPI_CXX)"                    >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp INTERFACE ARPACK::ARPACK)"   >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp ARPACK::ARPACK)"                  >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp INTERFACE PARPACK::PARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp PARPACK::PARPACK)"                >> CMakeLists.txt
echo ""                                                                 >> CMakeLists.txt
echo "# Targets compiled with with *.pc files generated by cmake"       >> CMakeLists.txt
echo ""                                                                 >> CMakeLists.txt
echo "find_package(PkgConfig REQUIRED)"                                 >> CMakeLists.txt
echo "pkg_check_modules(ARPACK IMPORTED_TARGET REQUIRED arpack${LIBSUFFIX}${ITF64SUFFIX})"   >> CMakeLists.txt
echo "pkg_check_modules(PARPACK IMPORTED_TARGET REQUIRED parpack${LIBSUFFIX}${ITF64SUFFIX})" >> CMakeLists.txt
echo ""                                                                     >> CMakeLists.txt
echo "add_executable(dnbdr1_pkg dnband.f dnbdr1.f)"                         >> CMakeLists.txt
echo "target_include_directories(dnbdr1_pkg INTERFACE BLAS::BLAS)"          >> CMakeLists.txt
echo "target_link_libraries(dnbdr1_pkg BLAS::BLAS)"                         >> CMakeLists.txt
echo "target_include_directories(dnbdr1_pkg INTERFACE LAPACK::LAPACK)"      >> CMakeLists.txt
echo "target_link_libraries(dnbdr1_pkg LAPACK::LAPACK)"                     >> CMakeLists.txt
echo "target_include_directories(dnbdr1_pkg INTERFACE PkgConfig::ARPACK)"   >> CMakeLists.txt
echo "target_link_libraries(dnbdr1_pkg PkgConfig::ARPACK)"                  >> CMakeLists.txt
echo ""                                                                         >> CMakeLists.txt
echo "add_executable(icb_arpack_c_pkg icb_arpack_c.c)"                          >> CMakeLists.txt
echo "target_include_directories(icb_arpack_c_pkg INTERFACE BLAS::BLAS)"        >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_c_pkg BLAS::BLAS)"                       >> CMakeLists.txt
echo "target_include_directories(icb_arpack_c_pkg INTERFACE LAPACK::LAPACK)"    >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_c_pkg LAPACK::LAPACK)"                   >> CMakeLists.txt
echo "target_include_directories(icb_arpack_c_pkg INTERFACE PkgConfig::ARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_c_pkg PkgConfig::ARPACK)"                >> CMakeLists.txt
echo ""                                                                           >> CMakeLists.txt
echo "add_executable(icb_arpack_cpp_pkg icb_arpack_cpp.cpp)"                      >> CMakeLists.txt
echo "target_include_directories(icb_arpack_cpp_pkg INTERFACE BLAS::BLAS)"        >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_cpp_pkg BLAS::BLAS)"                       >> CMakeLists.txt
echo "target_include_directories(icb_arpack_cpp_pkg INTERFACE LAPACK::LAPACK)"    >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_cpp_pkg LAPACK::LAPACK)"                   >> CMakeLists.txt
echo "target_include_directories(icb_arpack_cpp_pkg INTERFACE PkgConfig::ARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_arpack_cpp_pkg PkgConfig::ARPACK)"                >> CMakeLists.txt
echo ""                                                                     >> CMakeLists.txt
echo "add_executable(pdndrv1_pkg dnband.f pdndrv1.f)"                       >> CMakeLists.txt
echo "target_include_directories(pdndrv1_pkg INTERFACE BLAS::BLAS)"         >> CMakeLists.txt
echo "target_link_libraries(pdndrv1_pkg BLAS::BLAS)"                        >> CMakeLists.txt
echo "target_include_directories(pdndrv1_pkg INTERFACE LAPACK::LAPACK)"     >> CMakeLists.txt
echo "target_link_libraries(pdndrv1_pkg LAPACK::LAPACK)"                    >> CMakeLists.txt
echo "target_include_directories(pdndrv1_pkg INTERFACE MPI::MPI_Fortran)"   >> CMakeLists.txt
echo "target_link_libraries(pdndrv1_pkg MPI::MPI_Fortran)"                  >> CMakeLists.txt
echo "target_include_directories(pdndrv1_pkg INTERFACE PkgConfig::ARPACK)"  >> CMakeLists.txt
echo "target_link_libraries(pdndrv1_pkg PkgConfig::ARPACK)"                 >> CMakeLists.txt
echo "target_include_directories(pdndrv1_pkg INTERFACE PkgConfig::PARPACK)" >> CMakeLists.txt
echo "target_link_libraries(pdndrv1_pkg PkgConfig::PARPACK)"                >> CMakeLists.txt
echo ""                                                                           >> CMakeLists.txt
echo "add_executable(icb_parpack_c_pkg icb_parpack_c.c)"                          >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c_pkg INTERFACE BLAS::BLAS)"         >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c_pkg BLAS::BLAS)"                        >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c_pkg INTERFACE LAPACK::LAPACK)"     >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c_pkg LAPACK::LAPACK)"                    >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c_pkg INTERFACE MPI::MPI_C)"         >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c_pkg MPI::MPI_C)"                        >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c_pkg INTERFACE PkgConfig::ARPACK)"  >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c_pkg PkgConfig::ARPACK)"                 >> CMakeLists.txt
echo "target_include_directories(icb_parpack_c_pkg INTERFACE PkgConfig::PARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_c_pkg PkgConfig::PARPACK)"                >> CMakeLists.txt
echo ""                                                                             >> CMakeLists.txt
echo "add_executable(icb_parpack_cpp_pkg icb_parpack_cpp.cpp)"                      >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp_pkg INTERFACE BLAS::BLAS)"         >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp_pkg BLAS::BLAS)"                        >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp_pkg INTERFACE LAPACK::LAPACK)"     >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp_pkg LAPACK::LAPACK)"                    >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp_pkg INTERFACE MPI::MPI_CXX)"       >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp_pkg MPI::MPI_CXX)"                      >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp_pkg INTERFACE PkgConfig::ARPACK)"  >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp_pkg PkgConfig::ARPACK)"                 >> CMakeLists.txt
echo "target_include_directories(icb_parpack_cpp_pkg INTERFACE PkgConfig::PARPACK)" >> CMakeLists.txt
echo "target_link_libraries(icb_parpack_cpp_pkg PkgConfig::PARPACK)"                >> CMakeLists.txt

# 5. Build and test this new project with cmake: for this to be possible, find_package must work.

mkdir -p build
cd build

cmake .. -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" || exit 1
make all VERBOSE=1 || exit 1

export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/tmp/tstCMakeInstall/local/lib"

# The first targets are compiled with *.cmake files generated by cmake.
./dnbdr1 || exit 1
./icb_arpack_c || exit 1
./icb_arpack_cpp || exit 1
./arpackmm -h || exit 1
mpirun -n 2 ./pdndrv1 || exit 1
mpirun -n 2 ./icb_parpack_c || exit 1
mpirun -n 2 ./icb_parpack_cpp || exit 1

# The second targets are compiled with *.pc files generated by cmake.
./dnbdr1_pkg || exit 1
./icb_arpack_c_pkg || exit 1
./icb_arpack_cpp_pkg || exit 1
mpirun -n 2 ./pdndrv1_pkg || exit 1
mpirun -n 2 ./icb_parpack_c_pkg || exit 1
mpirun -n 2 ./icb_parpack_cpp_pkg || exit 1
