#!/bin/bash

## Helper functions

function wait_for_server {
    port=$1
    while ! nc -z -w 1 127.0.0.1 $port; do
        sleep 0.1
    done
    sleep 1
}

function echo_info { printf "\033[1;36m~~ $1 ~~\033[0m\n"; }
function echo_success { printf "\033[1;32m~~ $1 ~~\033[0m\n\n"; }
function echo_failure { printf "\033[1;31m~~ $1 ~~\033[0m\n\n"; }


## Environment setup
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR"

# Create a temporary directory for output files
tmpdir=/tmp/mg_client/output
if [ -d $tmpdir ]; then
    rm -rf $tmpdir
fi
mkdir -p $tmpdir
cd $tmpdir

# Find memgraph binaries.
memgraph_dir="$DIR/../../build"
if [ ! -d $memgraph_dir ]; then
    memgraph_dir="$DIR/../../build_release"
fi

# Find mg_client binaries.
client_dir="$memgraph_dir/tools/src"
if [ ! -d $client_dir ]; then
    echo_failure "mg-client directory not found"
    exit 1
fi

# Find tests dir.
tests_dir="$DIR/client"
if [ ! -d $tests_dir ]; then
    echo_failure "Directory with tests not found"
    exit 1
fi

# Find tests input files.
input_dir="$tests_dir/input"
if [ ! -d $input_dir ]; then
    echo_failure "Directory with tests input files not found"
    exit 1
fi

# Check and generate SSL certificates
use_ssl=false
key_file=""
cert_file=""
if [ "$1" == "--use-ssl" ]; then
    use_ssl=true
    key_file=".key.pem"
    cert_file=".cert.pem"
    openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
        -subj "/C=HR/ST=Zagreb/L=Zagreb/O=Memgraph/CN=db.memgraph.com" \
        -keyout $key_file -out $cert_file || exit 1
fi


## Startup

# Start the memgraph process and wait for it to start.
echo_info "Starting memgraph"
$memgraph_dir/memgraph --cert-file=$cert_file --key-file=$key_file &
pid=$!
wait_for_server 7687
echo_success "Started memgraph"


## Tests

echo_info "Running tests"
echo  # Blank line

client_flags="--use-ssl=$use_ssl"
test_code=0
for output_dir in $tests_dir/output_*; do
    for filename in $input_dir/*; do
        test_name=$(basename $filename)
        test_name=${test_name%.*}
        output_name="$test_name.txt"

        output_format=$(basename $output_dir)
        output_format=${output_format#*_}
        run_flags="$client_flags --output-format=$output_format"

        echo_info "Running test '$test_name' with $output_format output"
        $client_dir/mg_client $run_flags < $filename > $tmpdir/$test_name
        diff -b $tmpdir/$test_name $output_dir/$output_name
        test_code=$?
        if [ $test_code -ne 0 ]; then
            echo_failure "Test '$test_name' with $output_format output failed"
            break
        else
            echo_success "Test '$test_name' with $output_format output passed"
        fi

        # Clear database for each test.
        $client_dir/mg_client $client_flags <<< "MATCH (n) DETACH DELETE n;" \
                                &> /dev/null || exit 1
    done
    if [ $test_code -ne 0 ]; then
        break
    fi
done


## Cleanup
echo_info "Starting test cleanup"

# Shutdown the memgraph process.
kill $pid
wait -n
code_mg=$?

# Check memgraph exit code.
if [ $code_mg -ne 0 ]; then
    echo_failure "The memgraph process didn't terminate properly!"
    exit $code_mg
fi
echo_success "Test cleanup done"

exit $test_code
