# Exits with 1 if Perl is not on the PATH

##  ---- Check Perl exists ----
perlpath=

# Some users may set the above line manually
if [ "x$perlpath" = "x" ]; then
    if [ "x$PERLPATH" != "x" ]; then
	perlpath=$PERLPATH
    else
	# Check if a Greenstone perl-with-cpan exists at expected locations
	if [[ "x$GSDLHOME" != "x" && -d "$GSDLHOME/bin/$GSDLOS$GSDLARCH/perl/bin" ]] ; then	
	    perlpath=$GSDLHOME/bin/$GSDLOS$GSDLARCH/perl
	elif [[ "x$GSDL3SRCHOME" != "x" && -d "$GSDL3SRCHOME/ext-cli/selfcontained-perl-with-cpan/gsperl5-with-cpan-linux64" ]] ; then
	    perlpath=$GSDL3SRCHOME/ext-cli/selfcontained-perl-with-cpan/gsperl5-with-cpan-linux64
	fi
	if [ "x$perlpath" != "x" ]; then
	    # This env var now needs to be set to any perl-with-cpan
	    export GSPERL_WITH_CPAN_HOME=$perlpath
	    # actually need path to bin subfolder
	    perlpath=$perlpath/bin
	    
	    # good time to set PERLPATH env var since not already set?
	    echo "Found perl-with-cpan at standard location, setting PERLPATH"
	    PERLPATH=$perlpath
	    export PERLPATH
	    PATH=$PERLPATH:$PATH
	    export PATH	    
	else	
	    # Else check if Perl is on the search path
	    # Assume not a perl-with-cpan, so don't bother updating the path
	    perlpath=`which perl 2> /dev/null`
	fi
    fi
fi

# Check that a Perl executable has been found
echo "Perl:"
if [ ! -x "$perlpath" ] ; then
    echo
    if [ "$glilang" = "es" ]; then
    echo "La Interfaz de la $PROGNAME requiere Perl para "
    echo "poder operar, pero éste no aparece en su sistema. Por favor asegúrese "
    echo "de que Perl está instalado y se encuentra en su ruta de búsqueda. A "
    echo "continuación ejecute nuevamente este guión."
    elif [ "$glilang" = "fr" ]; then
    echo "$PROGNAME nécessite Perl pour son fonctionnement,"
    echo "mais perl n'a pas pu être détecté dans votre système. Veuillez vous "
    echo "assurer que perl est installé et est spécifié dans votre chemin de "
    echo "recherche, puis redémarrez ce script."
    elif [ "$glilang" = "ru" ]; then
    echo "âÉÂÌÉÏÔÅÞÎÙÊ $PROGNAME ÔÒÅÂÕÅÔ Perl, ÞÔÏÂÙ ÉÍÅÔØ ×ÏÚÍÏÖÎÏÓÔØ"
    echo "ÒÁÂÏÔÁÔØ, ÎÏ Perl ÎÅ ÂÙÌ × ×ÁÛÅÊ ÓÉÓÔÅÍÅ. ðÏÖÁÌÕÊÓÔÁ, ÐÏÄÔ×ÅÒÄÉÔÅ, ÞÔÏ "
    echo "Perl ÕÓÔÁÎÏ×ÌÅÎ É ÎÁÈÏÄÉÔÓÑ ÎÁ ×ÁÛÅÍ ÐÕÔÉ ÐÏÉÓËÁ, ÚÁÔÅÍ ÐÏ×ÔÏÒÎÏ××ÅÄÉÔÅ"
    echo "ÜÔÏÔ ÓËÒÉÐÔ."
    else
    echo "The $PROGNAME requires Perl in order to operate,"
    echo "but perl could not be detected on your system. Please ensure that perl"
    echo "is installed and is on your search path, then rerun this script."
    fi
    exit 1
fi
echo $perlpath
echo
