#!/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 "         -s force use of make" && \
  echo "         -i set include path" && \
  echo "         -h display this help and exit" && echo
}

defaults(){
  JOBS=2
  LIBMODE=2
}

defaults;

which scons > /dev/null 2>&1 && SCONS=TRUE
while getopts hsmcnoj:i: OPT
do
  case $OPT in
  c) CLEAN=TRUE ;;
  n) POSTCLEAN=FALSE ;;
  o) ONE=TRUE ;;
  j) JOBS=$OPTARG ;;
  m) LIBMODE=1 ;;
  s) SCONS=FALSE ;;
  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 [ "$SCONS" = "TRUE" ] ; then
  test -z "$IPATH" || IPATH="incdir="$IPATH
  /mt/home/dreichelt/batch/workspace/LesHouches2023/bin/make2scons x;
  for J in $DIRS ; do
    /mt/home/dreichelt/batch/workspace/LesHouches2023/bin/make2scons $J $LIBMODE;
  done
  scons -j$JOBS install $IPATH;
  exit 0;
fi

if ! test -z "g++ -Wl,--no-as-needed"; then export CXX="g++ -Wl,--no-as-needed"; fi
export CXXFLAGS="-std=c++14 -O2";
if ! test -z ""; then export LDFLAGS=""; fi
if ! test -z ""; then export LIBS=""; fi
if ! test -z ""; then export CPPFLAGS=""; fi
if ! test -z "gcc"; then export CC="gcc"; fi
if ! test -z "-g -O2"; then export CFLAGS="-g -O2"; fi
if ! test -z "gfortran"; then export FC="gfortran"; fi
if ! test -z "-g -O2"; then export FCFLAGS="-g -O2"; fi
if ! test -z "g++ -Wl,--no-as-needed -E"; then export CXXCPP="g++ -Wl,--no-as-needed -E"; fi

if echo "x86_64-unknown-linux-gnu" | grep -q "i686"; then
    export CXX="$CXX -m32"
    export CC="$CC -m32"
    export LDFLAGS="$LDFLAGS -m32"
fi

test -z "$IPATH" || IPATH="CURRENT_SHERPASYS="$IPATH
for J in $DIRS ; do
  echo "======================"
  echo "$J";
  echo "======================"
  cd $J 
  test -f Makefile.am.orig || cp Makefile.am Makefile.am.orig
  test -f configure.in.orig || cp configure.in configure.in.orig
  if test $LIBMODE -eq 1; then
    cp configure.in.orig configure.in
    diff -q Makefile.am Makefile.am.orig || cp Makefile.am.orig Makefile.am
  elif test $LIBMODE -eq 2; then
    sed -r '/[1-2]_[0-9*]__/ d' < configure.in.orig > configure.in
    libname="Proc_"$(basename $(pwd))
    sources=$(for i in $(find . -name \*.C | grep -v fsrchannels); do printf $i | sed 's/$/ /1' | tr '\n' ' '; done)
    echo "AUTOMAKE_OPTIONS = subdir-objects
lib_LTLIBRARIES = lib${libname}.la
lib${libname}_la_SOURCES = ${sources}
CURRENT_SHERPASYS ?= /mt/home/dreichelt/batch/workspace/LesHouches2023/include/SHERPA-MC
INCLUDES = -I\$(CURRENT_SHERPASYS)" > Makefile.am.new
    fsrdir=$(find . -name fsrchannels*)
    test -d $fsrdir && echo "SUBDIRS = ${fsrdir}" >> Makefile.am.new
    diff -q Makefile.am Makefile.am.new || mv Makefile.am.new Makefile.am
  fi
  if test -f configure.in; then
  autoreconf -fi
  ./configure --libdir='${prefix}/lib'
  if `test "$CLEAN" = "TRUE"` ; then make clean ; fi
  if ! make install -j $JOBS $IPATH; then
    echo "makeinstall: error: \"make install\" in $I failed"
    exit 1
  fi
  if `! test "$POSTCLEAN" = "FALSE"` ; then make distclean ; fi
  fi
  cd -
done