FROM alpine:edge
LABEL maintainer="Ali Mosajjal <hi@n0p.me>"

RUN apk add --no-cache git go zig file --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing/

ARG PROJECT=dnsmonster
ARG GOTAG=nolibpcap
ARG GOLDFLAGS='-s -w'
# OUTDIR is also used in the second layer for copying the folder.
ARG OUTDIR="/tmp/bins"
RUN mkdir /${PROJECT} && mkdir ${OUTDIR}
COPY . /${PROJECT}
WORKDIR /${PROJECT}

ENV GOBUILD="go build -ldflags=-s -tags ${GOTAG} -o ${OUTDIR}/${PROJECT}_${GOTAG}"
ENV BASEDIR="./cmd/dnsmonster"

# Linux
RUN echo "Building Linux binaries"
RUN export GOOS=linux && export CGO_ENABLED=1 && \
    export GOARCH=amd64 && CC="zig cc -target x86_64-${GOOS}-musl" ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm && CC="zig cc -target arm-${GOOS}-musleabihf" ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm64 && CC="zig cc -target aarch64-${GOOS}-musl" ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=riscv64 && CC="zig cc -target riscv64-${GOOS}-musl" ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR}
# MIPS and PPC are pending patches from Zig #10979, #11829

# Windows
RUN echo "Building Windows binaries"
RUN export GOOS=windows && export CGO_ENABLED=1 && \
    export GOARCH=amd64 && CC="zig cc -target x86_64-${GOOS}-gnu" ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm && CC="zig cc -target arm-${GOOS}-gnu" ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm64 && CC="zig cc -target aarch64-${GOOS}-gnu" ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR}

# Darwin
RUN echo "Building Darwin binaries"
RUN export GOOS=darwin && export CGO_ENABLED=0 && \
    export GOARCH=amd64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR}

# FreeBSD
RUN echo "Building FreeBSD binaries"
RUN export GOOS=freebsd && export CGO_ENABLED=0 && \
    export GOARCH=arm64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=amd64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \

    # NetBSD
    RUN echo "Building NetBSD binaries"
RUN export GOOS=netbsd && export CGO_ENABLED=0 && \
    export GOARCH=arm64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=amd64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \

    # OpenBSD
    RUN echo "Building OpenBSD binaries"
RUN export GOOS=openbsd && export CGO_ENABLED=0 && \
    export GOARCH=arm64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=amd64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \
    export GOARCH=arm && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR} && \

    # Android
    RUN echo "Building Android binaries"
RUN export GOOS=android && export CGO_ENABLED=0 && \
    export GOARCH=arm64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR}

# DragonflyBSD
RUN echo "Building Dragonfly binaries"
RUN export GOOS=dragonfly && export  CGO_ENABLED=0 && \
    export GOARCH=amd64 && ${GOBUILD}_${GOOS}_${GOARCH}.bin ${BASEDIR}

FROM scratch
COPY --from=0 /tmp/bins/*.bin  /
ENTRYPOINT ["/dnsmonster"]
