#!/bin/bash

base_path=`pwd`
tmp_dir="mygeant4_build"
bin_dir="bin"

usage="
Usage: `basename $0` [OPTION]... [FOLDER]
Compile a Geant4 project and create an executable using cmake.
In case a CMakeLists.txt file is missing a generic one will be created.
The script will configure and compile the project in folder ${tmp_dir} in the project's folder.
The executable will be installed in folder ${bin_dir} in the project's folder.

By default the project is assumed to be in the current folder.
If the user provides a [FOLDER], it will be used as a base directory.

  -h, --help              display this help and exit
  -c, --clean             by default the build folder will not by deleted, in order
                          to speedup compilation.
                          With this flag the build folder will be deleted
"

function make_cmakelist
{
   echo -e "set(projName ${filename%.*})" > CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '#----------------------------------------------------------------------------' >> CMakeLists.txt
   echo -e '# Setup the project' >> CMakeLists.txt
   echo -e 'cmake_minimum_required(VERSION 2.6 FATAL_ERROR)' >> CMakeLists.txt
   echo -e 'project(${projName})' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '#----------------------------------------------------------------------------' >> CMakeLists.txt
   echo -e '# Find Geant4 package, activating all available UI and Vis drivers by default' >> CMakeLists.txt
   echo -e '# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui' >> CMakeLists.txt
   echo -e '# to build a batch mode only executable' >> CMakeLists.txt
   echo -e '#' >> CMakeLists.txt
   echo -e 'option(WITH_GEANT4_UIVIS "Build program with Geant4 UI and Vis drivers" ON)' >> CMakeLists.txt
   echo -e 'if(WITH_GEANT4_UIVIS)' >> CMakeLists.txt
   echo -e '  find_package(Geant4 REQUIRED ui_all vis_all)' >> CMakeLists.txt
   echo -e 'else()' >> CMakeLists.txt
   echo -e '  find_package(Geant4 REQUIRED)' >> CMakeLists.txt
   echo -e 'endif()' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '#----------------------------------------------------------------------------' >> CMakeLists.txt
   echo -e '# Setup Geant4 include directories and compile definitions' >> CMakeLists.txt
   echo -e '# Setup include directory for this project' >> CMakeLists.txt
   echo -e '#' >> CMakeLists.txt
   echo -e 'include(${Geant4_USE_FILE})' >> CMakeLists.txt
   echo -e 'include_directories(${PROJECT_SOURCE_DIR}/include)' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '#----------------------------------------------------------------------------' >> CMakeLists.txt
   echo -e '# Locate sources and headers for this project' >> CMakeLists.txt
   echo -e '# NB: headers are included so they will show up in IDEs' >> CMakeLists.txt
   echo -e '#' >> CMakeLists.txt
   echo -e 'file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)' >> CMakeLists.txt
   echo -e 'file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '#----------------------------------------------------------------------------' >> CMakeLists.txt
   echo -e '# Add the executable, and link it to the Geant4 libraries' >> CMakeLists.txt
   echo -e '#' >> CMakeLists.txt
   echo -e 'add_executable(${projName} ${projName} ${sources} ${headers})' >> CMakeLists.txt
   echo -e 'target_link_libraries(${projName} ${Geant4_LIBRARIES})' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '#----------------------------------------------------------------------------' >> CMakeLists.txt
   echo -e '# Copy all scripts to the build directory, i.e. the directory in which we' >> CMakeLists.txt
   echo -e '# build B1. This is so that we can run the executable directly because it' >> CMakeLists.txt
   echo -e '# relies on these scripts being in the current working directory.' >> CMakeLists.txt
   echo -e '#' >> CMakeLists.txt
   echo -e 'set(PROJ_SCRIPTS' >> CMakeLists.txt
   echo -e '  ' >> CMakeLists.txt
   echo -e '  )' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e 'foreach(_script ${PROJ_SCRIPTS})' >> CMakeLists.txt
   echo -e '  configure_file(' >> CMakeLists.txt
   echo -e '    ${PROJECT_SOURCE_DIR}/${_script}' >> CMakeLists.txt
   echo -e '    ${PROJECT_BINARY_DIR}/${_script}' >> CMakeLists.txt
   echo -e '    COPYONLY' >> CMakeLists.txt
   echo -e '    )' >> CMakeLists.txt
   echo -e 'endforeach()' >> CMakeLists.txt
   echo -e '' >> CMakeLists.txt
   echo -e '#----------------------------------------------------------------------------' >> CMakeLists.txt
   echo -e '# Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX' >> CMakeLists.txt
   echo -e '#' >> CMakeLists.txt
   echo -e 'install(TARGETS ${projName} DESTINATION bin)' >> CMakeLists.txt
}

for arg in "$@" ; do
   case $1 in
      -c|--clean)
         clean_tmp_path=1
         shift
      ;;
      -h|--help)
         echo "$usage"
         exit 0
      ;;
      -*|--*)
         echo "Unknown option."
         echo "Type `basename $0` -h or --help for usage"
         exit 1
      ;;
      *)
         base_path=("$arg")
         shift
      ;;
   esac
done

if [ ! -d ${base_path} ] ; then echo -e "Folder ${base_path} does not exist!\nPlease give a valid folder!" ; exit 2 ; fi
cd ${base_path}

no_files_with_main=`grep main *.cc 2>/dev/null | wc -l`
if [ ${no_files_with_main} -eq 0 ] ; then echo -e "Folder ${base_path} doesn't contain a .cc file with main." ; exit 3 ; fi
if [ ${no_files_with_main} -gt 1 ] ; then echo -e "Folder ${base_path} contains more than one .cc files with main!" ; exit 4 ; fi

filename=`grep -l main *.cc 2>/dev/null`

if [ ${clean_tmp_path} ] ; then rm -rf ${tmp_dir} ; fi
if [ ! -f CMakeLists.txt ] ; then
   $(make_cmakelist)
fi
if [ ! -d ${tmp_dir} ] ; then mkdir ${tmp_dir} ; fi
cd ${tmp_dir}
cmake -DCMAKE_INSTALL_PREFIX=../ ../
make install -j2
cd ../
if [ ${clean_tmp_path} ] ; then rm -rf ${tmp_dir} ; fi

unset base_path
unset tmp_dir
unset bin_dir
unset usage
unset clean_tmp_path
unset no_files_with_main
unset filename

exit 0
