FROM debian:stretch-slim AS zip_downloader
LABEL maintainer="NebulousLabs <<EMAIL>>"

ARG SIA_VERSION="1.5.6"
ARG SIA_PACKAGE="Sia-v${SIA_VERSION}-linux-amd64"
ARG SIA_ZIP="${SIA_PACKAGE}.zip"
ARG SIA_RELEASE="https://sia.tech/releases/${SIA_ZIP}"

RUN apt-get update && \
    apt-get install -y wget unzip && \
    wget "$SIA_RELEASE" && \
    mkdir /sia && \
    unzip -j "$SIA_ZIP" "${SIA_PACKAGE}/siac" -d /sia && \
    unzip -j "$SIA_ZIP" "${SIA_PACKAGE}/siad" -d /sia

FROM alpine:3
LABEL maintainer="NebulousLabs <<EMAIL>>"

ARG SIA_DIR="/sia"
ARG SIA_DATA_DIR="/sia-data"
ARG SIAD_DATA_DIR="/sia-data"

# We need the mailcap package for the mime types support it provides.
RUN mkdir /lib64 && \
    ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 && \
    apk add mailcap logrotate

# Set up crond. We create a little shortcut to it, so we can work with crond the
# same way we work with cron in Debian.
# We also set up logrotate to run each hour instead of once per day.
RUN echo "/usr/sbin/crond" >> /usr/local/bin/cron && \
    chmod +x /usr/local/bin/cron && \
    mv /etc/periodic/daily/logrotate /etc/periodic/hourly

# Workaround for backwards compatibility with old images, which hardcoded the
# Sia data directory as /mnt/sia. Creates a symbolic link so that any previous
# path references stored in the Sia host config still work.
RUN ln -s "$SIA_DATA_DIR" /mnt/sia

WORKDIR "$SIA_DIR"

ENV SIA_DATA_DIR "$SIA_DATA_DIR"
ENV SIAD_DATA_DIR "$SIAD_DATA_DIR"
ENV SIA_MODULES gctwhr

COPY --from=zip_downloader /sia/siac /sia/siad /usr/bin/
COPY scripts/*.sh ./
COPY scripts/logrotate-sia /etc/logrotate.d/sia

EXPOSE 9980

ENTRYPOINT ["./run.sh"]
