# Use: docker build . --pull --no-cache --shm-size=196m -t darkfi:alpine -f ./contrib/docker/Dockerfile.alpine --build-arg DONT_EXEC_TESTS=1
#   optionally with: --build-arg ALPINE_VER=3.18 --build-arg RUST_VER=nightly

ARG RUST_VER=nightly-2025-04-10  # 1.72 nightly stable
ARG ALPINE_VER=3.18              # 3.17

FROM alpine:${ALPINE_VER} as rust_builder

ARG RUST_VER
ARG ALPINE_VER

RUN apk update

RUN apk add cmake jq wget clang curl gcc make llvm-dev \
  openssl-dev expat-dev freetype-dev libudev-zero-dev \
  libgudev-dev pkgconf clang-dev fontconfig-dev bash \
  ttf-opensans musl-dev alsa-lib-dev python3-dev perl sqlite-dev

RUN curl https://sh.rustup.rs -sSf | bash -s -- -y --default-toolchain "${RUST_VER}" --default-host $(uname -m)-unknown-linux-musl

ENV PATH="/root/.cargo/bin:${PATH}"

RUN rustup target add wasm32-unknown-unknown

RUN cargo search whatever  # creates .cargo cache

# 2. stage
FROM rust_builder as builder

ARG DONT_EXEC_TESTS
ARG RUST_VER

WORKDIR /opt/darkfi

COPY . ./

ARG RUSTFLAGS="-C target-feature=-crt-static"

RUN cargo update

RUN make clean

RUN rm -rf ./target/ zkas proof/*.bin

RUN bash -c "if [ -z \"$DONT_EXEC_TESTS\" ]; then make -j test; fi"

RUN bash -c 'make -j all'

# 3. stage
FROM alpine:${ALPINE_VER}

RUN apk add --no-cache openssl ttf-opensans libgcc alsa-lib sqlite-libs

WORKDIR /opt/darkfi


COPY --from=builder /opt/darkfi/drk /opt/darkfi/darkfid  \
   /opt/darkfi/darkirc /opt/darkfi/faucetd \
   /opt/darkfi/zkas /opt/darkfi/vanityaddr \
   /opt/darkfi/tau /opt/darkfi/taud   ./
