#!/usr/bin/env bash
SCRIPT_DIR=$(cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P)
LOG_DIR=$SCRIPT_DIR/logs
BUILD_DIR=$SCRIPT_DIR/builddir
GS_SRC=$SCRIPT_DIR/Server

FORCE_REBUILD=0
REFRESH_BUILD=1

GS_EXEC="cd $GS_SRC && java -Dnashorn.args=--no-deprecation-warning -jar $BUILD_DIR/server.jar"

# 0: parallel
# 1: fancy tmux
# 2: tests only
RUN_MODE=0

help()
{
    echo "Usage: $0 [-h] [-q] [-r] [-t] [-x] [-e <filename>] [-o <filename>]"
    echo "  -h: Display this message."
    echo "  -q: Don't perform the incremental build."
    echo "  -r: Force clean rebuild."
    echo "  -t: Only run tests, not the JARs."
    echo "  -x: Run in a fancy tmux session."
    echo "  -e: Write STDERR to 'logs/filename' as well as the terminal.";
    echo "  -o: Write STDOUT to 'logs/filename' as well as the terminal."
}

error()
{
    echo "$1";
    exit 1;
}

rebuild_project()
{
    "$SCRIPT_DIR/build" -qgc
}

refresh_project()
{
    "$SCRIPT_DIR/build" -qg
}

verify_binaries_exist()
{
    if [ ! -f "$SCRIPT_DIR/build/server.jar" ]; then
        error "Game server binary does not exist.";
    fi
}

run_server_parallel()
{
    if ! [ -x "$(command -v java)" ]; then 
        error "Java is not installed."
    fi

    echo "Running in parallel. All logs redirected to proper files."

    sh -c "$GS_EXEC" & wait
}

run_server_tmux()
{
    if ! [ -x "$(command -v tmux)" ]; then
        error "tmux is not installed. Install tmux or don't use this option."
    fi

    tmux new-session "$GS_EXEC"
}

run_server_tests()
{
    cd "$GS_SRC" || error "Could not change to game server source directory."
    sh mvnw test
}

while getopts ":hqrtxe:o:" arg; do
    case $arg in
        h)
            help
            exit 0
            ;;
        q)
            REFRESH_BUILD=0
            ;;
        r)
            FORCE_REBUILD=1
            ;;
        t)
            RUN_MODE=2
            ;;
        o)
            GS_EXEC="$GS_EXEC > >(tee $LOG_DIR/${OPTARG})"
            ;;
        e)
            GS_EXEC="$GS_EXEC 2> >(tee $LOG_DIR/${OPTARG})"
            ;;
        x)
            RUN_MODE=1
            ;;
        *)
            error "Argument -$arg is not a known or valid argument."
            ;;
    esac
done

if [ ! -d $LOG_DIR ]; then
    mkdir -p $LOG_DIR
fi

if [ ! -d $GS_SRC/target ]; then
    rebuild_project || error "Failed to perform a clean build."
else
    if [ $FORCE_REBUILD -eq 1 ]; then
        rebuild_project || error "Failed to perform a clean build."
    else
        if [ $REFRESH_BUILD -eq 1 ]; then
            refresh_project || error "Failed to perform an incremental build."
        fi
    fi
fi;

cd "$SCRIPT_DIR/builddir" || error "Failed to navigate to the build directory."

case $RUN_MODE in
    0)
        # The trap will allow the script to kill the background
        # Java instances upon exiting.
        trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT

        run_server_parallel
        ;;
    1)
        run_server_tmux
        ;;
    2)
        run_server_tests
        ;;
esac
