#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# One command to (re)build the storm-local Docker image from the CURRENT source.
#
# Why this exists: the binary distribution puts the daemon classpath in `lib`
# (from final-package's Maven deps) but the WORKER classpath in `lib-worker`
# (copied via a fileSet from the storm-client-bin module's target dir). Building
# only `final-package -am` refreshes `lib` but leaves `lib-worker` stale, so
# workers keep running old code. This script rebuilds BOTH storm-client-bin and
# final-package (with -am to refresh their Maven deps), so the tarball -- and the
# image built from it -- reflect the current source on every classpath.
#
# As a final step it also runs prepare-extlib.sh (the extlib-daemon jars that
# docker-compose mounts onto Nimbus), so a single command sets up everything.
# Pass --no-extlib (or set PREPARE_EXTLIB=0) to skip that step.
set -euo pipefail

PREPARE_EXTLIB="${PREPARE_EXTLIB:-1}"
for arg in "$@"; do
  case "${arg}" in
    --no-extlib) PREPARE_EXTLIB=0 ;;
    -h|--help)
      echo "usage: $0 [--no-extlib]"
      echo "  --no-extlib   skip running prepare-extlib.sh after the image build"
      exit 0
      ;;
    *) echo "error: unknown argument: ${arg}" >&2; echo "usage: $0 [--no-extlib]" >&2; exit 2 ;;
  esac
done

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

# Single source of truth: the project version in the repo root pom.xml. (The
# pom's own <version> child, not the <parent> version.) Override with STORM_VERSION.
POM_VERSION="$(python3 -c "import xml.etree.ElementTree as ET; ns={'m':'http://maven.apache.org/POM/4.0.0'}; print(ET.parse('${REPO_ROOT}/pom.xml').getroot().find('m:version', ns).text)")"
VERSION="${STORM_VERSION:-${POM_VERSION}}"
IMAGE="${IMAGE:-storm-local:${VERSION}}"
TARBALL="${REPO_ROOT}/storm-dist/binary/final-package/target/apache-storm-${VERSION}.tar.gz"

# Keep docker-compose's .env in sync so the image tag / build arg / jar path match.
printf '# Auto-generated by build-image.sh from the repo root pom.xml.\nSTORM_VERSION=%s\n' "${VERSION}" > "${HERE}/.env"
echo "==> Storm version: ${VERSION} (from pom.xml; wrote ${HERE}/.env)"

cd "${REPO_ROOT}"

echo "==> [1/2] Building distribution (storm-client-bin + final-package, current source)"
mvn -q clean install -DskipTests -Pdist -Dgpg.skip=true \
    -pl storm-dist/binary/storm-client-bin,storm-dist/binary/final-package -am

echo "==> [2/2] Building Docker image ${IMAGE} from the fresh tarball"
docker build -f "${HERE}/Dockerfile" \
    --build-arg "STORM_VERSION=${VERSION}" \
    -t "${IMAGE}" \
    "${REPO_ROOT}/storm-dist/binary/final-package/target"

if [[ "${PREPARE_EXTLIB}" != "0" ]]; then
  echo
  echo "==> Preparing extlib-daemon (prepare-extlib.sh; --no-extlib to skip)"
  STORM_VERSION="${VERSION}" "${HERE}/prepare-extlib.sh"
fi

echo
echo "Done. Image ${IMAGE} is up to date with the current source."
echo "Next:  cd ${HERE} && docker compose up -d   (recreates containers with the new image)"
