#!/usr/bin/env bash

SCRIPT_DIR=$(cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P)

BUILD_MS=0
BUILD_GS=0
CLEAN_MS=0
CLEAN_GS=0
SKIPTEST=""

GS_SRC=$SCRIPT_DIR/Server
MS_SRC=$SCRIPT_DIR/Management-Server

CLEANOPT=""
_JAR_DIR="$SCRIPT_DIR/builddir"

help()
{
    echo "Usage: $0 [-h] <-m | -g> [-c <m|g>] [-o <path>]";
    echo "  -h: Display this message.";
    echo "  -m: Build the management server.";
    echo "  -g: Build the game server.";
    echo "  -c: Clean: m: management, g: gameserver. "
    echo "      Can specify both. Need at least one if present.";
    echo "  -o: Specify jar-file directory."
    echo "  -q: Quick build - will skip tests.";
}

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

clean_ms()
{
    cd $MS_SRC

    if [[ "$CLEANOPT" == *"m"* ]]; then
	echo "Cleaning the management server."
        sh mvnw clean || error "Failed to clean the MS."
    fi
}

clean_gs()
{
    cd $GS_SRC;
    if [[ "$CLEANOPT" == *"g"* ]]; then
	echo "Cleaning the game server."
        sh mvnw clean || error "Failed to clean the game server. Giving up."
    fi
}

build_ms()
{
    cd $MS_SRC

    sh mvnw package $SKIPTEST || \
	error "Failed to build the management server. Giving up";
}

build_gs()
{
    cd $GS_SRC;

    sh mvnw package $SKIPTEST || \
	error "Failed to build the game server. Giving up."
}

while getopts "hqmgc:o:d" arg; do
    case $arg in
        h)
            help
            exit 0
            ;;
        m)
            BUILD_MS=1
            ;;
        g)
            BUILD_GS=1
            ;;
        c)
            CLEANOPT=${OPTARG}
            ;;
        o)
            _JAR_DIR=${OPTARG}
            ;;
        d)
            echo "BUILD_MS=$BUILD_MS"
            echo "BUILD_GS=$BUILD_GS"
            echo "CLEANOPT=$CLEANOPT"
            echo "_JAR_DIR=$_JAR_DIR"
            ;;
        q)
            SKIPTEST="-DskipTests"
            ;;
    esac
done

if [[ $CLEANOPT != "m" ]]       \
    && [[ $CLEANOPT != "g" ]]   \
    && [[ $CLEANOPT != "mg" ]]  \
    && [[ $CLEANOPT != "" ]]; 
then
    error "Invalid cleaning option '$CLEANOPT'. Valid options are 'm', 'g', 'mg' or none."
fi

if [ $BUILD_MS -eq 0 ] && [ $BUILD_GS -eq 0 ] && [[ $CLEANOPT == "" ]]; then
    error "Need to build or clean at least one of the modules. See -h for details."
fi

# Conditionals inside the functions.
clean_gs
clean_ms
# -----------

if [ -d $_JAR_DIR ]; then
    rm -r $_JAR_DIR
fi

if [ $BUILD_MS -ne 1 ] && [ $BUILD_GS -ne 1 ]; then
    echo "No build options specified. Stop."
    exit 0
fi

mkdir -p $_JAR_DIR || error "Failed to create build directory.";

if [ $BUILD_MS -eq 1 ]; then
    build_ms

    # Will never execute if build_ms fails because it quits upon failure.
    mv -v $MS_SRC/target/*-with-dependencies.jar $_JAR_DIR/ms.jar
fi

if [ $BUILD_GS -eq 1 ]; then
    build_gs

    # Will never execute if build_gs fails because it quits upon failure.
    mv -v $GS_SRC/target/*-with-dependencies.jar $_JAR_DIR/server.jar
fi
