#! /bin/bash

function  julia_check() {
    if  [[ -e examples/julial ]]; then
        JULIAL=examples/julial
    else
        JULIAL=julial
    fi
    
    # Check if 'julial' is in the system PATH
    if ! command -v $JULIAL &> /dev/null; then
        echo "Error: program 'julial' is not available."
        echo "It is available from the examples/ subdirectory. You should either:"
        echo ""
        echo "- cp examples/julial ~/bin (any directory on your path would work)"
        echo ""
        echo "- or run the program from the root directory of the project. That is:"
        echo "   examples/scripts/run_test_<fill relevant name>"
        echo ""
        echo " Also, don't forget to run julial src/deps.jl before running this script"
        echo -n " to install needed packages."
        echo ""
        echo "Anyway, bye for now!"
        exit 1
    fi
    
    REQUIRED_VER="1.12"
    CURRENT_VER=$(julia -e 'print(VERSION)')
    # sort -V handles multi-digit version components correctly
    if [ "$(printf '%s\n%s' "$REQUIRED_VER" "$CURRENT_VER" | sort -V | head -n1)" != "$REQUIRED_VER" ]; then
        echo "Error: Julia version $CURRENT_VER is below the required $REQUIRED_VER."
        echo "Please update Julia at https://julialang.org/downloads/"
        echo ""
        echo "Important: Use juliaup to install julia, not the default package provided."
        exit 1
    fi
    echo "Julia check completed!"
}

function  kill_grep() {
    TDIR=/tmp/sariel/kill_grep
    mkdir -p $TDIR >& /dev/null
    TFL=$TDIR/kill_grep.txt

    ps auxw | grep $1 | grep -v kill_grep | grep -v grep > $TFL
    cat $TFL |  gi -v kill_grep

    echo "---"
    kill -9 `cat $TFL | awk '{print $2}'` >& /dev/null
}

julia_check

# Lets make sure no irritating processes are running...
kill_grep java   >& /dev/null
kill_grep okular   >& /dev/null
kill_grep emacs   >& /dev/null
kill_grep firefox >& /dev/null
kill_grep julia >& /dev/null

DRES=various/results/
mkdir -p $DRES >& /dev/null

#exit

rm $DRES/*.txt

examples/scripts/run_test_queries_characters_serial >& $DRES/chars_ser.txt
examples/scripts/run_test_queries_characters_mt     >& $DRES/chars_mt.txt

examples/scripts/run_test_queries_sigspatial_serial >& $DRES/sig_ser.txt
examples/scripts/run_test_queries_sigspatial_mt     >& $DRES/sig_mt.txt

examples/scripts/run_test_queries_gbad_serial       >& $DRES/gbad_ser.txt
examples/scripts/run_test_queries_gbad_mt           >& $DRES/gbad_mt.txt

examples/scripts/run_test_queries_geolife_serial    >& $DRES/geolife_ser.txt
examples/scripts/run_test_queries_geolife_mt        >& $DRES/geolife_mt.txt


echo "sigspatial    : " `grep "Running time" $DRES/sig_ser.txt`
echo "sigspatial MT : " `grep "Running time" $DRES/sig_mt.txt`

echo "Characters    : " `grep "Running time" $DRES/chars_ser.txt`
echo "Characters MT : " `grep "Running time" $DRES/chars_mt.txt`

echo "GeoLife       : " `grep "Running time" $DRES/geolife_ser.txt`
echo "GeoLife MT    : " `grep "Running time" $DRES/geolife_mt.txt`
