# This stage in the Dockerfile will not be included in the final image, as it
# is only being used to build the statsite binary.
FROM alpine as builder

ARG STATSITE_SHA_HASH

RUN apk add --update build-base libtool automake autoconf python3 py3-requests scons git check-dev linux-headers

WORKDIR /build
RUN git clone https://github.com/statsite/statsite
WORKDIR /build/statsite
RUN git checkout -b build-tmp $STATSITE_SHA_HASH
RUN ./autogen.sh
RUN bash ./configure
RUN make
RUN make install

# ----------------------------------------------------------------------------
## Go build stage  for building plumbago and dockerize
FROM golang:1.14.2 as gobuild

ENV DOCKERIZE_VERSION v0.6.1
RUN \
	wget -O dockerize.tar.gz https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
    md5sum dockerize.tar.gz && \
    [ "$(md5sum dockerize.tar.gz)" = "5c1b3d9a7d9341e23e0b8bdf2a288387  dockerize.tar.gz" ] && \
    tar -C /go/bin -xzvf dockerize.tar.gz && \
    rm dockerize.tar.gz

WORKDIR /go/src/code.justin.tv/edge
COPY plumbago ./plumbago
COPY build-plumbago.sh ./plumbago/
WORKDIR /go/src/code.justin.tv/edge/plumbago
RUN ./build-plumbago.sh


# ----------------------------------------------------------------------------

# This stage is what will be distributed. By pulling the compiled binary from
# the previous stage, we lessen image bloat and installed package set.
FROM alpine

RUN apk add ca-certificates
COPY --from=builder /usr/local/bin/statsite /usr/local/bin/statsite
COPY --from=builder /usr/local/share/statsite/ /usr/local/share/statsite/
COPY --from=gobuild /go/bin/plumbago /go/bin/dockerize /bin/
COPY ./statsite.conf.tmpl /config/statsite.conf.tmpl
CMD ["/bin/dockerize", "-template", "/config/statsite.conf.tmpl:/config/statsite.conf", "/usr/local/bin/statsite", "-f", "/config/statsite.conf"]
