#!/usr/bin/env bash
set -euo pipefail

project_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$project_dir"

version="$(tr -d '\r\n' < VERSION)"
revision="unknown"
if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    revision="$(git rev-parse --short=12 HEAD)"
fi

GMAO_VERSION="$version" GIT_VERSION="$revision" \
    docker compose -f docker-compose.yml build gmao

if [[ "${1:-}" == "--latest" ]]; then
    if [[ "$version" == *-* ]]; then
        echo "Le tag latest est refuse pour une preversion: $version" >&2
        exit 1
    fi
    docker tag "gmao-college:$version" "gmao-college:latest"
fi

echo "Image construite: gmao-college:$version (revision $revision)"
