FROM --platform=$BUILDPLATFORM golang:bookworm AS builder

WORKDIR /app

COPY ../go.mod ../go.sum ./
RUN go mod download

# Copy source files
COPY . ./

# Build
ARG TARGETOS TARGETARCH
RUN CGO_ENABLED=0 GOGC=75 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -tags glibc -ldflags "-w -s" -o /agent ./internal/cmd/agent

# --------------------------
# Smartmontools builder stage
# --------------------------
FROM nvidia/cuda:12.2.2-base-ubuntu22.04 AS smartmontools-builder

RUN apt-get update && apt-get install -y \
  wget \
  build-essential \
  && wget https://downloads.sourceforge.net/project/smartmontools/smartmontools/7.5/smartmontools-7.5.tar.gz \
  && tar zxvf smartmontools-7.5.tar.gz \
  && cd smartmontools-7.5 \
  && ./configure --prefix=/usr --sysconfdir=/etc \
  && make \
  && make install \
  && rm -rf /smartmontools-7.5* \
  && apt-get remove -y wget build-essential \
  && apt-get autoremove -y \
  && rm -rf /var/lib/apt/lists/*

# --------------------------
# Final image: GPU-enabled agent with nvidia-smi
# --------------------------
FROM nvidia/cuda:12.2.2-base-ubuntu22.04
COPY --from=builder /agent /agent

# Copy smartmontools binaries and config files
COPY --from=smartmontools-builder /usr/sbin/smartctl /usr/sbin/smartctl

# Ensure data persistence across container recreations
VOLUME ["/var/lib/beszel-agent"]

ENTRYPOINT ["/agent"]
