#!/usr/bin/sh

# Call common script
. createCommonStructure_test

cpp_program_name="CppProgramTest"
module_dep_name="Alexandria"
library_dep_name="Boost"
elements_kernel="ElementsKernel"

# Create a unique directory
tmploc=${TEMPORARY_LOCATION}

cd ${tmploc}
cd ${MODULE_NAME_PATH}

# Create a CPP program
AddCppProgram $cpp_program_name -md $module_dep_name -ld $library_dep_name --yes

# if error stop and clean up
if [ $? -ne 0 ]; then
   echo "Error: <AddCppProgram $cpp_program_name -md $module_dep_name -ld $library_dep_name> command failed!" 1>&2
   clean_and_exit 1
fi

# make sure the class files are created
if [ ! -e "src/program/$cpp_program_name.cpp" ]; then
   echo "Error: <src/program/$cpp_program_name.cpp> file not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the elements_depends_on_subdirs Macro is there and ElementsKernel
result=$( grep "elements_depends_on_subdirs" ${MODULE_NAME_PATH}/CMakeLists.txt |
          grep ElementsKernel )
if [ $? -ne 0 ]  ;then
   echo "Error: <elements_depends_on_subdirs> macro with <ElementsKernel> module not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the elements_depends_on_subdirs Macro is there and module dependency
result=$( grep "elements_depends_on_subdirs" ${MODULE_NAME_PATH}/CMakeLists.txt |
           grep -v "#" | grep $module_dep_name )
if [ $? -ne 0 ]  ;then
   echo "Error: <elements_depends_on_subdirs> macro with <$module_dep_name> dependency not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the find_package Macro is there and module dependency
result=$( grep "find_package" ${MODULE_NAME_PATH}/CMakeLists.txt |  grep -v "#" |
          grep $library_dep_name )
if [ $? -ne 0 ]  ;then
   echo "Error: find_package macro not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the elements_add_executable Macro is there
result=$( grep "elements_add_executable" ${MODULE_NAME_PATH}/CMakeLists.txt | grep $cpp_program_name )
if [ $? -ne 0 ]  ;then
   echo "Error: <elements_add_executable> macro with <$cpp_program_name> executable not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the LINK_LIBRARIES Macro is there and ElementsKernel
result=$( grep "LINK_LIBRARIES" ${MODULE_NAME_PATH}/CMakeLists.txt | grep -v "#" |
          grep $elements_kernel )
if [ $? -ne 0 ]  ;then
   echo "Error: <LINK_LIBRARIES> macro with <$elements_kernel> module not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the LINK_LIBRARIES Macro is there and module dependency
result=$( grep "LINK_LIBRARIES" ${MODULE_NAME_PATH}/CMakeLists.txt | grep -v "#" |
          grep $module_dep_name )
if [ $? -ne 0 ]  ;then
   echo "Error: <LINK_LIBRARIES> macro with <$module_dep_name> module not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the LINK_LIBRARIES Macro is there and library dependency
result=$( grep "LINK_LIBRARIES" ${MODULE_NAME_PATH}/CMakeLists.txt | grep -v "#" |
          grep $library_dep_name )
if [ $? -ne 0 ]  ;then
   echo "Error: <LINK_LIBRARIES> macro with <$library_dep_name> library not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the INCLUDE_DIRS is there and ElementsKernel dependency
result=$( grep "INCLUDE_DIRS" ${MODULE_NAME_PATH}/CMakeLists.txt |  grep -v "#" |
          grep $elements_kernel)
if [ $? -ne 0 ]  ;then
   echo "Error: <INCLUDE_DIRS> macro with <$elements_kernel> module not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the INCLUDE_DIRS is there and  module dependency
result=$( grep "INCLUDE_DIRS" ${MODULE_NAME_PATH}/CMakeLists.txt |  grep -v "#" |
          grep $module_dep_name)
if [ $? -ne 0 ]  ;then
   echo "Error: <INCLUDE_DIRS> macro with <$module_dep_name> module dependency not found!" 1>&2
   clean_and_exit 1
fi

# Make sure the INCLUDE_DIRS is there and  library dependency
result=$( grep "INCLUDE_DIRS" ${MODULE_NAME_PATH}/CMakeLists.txt |  grep -v "#" |
          grep $library_dep_name)
if [ $? -ne 0 ]  ;then
   echo "Error: <INCLUDE_DIRS> macro with <$library_dep_name> library dependency not found!" 1>&2
   clean_and_exit 1
fi


# Make sure the elements_install_conf_files Macro is there
result=$( grep "elements_install_conf_files" ${MODULE_NAME_PATH}/CMakeLists.txt )
if [ $? -ne 0 ]  ;then
   echo "Error: <elements_install_conf_files> macro not found!" 1>&2
   clean_and_exit 1
fi

clean_and_exit 0
