#!/usr/bin/env bash
# Signed by TECC-SE - T524467 (Logan MAUZAIZE)

basedir="${bamboo_build_working_directory}"

function main() {
    which docker >/dev/null 2>&1 || {
        echo "'docker' command is required" >&2
        return 1
    }

    local image_version="${bamboo_buildNumber}"
    local image_revision="${bamboo_planRepository_1_revision}"
    local image_basetag='nexusoss-docker-int.airfrance.fr/sspt/sspt-retro'
    local image_tag="${image_basetag}:${image_version}"
    
    {
        echo "Generating Docker image '${image_tag}'" && \
        docker build -t "${image_tag}" --build-arg "version=${image_version}" --build-arg "revision=${image_revision}" --build-arg "tag=${image_tag}" "${basedir}/ci/asset/" && \
        echo "Pushing Docker image '${image_tag}'" && \
        docker push "${image_tag}"
    } || {
        local rc="$?"
        echo "Removing Docker image '${image_tag}'" && \
        docker rmi "${image_tag}"
        return "${rc}"
    }
    
}

main "$@"
