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

# =============================================================================
# Stage 1: Builder — compilation du binaire Go sur Rocky Linux 9
# Rocky Linux 9 comme base builder assure la compatibilité binaire sur toutes
# les distros cibles (el8/el9/el10 sont ABI-compatibles pour les libs system).
# =============================================================================
FROM rockylinux:9 AS builder

WORKDIR /build

RUN dnf install -y epel-release && \
    dnf config-manager --set-enabled crb && \
    dnf install -y golang git libpcap-devel gcc make && \
    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/sentinel
RUN go mod download || true

COPY services/sentinel/ /build/services/sentinel/

ARG VERSION=dev
ARG BUILD_TIME=""
ARG GIT_COMMIT=""

RUN mkdir -p dist && \
    CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \
    go build -buildvcs=false \
    -ldflags "-X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME} -X main.GitCommit=${GIT_COMMIT}" \
    -o dist/sentinel \
    ./cmd/ja4sentinel

# =============================================================================
# Stage 2: rpm-builder — construction des RPMs avec rpmbuild
# Un seul stage, trois appels rpmbuild successifs (el8, el9, el10).
# =============================================================================
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,RPMS,SOURCES,SPECS,SRPMS} && \
    mkdir -p /root/rpmbuild/SOURCES/logrotate && \
    mkdir -p /packages/rpm/{el8,el9,el10}

# Spec et fichiers sources
COPY services/sentinel/packaging/rpm/ja4sentinel.spec /root/rpmbuild/SPECS/ja4sentinel.spec
COPY --from=builder /build/services/sentinel/dist/sentinel /root/rpmbuild/SOURCES/ja4sentinel
COPY services/sentinel/packaging/systemd/ja4sentinel.service /root/rpmbuild/SOURCES/ja4sentinel.service
COPY services/sentinel/packaging/logrotate/ja4sentinel /root/rpmbuild/SOURCES/logrotate/ja4sentinel
COPY services/sentinel/config.yml.example /root/rpmbuild/SOURCES/config.yml

RUN chmod 755 /root/rpmbuild/SOURCES/ja4sentinel && \
    chmod 644 /root/rpmbuild/SOURCES/ja4sentinel.service && \
    chmod 644 /root/rpmbuild/SOURCES/logrotate/ja4sentinel && \
    chmod 640 /root/rpmbuild/SOURCES/config.yml

# el8
RUN rpmbuild --define "_topdir /root/rpmbuild" \
             --define "dist .el8" \
             --define "build_version ${VERSION}" \
             --target x86_64 \
             -bb /root/rpmbuild/SPECS/ja4sentinel.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 "build_version ${VERSION}" \
             --target x86_64 \
             -bb /root/rpmbuild/SPECS/ja4sentinel.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 "build_version ${VERSION}" \
             --target x86_64 \
             -bb /root/rpmbuild/SPECS/ja4sentinel.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/"]
