#!/bin/bash

# launcher for matlab/octave/ifit data plotter

############################################
# Start of standard CMake-generated preamble
set -e
FILE=${0}
# First, probe if we are calling script at
# install-time (postinst/postrm) or we are 
# running in an installation "userland"
SCRIPT=`basename $FILE`
if [[ ${SCRIPT} == *.postinst || ${SCRIPT} == *.postrm || ${SCRIPT} == *rpm-tmp* ]];
  then
  MCCODE_BINDIR=/usr/bin
else
  readlinkf(){ python3 -c 'import sys,pathlib
print(pathlib.Path(sys.argv[1]).resolve().absolute())' "$1"
  }
  LINK=$(readlinkf ${FILE}||true)
  if [ "x${LINK}" != "x" ]
 then
    FILE=${LINK}
  fi
  MCCODE_BINDIR="$( cd -P "$( dirname "${FILE}" )" && pwd )"
fi
MCCODE_TOOLDIR="${MCCODE_BINDIR}/../share/mcxtrace/tools"
MCCODE_LIBDIR="${MCCODE_BINDIR}/../lib"
MCCODE_RESOURCEDIR="${MCCODE_BINDIR}/../share/mcxtrace/resources"
if [ -d "${MCCODE_TOOLDIR}" ]
 then
    MCCODE_TOOLDIR="$( cd -P "${MCCODE_TOOLDIR}" && pwd )"
else
    MCCODE_TOOLDIR=""
fi
if [ -d "${MCCODE_LIBDIR}" ]
 then
    MCCODE_LIBDIR="$( cd -P "${MCCODE_LIBDIR}" && pwd )"
else
    MCCODE_LIBDIR=""
fi
if [ -d "${MCCODE_RESOURCEDIR}" ]
 then
    MCCODE_RESOURCEDIR="$( cd -P "${MCCODE_RESOURCEDIR}" && pwd )"
else
    MCCODE_RESOURCEDIR=""
fi
# End of standard preamble
############################################

LIB="${MCCODE_TOOLDIR}/matlab/mxplot"
TOOL="mcplot"
VERS="v3.5.39"

canrun_m() {
    if ! [ -x ${LIB}/${TOOL}.m ]; then
        exit 127;
    fi
    
    # is matlab on the path?
    if ! [ `which matlab` ]; then
            exit 127;
    fi
}

canrun_i() {
    if ! [ -x ${LIB}/${TOOL}.m ]; then
        exit 127;
    fi
    
    # is ifit on the path?
    if ! [ `which ifit` ]; then
            exit 127;
    fi
}

canrun_o() {
    if ! [ -x ${LIB}/${TOOL}.m ]; then
        exit 127;
    fi
    
    # is octave on the path?
    if ! [ `which octave` ]; then
            exit 127;
    fi
}

# defaults
if ( canrun_m ); then RUN_MATLAB=1; else RUN_MATLAB=0; fi
if ( canrun_o ); then RUN_OCTAVE=1; else RUN_OCTAVE=0; fi
if ( canrun_i ); then RUN_IFIT=1;   else RUN_IFIT=0;   fi

# handle input options, overriding defaults
while getopts hiom flag
do
  case "${flag}" in
  o)
    RUN_OCTAVE=1
    RUN_MATLAB=0
    RUN_IFIT=0
  ;;
  m)
    RUN_MATLAB=1
    RUN_OCTAVE=0
    RUN_IFIT=0
  ;;
  i)
    RUN_IFIT=1
    RUN_MATLAB=0
    RUN_OCTAVE=0
  ;;
  *)
    echo "$0: plot a McCode simulation result using the Matlab/Octave backend."
    echo "usage:"
    echo "  $0 FILE|DIR"
    echo "        Display the specified monitor file or directory."
    echo "  $0 [-png|-jpg|-fig|-eps|-pdf] [FILE|DIR]"
    echo "        Export the specified monitor file or directory to given format."
    echo "  $0 -m FILE|DIR"
    echo "        Explicitely request to use Matlab"
    echo "  $0 -o FILE|DIR"
    echo "        Explicitely request to use Octave"
    exit 1
    ;;
  esac
done  
shift $((OPTIND-1))


if [ "$RUN_MATLAB" == "1" ]; then
    matlab -nosplash -nodesktop -r "addpath('${LIB}');${TOOL} $*"
elif [ "$RUN_IFIT" == "1" ]; then
    ifit -r $*
elif [ "$RUN_OCTAVE" == "1" ]; then
    octave --eval "addpath('${LIB}');${TOOL} $*;"
else
    echo ":: Failed to run Matlab/iFit/Octave ${TOOL}, trying default ${TOOL} instead."
    echo ":: If this fails too, consider reinstalling ${TOOL}."
    echo ""

    # Try old Perl-version of mcplot if Python version cannot run
    ${TOOL} $*
fi
