#!/bin/sh

# launcher for matlab/octave/ifit data plotter

LIB="/usr/mcstas/3.2/tools/matlab/mcplot/"
TOOL="mcplot"
VERS="3.2"

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
}


if ( canrun_m ); then
    matlab -nosplash -nodesktop -r "addpath('${LIB}');${TOOL} $*"
elif ( canrun_i ); then
    ifit -r $*
elif ( canrun_o ); 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
