#!/usr/bin/env bash
# Wrapper script for mcgui-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

ARCH=`arch`
UNAME=`uname -s`
if [[ ${UNAME} = Darwin* ]]; then
    OSXVER=`sw_vers -productVersion|cut -f 1 -d.`

    # Check if we are in / - in that case cd to $HOME
    PW==`pwd`
    if [[ ${PWD} = / ]]; then
	cd $HOME
    fi
fi
if [ -d "$LIB/../miniconda3" ]; then
    source $LIB/../miniconda3/bin/activate base
fi

LIB="$LIB/../tools/Python/mcgui"
TOOL="mcgui"
VERS="3.3"

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

    modules="PyQt5"
    cmd=""
    for name in ${modules}; do
        cmd="${cmd}import ${name}; "
    done
    python3 -c "${cmd}"
}

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