#!/bin/bash

# compile_src() - Function that compiles the java source code thru maven
function compile_src {
    local src_folder=$1
    pushd $src_folder
    if [ -f pom.xml ]; then
        install_maven
        mvn clean install -U -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none
    fi
    popd
}

# build_docker_image() - Build Docker container image from source code
function build_docker_image {
    local src_folder=$1
    local profile=$2
    install_maven
    install_docker
    pushd $src_folder

    # Cleanup external repo
    sed -i 's|${docker.push.registry}/||g' pom.xml
    local mvn_docker="mvn clean package docker:build"
    if [ $profile ]; then
        mvn_docker+=" -P $profile"
    fi
    if [ $http_proxy ]; then
        if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
            mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy"
        fi
        if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
            mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
        fi
    fi
    if [ $https_proxy ]; then
        if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
            mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy"
        fi
        if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
            mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
        fi
    fi
    eval $mvn_docker
    popd
}