#! /bin/sh

# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ Script for launching all executable tests.                                 ┃
# ┃                                                                            ┃
# ┃ É. Canot -- IPR/CNRS -- 30 Sep 2020                                        ┃
# ┃                                                                            ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

# check if the selected shell for running the current script is
# compatible with 'echo -e'
if [ "`echo -e item`" = "item" ]; then
   ECHO_OPT=-e
else
   ECHO_OPT=
fi

# determine the compiler used
DIR=`pwd`
IFS="/"
set $DIR
shift
while [ "$#" -ne "2" ]; do
  shift
done
F90C_TAG=$1
IFS=" "

# set LAPACK_DIR from the local config
# fix: doublequotes around $DIR are required to take into account
# some blanks in the path...
set `grep LAPACK_DIR "$DIR"/../Makefile.config`
shift
shift
LAPACK_DIR=$1

# required for OPTIM mode (use of shared libraries)
if [ -z "$LD_LIBRARY_PATH" ]; then
  LD_LIBRARY_PATH="$DIR"/../lib:$LAPACK_DIR/$F90C_TAG
else
  LD_LIBRARY_PATH="$DIR"/../lib:$LAPACK_DIR/$F90C_TAG:$LD_LIBRARY_PATH
fi
export LD_LIBRARY_PATH

LISTE_PROGS="test_mod_physunits \
             test_mod_core \
             test_mod_sparse_1 \
             test_mod_sparse_2 \
             test_mod_elmat \
             test_mod_fileio \
             test_mod_elfun \
             test_mod_ops \
             test_mod_datafun \
             test_mod_specfun \
             test_mod_matfun_1 \
             test_mod_matfun_2 \
             test_mod_polyfun \
             test_mod_funfun"

STATUS=0

for PROG in $LISTE_PROGS; do
  echo "--- ok to run $PROG?"
  read ANS
  if [ "$ANS" != "n" ]; then
    if ! [ -x $PROG ]; then
      echo "executable '$PROG' cannot be found!"
      STATUS=1
      continue
    fi
    ./$PROG
    status=$?
    # check a returned status different of 0 (Fortran program must contain a
    # "stop 1" instruction to trigger the exit of the current script.
    if [ "$status" != "0" ]; then
      echo $ECHO_OPT "\n *** ERROR in $PROG -- script aborted"
      exit 1
    fi
    echo "--- end of $PROG!"
  fi
done

if [ "$STATUS" = "0" ]; then
  echo $ECHO_OPT "\n *** All the FML program tests have been run with success.\n"
else
  echo $ECHO_OPT "\n *** All the FML program tests found have been run with success,"
  echo $ECHO_OPT "     but some of the executables have been discarded, because not found!\n"
fi

