#!/usr/bin/env bash
# Wrapper script for mcplot-matplotlib-py

# First ensure that we follow symlink if needed
LINK=`readlink $0`

# Am I a symlink?
if [ "x$LINK" != "x" ]; then
    LIB=`dirname $LINK`
else
    LIB=`dirname $0`
fi
UNAME=`uname -s`
ARCH=`arch`

if [ -d "$LIB/../miniconda3" ]; then
    source $LIB/../miniconda3/bin/activate base
fi

LIB="$LIB/../tools/Python/mcplot/matplotlib"
TOOL="mcplot"
VERS="3.3"

PYTHON="python3"

canrun() {
    if ! [ -x ${LIB}/${TOOL}.py ]; then
        exit 127;
    fi

    modules="matplotlib numpy"
    cmd=""
    for name in ${modules}; do
        cmd="${cmd}import ${name}; "
    done
    $PYTHON -c "${cmd}"
}

if ( canrun ); then
    $PYTHON ${LIB}/${TOOL}.py $*
else
    mcstas_errmsg Failed to run Python ${TOOL} - permissions or missing dependencies\?
fi
