ARG ALPINE_VERSION=3.13.6
ARG BASE_IMAGE=alpine
ARG EMAIL=monit@sutty.nl
ARG EMAIL_FROM=monit@sutty.nl
FROM ${BASE_IMAGE}:${ALPINE_VERSION} as build
ARG EMAIL
ARG EMAIL_FROM

RUN apk add --no-cache tzdata

COPY ./monitrc /etc/monitrc
RUN chmod 600 /etc/monitrc
RUN sed -re "s/@@EMAIL@@/${EMAIL}/" -i /etc/monitrc
RUN sed -re "s/@@EMAIL_FROM@@/${EMAIL_FROM}/" -i /etc/monitrc
RUN monit -t

FROM ${BASE_IMAGE}:${ALPINE_VERSION}
ARG ALPINE_VERSION
MAINTAINER "f <f@sutty.nl>"

# Locale
ENV LANG="C.UTF-8"

# Sutty's repository
RUN echo ${ALPINE_VERSION} | cut -d . -f 1,2 | xargs -I {} echo https://alpine.sutty.nl/alpine/v{}/sutty | tee -a /etc/apk/repositories
RUN wget https://alpine.sutty.nl/alpine/sutty.pub -O /etc/apk/keys/alpine@sutty.nl-5ea884cd.rsa.pub

# Install monit and remove default config
RUN apk add --no-cache tini monit knsupdate
COPY --from=build /usr/share/zoneinfo/UTC /etc/localtime

# Create directories
RUN install -dm 2750 -o root -g root /etc/monit.d
RUN install -dm 2750 -o root -g root /var/lib/monit

# Install config
COPY --from=build /etc/monitrc /etc/monitrc
COPY ./nsupdate.sh /usr/local/bin/nsupdate
COPY ./whatsmyip6.sh /usr/local/bin/whatsmyip6

# Allow access to the web GUI
EXPOSE 2812

# Use tini as init
ENTRYPOINT ["/sbin/tini", "--"]

# Run monit, it will take care of running services afterwards
CMD ["/usr/bin/monit"]