#!/bin/bash

export ECLPATH="/prog/res/ecl/grid"
eclversion=$($ECLPATH/macros/eclrun --report-versions eclipse | xargs -n1 | sort -gr | head -1)
if [ -z "$eclversion" ] ; then
    eclversion="2022.2";
fi

if [ "$1" == "-v" ] && [ "$2" != "" ] ; then
    eclversion=$2
fi

manualpath="$ECLPATH/$eclversion/manuals/bookshelf.pdf"

if [ -f $manualpath ] ; then

    if  [ -x /usr/bin/xdg-open ] ; then
        /usr/bin/xdg-open $manualpath 2>/dev/null &
    elif  [ -x /usr/bin/evince ] ; then
        /usr/bin/evince $manualpath 2>/dev/null &
    elif  [ -x /usr/bin/kpdf ] ; then
        /usr/bin/kpdf --caption "ECLIPSE manuals" $manualpath &
    elif  [ -x /usr/bin/xpdf ] ; then
        /usr/bin/xpdf $manualpath &
    elif [ -x /usr/bin/acroread ] ; then
        /usr/bin/acroread $manualpath 2>/dev/null &
    else
        echo "Unable to find pdf viewer"
    fi

else
    echo "Unable to find manuals for version $eclversion"
fi
