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

defaults(){
  JOBS=2
}

defaults;

which scons > /dev/null 2>&1 && SCONS=TRUE
while getopts hscnoj: OPT
do
  case $OPT in
  c) CLEAN=TRUE ;;
  n) POSTCLEAN=FALSE ;;
  o) ONE=TRUE ;;
  j) JOBS=$OPTARG ;;
  s) SCONS=FALSE ;;
  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
  /usr/bin/make2scons x;
  for J in $DIRS ; do
    /usr/bin/make2scons $J 1;
  done
  scons -j$JOBS install;
  exit 0;
fi

if ! test -z "g++ -Wl,--no-as-needed"; then export CXX="g++ -Wl,--no-as-needed"; fi
export CXXFLAGS="-fomit-frame-pointer -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -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 "-fomit-frame-pointer -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g"; then export CFLAGS="-fomit-frame-pointer -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g"; fi
if ! test -z "gfortran"; then export FC="gfortran"; fi
if ! test -z "-g -O2"; then export FCFLAGS="-g -O2"; fi
if ! test -z "gcc -E"; then export CPP="gcc -E"; fi
if ! test -z "g++ -Wl,--no-as-needed -E"; then export CXXCPP="g++ -Wl,--no-as-needed -E"; fi

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

for J in $DIRS ; do
  echo "======================"
  echo "$J";
  echo "======================"
  cd $J 
  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; then
    echo "makeinstall: error: \"make install\" in $I failed"
    exit 1
  fi
  if `! test "$POSTCLEAN" = "FALSE"` ; then make distclean ; fi
  fi
  cd -
done
