#!/bin/bash

if ! type -p cmake >& /dev/null
then
  echo "CMake not available."
  exit 1
fi

top_dir="$(cd $(dirname ${BASH_SOURCE[0]}) >&/dev/null && pwd)"
cmake_dir_kokkos=$top_dir/.cmake-kokkos

if [ -z ${WC_GEN_KK_BUILD} ] ; then
	build_dir_kokkos=$top_dir/build/kokkos
else
	build_dir_kokkos=${WC_GEN_KK_BUILD}
fi

mkdir -p  $build_dir_kokkos

#src_rn_dir=$cmake_dir_kokkos/src
#mkdir -p $src_rn_dir

#cd $top_dir/src
#cp *.cxx ${src_rn_dir}
#for file in *.kokkos
#do
#  cp  $file ${src_rn_dir}/$(basename "$file" .kokkos).kokkos.cxx
#done


build_cmds="$@"

echo
echo "======================="
echo "Building CXX components"
echo "======================="
echo
#cd $build_dir_cxx
#[ ! -f Makefile ] && \
#    (cmake $cmake_dir_cxx -DCMAKE_CXX_EXTENSIONS=Off || exit 2)
#make $build_cmds || exit 3

echo
echo "=========================="
echo "Building Kokkos components"
echo "=========================="
echo
cd $build_dir_kokkos
[ ! -f Makefile ] && \
    (cmake $cmake_dir_kokkos \
    -DCUDA_TOOLKIT_ROOT_DIR=/opt/cuda \
    -DCMAKE_CXX_EXTENSIONS=Off \
    -DCMAKE_CXX_COMPILER_FORCED=On -DCMAKE_CXX_COMPILER=$(type -p nvcc_wrapper) || exit 4)
make $build_cmds || exit 5
