#!/bin/bash

# check JAVA, JAVAC, ANT, PERL, FIREFOX, CHROME, EDGE if Win, SAFARI if Mac
verbose=0
if [ "x$1" = "x-verbose" ] ; then
    verbose=1
fi
    
version=
result=

echo ""
echo "Checking for Java..."
version=`java -version 2>&1`
result=$?
if [ $result != 0 ] ; then
    #echo "Result: $result"
    echo "   NO JAVA installed. Won't be able to run the automated tutorial tests."
else
    if [ $verbose != 0 ] ; then
	echo "   Java version installed:"
	echo "    $version"
    else	
	echo "   java installed."
    fi
fi

echo ""
echo "Checking for JavaC..."
version=`javac -version 2>&1`
result=$?
if [ $result != 0 ] ; then
    #echo "Result: $result"
    echo "   NO JAVAC installed. Won't be able to (re)compile the test classes."
else
    echo "   javac installed, version $version"
fi

echo ""
echo "Checking for Ant..."
version=`ant -version`
result=$?
if [ $result != 0 ] ; then
    #echo "Result: $result"
    echo "   NO ANT installed. Won't be able to run the ant targets."
    echo "   to build and run tutorial tests"
else
    echo "   ant installed, version: $version."
fi


echo ""
echo "Checking for Perl..."
version=`perl -version`
result=$?
if [ $result != 0 ] ; then
    #echo "Result: $result"
    echo "   NO PERL installed. Won't be able to run collection building and perl scripts"
    echo "   during automated tutorial testing"
else
    if [ $verbose != 0 ] ; then
	echo "   Perl version installed:"
	echo "    $version"
    else	
	echo "   perl installed."
    fi
fi

echo ""
echo "Checking for Firefox..."
version=`firefox -version`
result=$?
if [ $result != 0 ] ; then
    #echo "Result: $result"
    echo "   NO FIREFOX installed. Can't use it for browser tests"
else	
    echo "   Firefox installed, version: $version"
fi

echo ""
echo "Checking for Chrome..."
version=`google-chrome -version`
result=$?
if [ $result != 0 ] ; then
    #echo "Result: $result"
    echo "   NO CHROME installed. Can't use it for browser tests"
else	
    echo "   Chrome installed, version: $version"
fi

echo ""
os=`uname -s`
#echo "Running OS: $os"
# duckduckgo chatbot: following will lowercase all uppercase chars in any bash version
os=`echo "$os" | tr '[:upper:]' '[:lower:]'`
echo "Running OS: $os"

if [ "x$os" = "xdarwin" ] ; then
    echo "*** Darwin"
    # Per Search Assist (duckduckgo chatbot):
    # To check the version of Safari from the command line, you can use the following command: defaults read /Applications/Safari.app/Contents/Info.plist CFBundleShortVersionString. This will display the version number of Safari installed on your Mac.

    echo ""
    echo "Checking for Safari..."
    version=`defaults read /Applications/Safari.app/Contents/Info.plist CFBundleShortVersionString`
    result=$?
    if [ $result != 0 ] ; then
	#echo "Result: $result"
	echo "   NO SAFARI installed. Can't use safari for browser tests"
    else	
	echo "   Safari installed, version: $version"
    fi
fi

echo ""
echo "****************"
echo "  You may want to first run ./setenv.sh, to set up the environment"
echo "  for automated tutorial testing, *before* running this script $0"
echo "****************"
echo ""
