# syntax=docker.io/docker/dockerfile:1.3@sha256:42399d4635eddd7a9b8a24be879d2f9a930d0ed040a61324cfdf59ef1357b3b2
FROM cli-gen as cli-export
FROM node:18-alpine@sha256:44aaf1ccc80eaed6572a0f2ef7d6b5a2982d54481e4255480041ac92221e2f11 as const-build

# fetch scripts/guardian-set-init.sh deps
RUN apk update && apk add bash g++ make python3 curl jq findutils

# Support additional root CAs
COPY README.md cert.pem* /certs/
# Alpine
RUN if [ -e /certs/cert.pem ]; then cp /certs/cert.pem /etc/ssl/cert.pem; fi

# Copy and link CLI
COPY --from=cli-export clients/js /cli

WORKDIR /cli

RUN npm link

WORKDIR /

COPY scripts ./scripts
COPY ethereum/.env.test ./ethereum/.env.test

ARG num_guardians
ENV NUM_GUARDIANS=$num_guardians

# run distribute-devnet-consts.sh to copy devnet-consts.json to chain dirs for use
RUN ./scripts/distribute-devnet-consts.sh

# run guardian-set-init.sh to create env files with the init state for NUM_GUARDIANS
RUN ./scripts/guardian-set-init.sh $NUM_GUARDIANS

FROM scratch AS const-export
COPY --from=const-build /scripts/.env.0x .env.0x
COPY --from=const-build /scripts/.env.hex .env
COPY --from=const-build /scripts/devnet-consts.json devnet-consts.json