#!/bin/bash
# 
# To successfully employ this script, make sure that your bash is in
# the bin - directory or adjust accordingly.
#

DIRS=`find Process/Amegic/ -name P?_?`" "`find Process/Amegic/ -name P?_??`

print_help(){
  echo "makelibs version 1.1" && echo && \
  echo "options: -c \"make clean\" before compiling" && \
  echo "         -n skip \"make distclean\" after compiling" && \
  echo "         -o install single module" && \
  echo "         -j [N] allow N compilations at once" && \
  echo "         -m create one library per process" && \
  echo "         -i set include path" && \
  echo "         -h display this help and exit" && echo
}

defaults(){
  JOBS=2
  LIBMODE=2
}

defaults;

while getopts hmcnoj:i: OPT
do
  case $OPT in
  c) CLEAN=TRUE ;;
  n) POSTCLEAN=FALSE ;;
  o) ONE=TRUE ;;
  j) JOBS=$OPTARG ;;
  m) LIBMODE=1 ;;
  i) IPATH=$OPTARG ;;
  h) print_help && exit 0 ;;
  \?)
    shift `expr $OPTIND - 1`
    if [ "$1" = "--help" ]; then print_help && exit 0;
    else 
      echo -n "makelibs: error: unrecognized option "
      if [ $OPTARG != "-" ]; then echo "'-$OPTARG'. try '-h'"
      else echo "'$1'. try '-h'"
      fi
      print_help && exit 1
    fi
    shift 1
    OPTIND=1
  esac
done

if [ "$ONE" = "TRUE" ] ; then 
  echo "makelibs: select module ( q) to quit )"
  select DIRS in $DIRS ; do
    if [ "$DIRS" != "" ] ; then 
      SINGLE="TRUE"
      break
    else 
      if [ "$REPLY" = "q" ] || [ "$REPLY" = "Q" ]  ; then exit ; fi
    fi
  done
fi

if test -z "$CXX"; then export CXX="@CXX@"; fi
if test -z "$CXXFLAGS"; then export CXXFLAGS="@CXXFLAGS@"; fi
if test -z "$CC"; then export CC="@CC@"; fi
if test -z "$CFLAGS"; then export CFLAGS="@CFLAGS@"; fi
if test -z "$FC"; then export FC="@FC@"; fi
if test -z "$FCFLAGS"; then export FCFLAGS="@FCFLAGS@"; fi
if test -z "$CMAKE"; then export CMAKE="@CMAKE_COMMAND@"; fi
if test -z "$SHERPA_CMAKE_PATH"; then export SHERPA_CMAKE_PATH="@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_DATADIR@/SHERPA-MC"; fi
if [[ ! -f $CMAKE ]]; then
  echo "makelibs: the original cmake is not present in the system. Falling back to 'cmake3' or 'cmake'"
  if [[ `type -p cmake3` != "" ]]; then
    export CMAKE=cmake3
  else
    if [[ `type -p cmake` != "" ]]; then
      export CMAKE=cmake
    else
      echo "makelibs: cannot find cmake in the system" 
      exit 1
    fi
  fi
  echo "makelibs: using $CMAKE"
fi

test -z "$IPATH" || IPATH="CURRENT_SHERPASYS="$IPATH
for J in $DIRS ; do
  echo "======================"
  echo "$J";
  echo "======================"
  cd $J 
  test -f CMakeLists.txt.orig || cp CMakeLists.txt CMakeLists.txt.orig
  if test $LIBMODE -eq 1; then
    cp CMakeLists.txt.orig CMakeLists.txt
    diff -q CMakeLists.txt CMakeLists.txt.orig || cp CMakeLists.txt.orig CMakeLists.txt
  elif test $LIBMODE -eq 2; then
    sed -r '/[1-2]_[0-9*]__/ d' < CMakeLists.txt.orig > CMakeLists.txt
    libname="Proc_"$(basename $(pwd))
    sources=$(for i in $(find . -name \*.C | grep -v fsrchannels); do printf $i | sed 's/$/ /1' | tr '\n' ' '; done)
    echo "
project(${libname} LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.11.0)
find_package(SHERPA-MC PATHS ${SHERPA_CMAKE_PATH} REQUIRED)
include(\"GNUInstallDirs\")
set(lib${libname}_la_SOURCES  ${sources})
add_library( ${libname} SHARED \${lib${libname}_la_SOURCES})
amegic_handle_shared_library(${libname})
set(CMAKE_INSTALL_PREFIX \${PROJECT_SOURCE_DIR}/../)
install(TARGETS ${libname} DESTINATION \${CMAKE_INSTALL_PREFIX}/\${CMAKE_INSTALL_LIBDIR})
" > CMakeLists.txt.new
    fsrdir=$(find . -name fsrchannels*)
    test -f $fsrdir/CMakeLists.txt && echo "add_subdirectory(${fsrdir})" >> CMakeLists.txt.new
    diff -q CMakeLists.txt CMakeLists.txt.new || mv CMakeLists.txt.new CMakeLists.txt
  fi
  if test -f CMakeLists.txt; then
  $CMAKE -S . -B . -DCMAKE_INSTALL_LIBDIR=lib || ( echo "cmake in $I failed" ; exit 1)
  if `test "$CLEAN" = "TRUE"` ; then make clean ; fi
  $CMAKE --build . -j $JOBS $IPATH || ( echo "cmake --build in $I failed" ; exit 1)
  $CMAKE --install . || ( echo "cmake --install in $I failed" ; exit 1)
  #if `! test "$POSTCLEAN" = "FALSE"` ; then rm -rf CMakeFiles CMakeCache ; fi
  fi
  cd -
done