# syntax=docker/dockerfile:1
# =============================================================================
# correlator — Dockerfile de packaging RPM (Rocky Linux 8/9, AlmaLinux 10)
# Build context: monorepo root (ja4-platform/)
# Méthode: 1 builder Rocky → 1 rpm-builder (rpmbuild, 3 × dist) → 1 output alpine
# =============================================================================

# =============================================================================
# Stage 1: Builder — compilation du binaire Go sur Rocky Linux 9
# CGO_ENABLED=0 → binaire statique, mais compilé sur la même distro cible
# =============================================================================
FROM rockylinux:9 AS builder

WORKDIR /build

RUN dnf install -y golang git && dnf clean all

# Copie du workspace Go et du module partagé en premier (meilleur cache)
COPY go.work go.work.sum* ./
COPY shared/go/ja4common/ ./shared/go/ja4common/
COPY services/sentinel/go.mod services/sentinel/go.sum* ./services/sentinel/
COPY services/correlator/go.mod services/correlator/go.sum* ./services/correlator/

WORKDIR /build/services/correlator
RUN go mod download

COPY services/correlator/ /build/services/correlator/

ARG VERSION=dev
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -ldflags="-w -s -X main.Version=${VERSION}" \
    -o /tmp/correlator \
    ./cmd/logcorrelator

# =============================================================================
# Stage 2: rpm-builder — construction des RPMs avec rpmbuild
# Un seul stage, trois appels rpmbuild successifs (el8, el9, el10).
# Le spec lit les fichiers depuis %{_builddir} (répertoire BUILD de rpmbuild).
# =============================================================================
FROM rockylinux:9 AS rpm-builder

WORKDIR /package

ARG VERSION=dev

RUN dnf install -y rpm-build rpmdevtools && dnf clean all

RUN mkdir -p /root/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} && \
    mkdir -p /packages/rpm/{el8,el9,el10}

# Disposition des fichiers dans BUILD/ (attendue par le spec correlator)
RUN mkdir -p /root/rpmbuild/BUILD/usr/bin \
             /root/rpmbuild/BUILD/etc/logcorrelator \
             /root/rpmbuild/BUILD/etc/systemd/system \
             /root/rpmbuild/BUILD/etc/logrotate.d

COPY --from=builder /tmp/correlator /root/rpmbuild/BUILD/usr/bin/logcorrelator
COPY services/correlator/config.example.yml /root/rpmbuild/BUILD/etc/logcorrelator/logcorrelator.yml
COPY services/correlator/config.example.yml /root/rpmbuild/BUILD/etc/logcorrelator/logcorrelator.yml.example
COPY services/correlator/logcorrelator.service /root/rpmbuild/BUILD/etc/systemd/system/logcorrelator.service
COPY services/correlator/packaging/rpm/logrotate /root/rpmbuild/BUILD/etc/logrotate.d/logcorrelator

RUN chmod 755 /root/rpmbuild/BUILD/usr/bin/logcorrelator && \
    chmod 640 /root/rpmbuild/BUILD/etc/logcorrelator/logcorrelator.yml && \
    chmod 640 /root/rpmbuild/BUILD/etc/logcorrelator/logcorrelator.yml.example && \
    chmod 644 /root/rpmbuild/BUILD/etc/systemd/system/logcorrelator.service && \
    chmod 644 /root/rpmbuild/BUILD/etc/logrotate.d/logcorrelator

COPY services/correlator/packaging/rpm/logcorrelator.spec /root/rpmbuild/SPECS/logcorrelator.spec

# el8
RUN rpmbuild --define "_topdir /root/rpmbuild" \
             --define "dist .el8" \
             --define "version ${VERSION}" \
             --target x86_64 \
             -bb /root/rpmbuild/SPECS/logcorrelator.spec && \
    cp /root/rpmbuild/RPMS/x86_64/*.el8.x86_64.rpm /packages/rpm/el8/

# el9
RUN rpmbuild --define "_topdir /root/rpmbuild" \
             --define "dist .el9" \
             --define "version ${VERSION}" \
             --target x86_64 \
             -bb /root/rpmbuild/SPECS/logcorrelator.spec && \
    cp /root/rpmbuild/RPMS/x86_64/*.el9.x86_64.rpm /packages/rpm/el9/

# el10
RUN rpmbuild --define "_topdir /root/rpmbuild" \
             --define "dist .el10" \
             --define "version ${VERSION}" \
             --target x86_64 \
             -bb /root/rpmbuild/SPECS/logcorrelator.spec && \
    cp /root/rpmbuild/RPMS/x86_64/*.el10.x86_64.rpm /packages/rpm/el10/

# =============================================================================
# Stage 3: output — image finale contenant uniquement les RPMs
# =============================================================================
FROM alpine:latest AS output

WORKDIR /packages
COPY --from=rpm-builder /packages/rpm/el8/*.rpm /packages/rpm/el8/
COPY --from=rpm-builder /packages/rpm/el9/*.rpm /packages/rpm/el9/
COPY --from=rpm-builder /packages/rpm/el10/*.rpm /packages/rpm/el10/

CMD ["sh", "-c", \
  "echo '=== RPM el8 ===' && ls -la /packages/rpm/el8/ && \
   echo '' && echo '=== RPM el9 ===' && ls -la /packages/rpm/el9/ && \
   echo '' && echo '=== RPM el10 ===' && ls -la /packages/rpm/el10/"]
