# syntax=docker/dockerfile:1
FROM fedora:42

# Minimal toolchain + rpm build tools + python build deps.
# NOTE: rpmbuild does NOT auto-install BuildRequires. Since we're building
# directly in a container (not via mock), we install the macro packages too.
RUN set -eux;     dnf -y update;     dnf -y install         rpm-build rpmdevtools redhat-rpm-config         gcc make findutils tar gzip rsync         python3 python3-devel         python3-setuptools python3-wheel         pyproject-rpm-macros python3-rpm-macros         openssl-devel         ;     dnf -y clean all

# Build runner script (copies repo, tars, runs rpmbuild)
RUN set -eux;   cat > /usr/local/bin/build-rpm <<'EOF'
#!/usr/bin/env bash
set -euo pipefail

SRC="${SRC:-/src}"
WORKROOT="${WORKROOT:-/work}"
OUT="${OUT:-/out}"

mkdir -p "${WORKROOT}" "${OUT}"
WORK="${WORKROOT}/src"
rm -rf "${WORK}"
mkdir -p "${WORK}"

rsync -a --delete \
  --exclude '.git' \
  --exclude '.venv' \
  --exclude 'dist' \
  --exclude 'build' \
  --exclude '__pycache__' \
  --exclude '.pytest_cache' \
  --exclude '.mypy_cache' \
  "${SRC}/" "${WORK}/"

cd "${WORK}"

# Determine version from pyproject.toml unless provided
if [ -n "${VERSION:-}" ]; then
  ver="${VERSION}"
else
  ver="$(grep -m1 '^version = ' pyproject.toml | sed -E 's/version = "([^"]+)".*/\1/')"
fi

TOPDIR="${WORKROOT}/rpmbuild"
mkdir -p "${TOPDIR}"/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

tarball="${TOPDIR}/SOURCES/bouquin-sqlcipher4-${ver}.tar.gz"
tar -czf "${tarball}" --transform "s#^#bouquin-sqlcipher4/#" .

spec_src="rpm/bouquin-sqlcipher4.spec"

cp -v "${spec_src}" "${TOPDIR}/SPECS/bouquin-sqlcipher4.spec"

rpmbuild -ba "${TOPDIR}/SPECS/bouquin-sqlcipher4.spec" \
  --define "_topdir ${TOPDIR}" \
  --define "upstream_version ${ver}"

shopt -s nullglob
cp -v "${TOPDIR}"/RPMS/*/*.rpm "${OUT}/" || true
cp -v "${TOPDIR}"/SRPMS/*.src.rpm "${OUT}/" || true
echo "Artifacts copied to ${OUT}"
EOF

RUN chmod +x /usr/local/bin/build-rpm

WORKDIR /work
ENTRYPOINT ["/usr/local/bin/build-rpm"]
