FROM alpine as chicken

ENV CHICKEN_VERSION 4.11.0
ENV PLATFORM linux

RUN set -eux; \
    apk update; \
    apk --no-cache --update add build-base; \
    wget -qO- https://code.call-cc.org/releases/${CHICKEN_VERSION}/chicken-${CHICKEN_VERSION}.tar.gz | tar xzv; \
    cd /chicken-${CHICKEN_VERSION}; \
    make PLATFORM=${PLATFORM}; \
    make PLATFORM=${PLATFORM} install; \
    make PLATFORM=${PLATFORM} check; \
    cd /; \
    rm -rf /chicken-${CHICKEN_VERSION}

FROM chicken as deps
RUN chicken-install http-session srfi-69 coops uri-common srfi-18 medea numbers spiffy spiffy-cookies sql-de-lite crypt intarweb sxml-transforms websockets miscmacros

FROM deps as deps2
RUN chicken-install -r pll; \
    cd pll; \
    sed -i '1s/^/(import scheme)\n/' amb.scm; \
    chicken-install

FROM deps2 as compat
RUN apk --no-cache --update add libc6-compat

FROM node:16 as node
WORKDIR /farm
# COPY ./ /farm
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install

FROM node as buildfe
WORKDIR /farm
COPY ./ /farm
RUN make prodfe

FROM compat as buildfarm
WORKDIR /farm
COPY ./ /farm
RUN make farm

FROM buildfarm as farm
WORKDIR /farm
RUN mkdir dist
COPY --from=buildfe /farm/dist /farm/dist
RUN cp src/server/farm dist/; \
    chmod +x dist/farm

FROM farm as run
WORKDIR /farm/dist
ENTRYPOINT ["./farm"]
CMD ["-:a50"]
# CMD ./farm

