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

SHELL ["/bin/ash", "-c"]

RUN apk add --no-cache libcap-static libpcap-dev linux-headers git go file dpkg rpm findutils coreutils grep rpm-dev --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing/

ENV REPO="github.com/mosajjal/dnsmonster"
RUN git clone https://${REPO}.git /opt/dnsmonster \
    && cd /opt/dnsmonster \
    && go build --ldflags "-L /usr/lib/libcap.a -linkmode external -extldflags \"-static\"" -o /opt/dnsmonster-linux-amd64.bin ./cmd/dnsmonster

RUN ls -lha /opt/dnsmonster-linux-amd64.bin

ENV CGO_ENABLED=1
ENV GOOS=windows
ENV GOARCH=amd64
RUN cd /opt/dnsmonster \
    && go build -o /opt/dnsmonster-windows-amd64.exe ./cmd/dnsmonster

WORKDIR /opt/dnsmonster

# build Deb file
RUN cd /opt/dnsmonster \
    && export LATEST_TAG=`git describe --tags $(git rev-list --tags --max-count=1)` \
    && echo ${LATEST_TAG} \
    && export DIR="/tmp/dnsmonster_${LATEST_TAG:1}-1_amd64" \
    && mkdir -p $DIR/sbin $DIR/etc/dnsmonster $DIR/usr/share/man/man7 $DIR/DEBIAN $DIR/etc/bash_completion.d $DIR/usr/share/fish/vendor_completions.d \
    && cp /opt/dnsmonster-linux-amd64.bin $DIR/sbin/dnsmonster \
    && $DIR/sbin/dnsmonster --manPage > $DIR/usr/share/man/man7/dnsmonster.7 \
    && $DIR/sbin/dnsmonster --bashCompletion > $DIR/etc/bash_completion.d/dnsmonster.bash \
    && $DIR/sbin/dnsmonster --fishCompletion > $DIR/usr/share/fish/vendor_completions.d/dnsmonster.fish \
    && $DIR/sbin/dnsmonster --writeConfig $DIR/etc/dnsmonster/dnsmonster.ini \
    && echo -e  "Package: dnsmonster\nVersion: ${LATEST_TAG:1}\nArchitecture: amd64\nMaintainer: Ali Mosajjal <hi@n0p.me>\nDescription: Passive DNS monitoring framework." > $DIR/DEBIAN/control \
    && dpkg-deb --build --root-owner-group $DIR \
    && mv /tmp/dnsmonster_*_amd64.deb /tmp/dnsmonster-latest.deb

# build rpm file and move it to /tmp/dnsmonster-latest.rpm
RUN <<EOF cat > /tmp/dnsmonster.spec
Name:       dnsmonster
Version:    envLATEST_TAG
Release:    1
Summary:    Passive DNS monitoring framework
License:    GPLv2
BuildArch:  x86_64

%description
Passive DNS monitoring framework, Github: github.com/mosajjal/dnsmonster

%prep
# we have no source, so nothing here

%build
# binary is already built

%install
mkdir -p %{buildroot}/sbin
mkdir -p %{buildroot}/etc/dnsmonster
mkdir -p %{buildroot}/usr/share/man/man7
mkdir -p %{buildroot}/etc/bash_completion.d
mkdir -p %{buildroot}/usr/share/fish/vendor_completions.d

cp /opt/dnsmonster-linux-amd64.bin %{buildroot}/sbin/dnsmonster
%{buildroot}/sbin/dnsmonster --manPage | gzip > %{buildroot}/usr/share/man/man7/dnsmonster.7.gz
%{buildroot}/sbin/dnsmonster --bashCompletion > %{buildroot}/etc/bash_completion.d/dnsmonster.bash
%{buildroot}/sbin/dnsmonster --fishCompletion > %{buildroot}/usr/share/fish/vendor_completions.d/dnsmonster.fish
%{buildroot}/sbin/dnsmonster --writeConfig %{buildroot}/etc/dnsmonster/dnsmonster.ini

%files
/sbin/dnsmonster
/usr/share/man/man7/dnsmonster.7.gz
/etc/bash_completion.d/dnsmonster.bash
/usr/share/fish/vendor_completions.d/dnsmonster.fish
/etc/dnsmonster/dnsmonster.ini

EOF

RUN export LATEST_TAG=`git describe --tags $(git rev-list --tags --max-count=1) | awk -F- '{print $1}'` && \
    sed -i "s/envLATEST_TAG/${LATEST_TAG}/" /tmp/dnsmonster.spec && \
    rpmbuild -ba --define "_rpmdir /tmp" --define "_builddir /tmp" /tmp/dnsmonster.spec && \
    mv /tmp/x86_64/dnsmonster-*.x86_64.rpm /tmp/dnsmonster-latest.rpm
