#! /bin/sh

# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ Script for launching all executable tests.                                 ┃
# ┃                                                                            ┃
# ┃ É. Canot -- IPR/CNRS -- 29 Jan 2024                                        ┃
# ┃                                                                            ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

# 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
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

# required for MFPLOT (we set MFPLOT_DIR because MUESLI perhaps not yet installed)
export MFPLOT_DIR="$DIR"/../../../fgl/mfplot

LISTE_PROGS="pcolor_ij_flat \
             pcolor_ij_interp \
             pcolor_XY_flat \
             pcolor_XY_flat_light \
             pcolor_XY_interp \
             pcolor_XY_interp_light \
             pcolor_XY_map_flat \
             pcolor_XY_map_flat_light \
             pcolor_XY_map_interp \
             pcolor_XY_map_interp_light"

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
    yes | ./$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 FGL program tests have been run with success.\n"
else
  echo $ECHO_OPT "\n *** All the FGL program tests found have been run with success,"
  echo $ECHO_OPT "     but some of the executables have been discarded, because not found!\n"
fi

