#!/usr/bin/env bash
# Wrapper script for mcdoc-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
if [ -d "$LIB/../miniconda3" ]; then
    source $LIB/../miniconda3/bin/activate base
fi
LIB="$LIB/../tools/Python/mcdoc/"
TOOL="mcdoc"
VERS="3.3"

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

if ( canrun ); then
    python3 ${LIB}/mcdoc.py $*
else
    echo ":: Failed to run Python  ${TOOL}, trying Perl ${TOOL} instead."
    echo ":: If this fails too, consider reinstalling ${TOOL}."
    echo ""

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