#! /bin/sh

# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ Script for launching all executable tests.                                 ┃
# ┃                                                                            ┃
# ┃ É. Canot -- IPR/CNRS -- 13 Mar 2025                                        ┃
# ┃                                                                            ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

# 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="Arrow_test \
             AxisFontSize_test \
             AxisLineWidth_test \
             Axis_test \
             Bar_test \
             CAxis_test \
             CharEncoding_ISO_test \
             CharEncoding_UTF8_test \
             CharInPixels_test \
             ColorBar_test \
             ColorMap_Extern_test \
             ColorMap_Intern_test \
             ColorMapSize_test \
             Color_Text_and_Lines_test \
             Contour_test \
             ContourF_test \
             CumulHist_test \
             Delaunay_test \
             ErrorBar_test \
             Figure_test\
             GetX11Pixmap_test \
             Ginput_test\
             Ginput_test_2 \
             GinputCustom_test \
             GinputRect_test \
             Grid_test \
             Image_test \
             Labels_and_Languages_test \
             Legend_test\
             Log_test\
             MoveGrObj_test \
             Pan_test \
             Patch_test \
             Patch_test_2 \
             Pcolor_test\
             PlotAnim_test \
             PlotCubicBezier_test \
             PlotCubicSpline_test \
             PlotHist_test \
             PlotQuadrBezier_test \
             PlotVoronoi_test \
             Plot_test \
             Quiver_test\
             Spy_test \
             Streamline_test \
             Symbol_test\
             Text_layout_ISO_test \
             Text_layout_UTF8_test \
             TriFill_test \
             TriMesh_test \
             TriQuiver_test \
             TriStreamline_test \
             Zoom_test "

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
    if [ "$PROG" = "ColorMap_Extern_test" ]; then
      yes | ./$PROG colormaps/*
    else
      yes | ./$PROG
    fi
    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\n *** All the FGL program tests have been run with success."
  echo
  echo "      ================================================================="
  echo "              you should have been obtained 218 EPS and 222 PDF"
  echo "      =================================================================\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

