#!/bin/bash

# SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
#
# SPDX-License-Identifier: Apache-2.0

# This script is used to build the openSUSE Tumbleweed images

set -xeuo pipefail

CONTAINER_DRIVER=${CONTAINER_DRIVER:-podman}
declare -r DOCKER_REPOSITORY=ghcr.io/ornladios/adios2
declare -u ENABLE_PUSH=

#=============================================================================
# Functions

# Function to build a docker image
function docker_build() {
  local docker_file=$1
  local docker_tag=$2
  local args=(--rm)


  "${CONTAINER_DRIVER}" build "${args[@]}"  \
    --file "${docker_file}" \
    --tag "${DOCKER_REPOSITORY}:${docker_tag}-$(date +"%Y%m%d")" \
    .

  if [[ -n "${ENABLE_PUSH}" ]]; then
    "${CONTAINER_DRIVER}" push "${DOCKER_REPOSITORY}:${docker_tag}-$(date +"%Y%m%d")"
  fi
}

function usage() {
  echo "Usage: $0 [-p <PROJECT>] [-m <MILESTONE>]"
  echo "    -p    Push to the docker registry"
  exit 0
}

#=============================================================================
# Main
while getopts "p" o; do
  case "${o}" in
    p) ENABLE_PUSH=true ;;
    *) usage ;;
  esac
done

docker_build opensuse-tw-sanitizer-base.dockerfile ci-opensuse-tw-sanitizer-base
docker_build opensuse-tw-full-stack-onbuild.dockerfile ci-opensuse-tw-full-stack-onbuild
docker_build opensuse-tw-asan.dockerfile ci-opensuse-tw-asan
docker_build opensuse-tw-msan.dockerfile ci-opensuse-tw-msan
docker_build opensuse-tw-tsan.dockerfile ci-opensuse-tw-tsan
docker_build opensuse-tw-ubsan.dockerfile ci-opensuse-tw-ubsan
