#!/bin/bash

############################################################
# The Dynamixs.AI Developer Script
# start, build, hot, setup, deploy
# 
############################################################

strip_dash() {
    echo "$1" | sed 's/^-//'
}

    echo "     _            _   _          _  "     
    echo "  __| | _____   _(_) | |__   ___| |_ __"  
    echo " / _\` |/ _ \\ \\ / / | | '_ \\ / _ \\ | \'_ \\" 
    echo "| (_| |  __/\ V /| | | | | |  __/ | |_) |"
    echo " \__,_|\___| \_/ |_| |_| |_|\___|_| .__/ "
    echo "                                  |_|  "
    echo "    Imixs Developer Script..."
    echo "_________________________________________"

# Verify params
if [[ "$(strip_dash $1)" == "setup" ]]; then
    echo " Dev Setup..."
    sudo chmod -R 777 docker/deployments/
    echo " starting dev environment..."
    mvn clean install -Pdebug
    cp ./*-app/target/*.war ./docker/deployments/
    docker compose -f ./docker-compose-dev.yaml up
fi


if [[ "$(strip_dash $1)" == "start" ]]; then
    echo " Start Dev Environment..."
    docker compose -f ./docker-compose.yaml up
fi

if [[ "$(strip_dash $1)" == "dev" ]]; then
    echo " Start Dev Environment..."
    mvn clean install -DskipTests -Pdebug
    docker compose -f ./docker-compose-dev.yaml up
fi

if [[ "$(strip_dash $1)" == "docker" ]]; then
    echo " Build Docker image..."
    mvn clean install -DskipTests -Pdocker
fi



if [[ "$(strip_dash $1)" == "build" ]]; then
    echo " Build and Autodeploy..."
    mvn clean install -DskipTests
fi

if [[ "$(strip_dash $1)" == "test" ]]; then
    echo " Run JUnit Tests..."
    mvn clean test
fi

if [[ "$(strip_dash $1)" == "hot" ]]; then
    echo "* Hotdeploy..."
    cd *-app
    mvn manik-hotdeploy:hotdeploy
    cd ..
fi


# Überprüfen, ob keine Parameter übergeben wurden - standard build
if [[ $# -eq 0 ]]; then

    echo " Run with ./dev.sh -XXX"
    echo " "
    echo "   -start  : start Docker Containers"
    echo "   -dev  : start the Docker dev environment"
    echo "   -build  : build application and redeploy"
    echo "   -hot    : Manik Hotdeploy"
    echo "   -test   : run tests"
    echo "   -setup  : setup dev environment - rebuild docker containers "
    echo "_________________________________________"
    echo " "

fi