#!/usr/bin/env bash
# =============================================================================
#  download-rpms.sh - OPTIONAL helper, run on a machine WITH internet access
# =============================================================================
#
#  Only needed when the offline RHEL 9.6 host has no local RHEL repository
#  (for example no mounted RHEL DVD / ISO).
#
#  Run this on an online RHEL 9.6 x86_64 machine that has the same packages
#  installed as the offline host (ideally a copy of it). It downloads tomcat,
#  Java (OpenJDK headless), httpd-tools (for the "ab" load tool) and every
#  dependency that machine is missing into ./rpms/. Copy the whole kit folder,
#  including rpms/, to the offline host; start-tomcat.sh then installs from
#  rpms/ without any network.
#
#  USAGE:   sudo ./download-rpms.sh                   Java 17 (default)
#           sudo JAVA_VERSION=21 ./download-rpms.sh   another Java version
#                                                     (set the same JAVA_VERSION in settings.conf)
# =============================================================================

set -euo pipefail

KIT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
RPM_DIR="$KIT_DIR/rpms"
JAVA_VERSION="${JAVA_VERSION:-17}"
PACKAGES=(tomcat "java-${JAVA_VERSION}-openjdk-headless" httpd-tools)

source /etc/os-release
if [[ $ID != rhel || $VERSION_ID != 9.6 ]]; then
    echo "[ WARN ] This machine is '$PRETTY_NAME'. For the best match use RHEL 9.6." >&2
fi

mkdir -p "$RPM_DIR"

# --resolve downloads the dependencies that are not installed on this machine.
dnf download --resolve --arch x86_64 --arch noarch --destdir "$RPM_DIR" "${PACKAGES[@]}"

echo
echo "[  OK  ] Downloaded into $RPM_DIR:"
ls -1 "$RPM_DIR"
echo
echo "Next: copy the whole folder '$KIT_DIR' to the offline host and run ./start-tomcat.sh"
