#!/usr/bin/env bash

set -eEo pipefail

SCRIPTPATH="$(
        cd "$(dirname "$0")"
        pwd -P
)"

# shellcheck source=ci/docker_commands
source "${SCRIPTPATH}/docker_commands"
# shellcheck source=ci/.colors
source "${SCRIPTPATH}/.colors"

TARGET_FLAVOR=$1
TARGET_ARCH=$2
TARGET_FILE=$3

if [ -z "${TARGET_ARCH}" ] || [ -z "${TARGET_FILE}" ]; then
        echo -e "${RED}Missing required arguments. Usage: build_helper_docker TARGET_FLAVOR TARGET_ARCH TARGET_FILE [PWSH_TARGET_FLAVOR_IMAGE_VERSION]${RST}"
        exit 1
fi

DOCKERFILE=${TARGET_DOCKERFILE:-"Dockerfile.${TARGET_FLAVOR}"}
HELPER_BINARY_POSTFIX=${HELPER_BINARY_POSTFIX:-}

if [[ $IMAGE_SHELL == 'pwsh' ]]; then
        PWSH_TARGET_FLAVOR_IMAGE_VERSION=$4

        if [ -z "${TARGET_FLAVOR}" ] || [ -z "${PWSH_VERSION}" ] || [ -z "${PWSH_TARGET_FLAVOR_IMAGE_VERSION}" ]; then
                echo -e "${RED}Missing required arguments. When IMAGE_SHELL is 'pwsh', PWSH_VERSION, " \
                        "TARGET_FLAVOR, and PWSH_TARGET_FLAVOR_IMAGE_VERSION must be defined${RST}"
                exit 1
        fi

        # NOTE: To find the most recent Powershell Core tag that supports the desired Powershell Core version on the
        # desired ${TARGET_FLAVOR} version, run the following command:
        # ```
        # export PWSH_VERSION="7.3"
        # export PWSH_TARGET_FLAVOR_IMAGE_VERSION="3.18"
        # curl -sL https://mcr.microsoft.com/v2/powershell/tags/list | \
        #   jq -r '.tags[]' | \
        #   grep "${PWSH_VERSION}-${TARGET_FLAVOR}-${PWSH_TARGET_FLAVOR_IMAGE_VERSION}" | \
        #   tail -n 1
        # ```
        # Currently PWSH_IMAGE_DATE isn't being used anywhere but the convention is still used in the Powershell images
        # Right now no Alpine or Ubuntu image we need has a date in the tag but if that changes in the future we can use it
        # and not add the code all over again
        if [ -z "${PWSH_IMAGE_DATE}" ]; then
                BASE_IMAGE="registry.gitlab.com/gitlab-org/gitlab-runner/powershell:${PWSH_VERSION}-${TARGET_FLAVOR}-${PWSH_TARGET_FLAVOR_IMAGE_VERSION}"
        else
                BASE_IMAGE="registry.gitlab.com/gitlab-org/gitlab-runner/powershell:${PWSH_VERSION}-${TARGET_FLAVOR}-${PWSH_TARGET_FLAVOR_IMAGE_VERSION}-${PWSH_IMAGE_DATE}"
        fi
else
        TARGET_FLAVOR_IMAGE_VERSION=$4

        if [ -z "${TARGET_FLAVOR}" ] || [ -z "${TARGET_FLAVOR_IMAGE_VERSION}" ]; then
                echo -e "${RED}Missing required arguments. TARGET_FLAVOR and TARGET_FLAVOR_IMAGE_VERSION must be defined${RST}"
                exit 1
        fi

        if [[ $TARGET_FLAVOR == 'alpine-edge' ]]; then
                BASE_IMAGE="alpine:edge"
        else
                BASE_IMAGE="${TARGET_FLAVOR}:${TARGET_FLAVOR_IMAGE_VERSION}"
        fi
fi

REVISION=${REVISION:-}
if [[ -z "${REVISION}" ]]; then
        REVISION=$(git rev-parse --short=8 HEAD || echo "unknown")
fi

case "${TARGET_ARCH}" in
"x86_64")
        platform_arch='amd64'
        ;;
*)
        platform_arch="${TARGET_ARCH}"
        ;;
esac

binary_file="out/binaries/gitlab-runner-helper/gitlab-runner-helper.${TARGET_ARCH}${HELPER_BINARY_POSTFIX}"

if [ ! -f "$binary_file" ]; then
        echo -e "${RED}Missing binary file ${binary_file}. You probably need to run 'make helper-bin'.${RST}"
        exit 1
fi

cp "$binary_file" dockerfiles/runner-helper/binaries/gitlab-runner-helper
chmod +x dockerfiles/runner-helper/binaries/gitlab-runner-helper

os=$(_docker version -f '{{.Server.Os}}')
platform="${os}/${platform_arch}"

echo -e "Building helper image for: ${GRN}${platform}${RST} based on ${GRN}${BASE_IMAGE}${RST}"

trap cleanup_docker_context_trap ERR SIGINT SIGTERM
setup_docker_context

# shellcheck disable=SC2154
_docker_buildx build \
        --platform "${platform}" \
        --no-cache \
        --build-arg "BASE_IMAGE=${BASE_IMAGE}" \
        --build-arg "ARCH=${platform_arch}" \
        --build-arg "http_proxy=${http_proxy}" \
        --build-arg "DUMB_INIT_VERSION=${DUMB_INIT_VERSION}" \
        --build-arg "GIT_LFS_VERSION=${GIT_LFS_VERSION}" \
        --output "type=tar,dest=$TARGET_FILE" \
        --tag "gitlab/gitlab-runner-helper:$TARGET_ARCH-$REVISION" \
        --file "dockerfiles/runner-helper/${DOCKERFILE}" \
        dockerfiles/runner-helper

trap - ERR SIGINT SIGTERM
cleanup_docker_context
