FROM docker.io/haskell:slim-buster as build

RUN apt-get update && apt-get install -y \
  ca-certificates

RUN mkdir /opt/build

WORKDIR /opt/build

COPY ./custom/src .

# update package index
RUN cabal update
# install library for aeson and utf-8-string
RUN cabal install --lib aeson
RUN cabal install --lib utf8-string

# compile Main.hs with shared/dynamic libraries
RUN ghc --make -dynamic Main.hs

RUN mkdir /opt/lib
# copy necessary libraries into /opt/lib
RUN find /opt/ghc/9.4.5/lib/ghc-9.4.5/lib/x86_64-linux-ghc-9.4.5/ -maxdepth 1 -name *.so* -exec cp '{}' /opt/lib/ \;
RUN find /root/.cabal/store/ghc-9.4.5/ -name *.so* -exec cp '{}' /opt/lib/ \;

FROM docker.io/ubuntu:23.10
RUN mkdir -p /opt/myapp

# add build-essentials useful for compiling python modules during pip install
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates libgmp-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/* # Clean up to keep the image size as small as possible
# add fakechroot to allow decoupling from host operating system without root permission
RUN apt-get update && apt-get install -y --no-install-recommends fakechroot \
    && apt-get clean && rm -rf /var/lib/apt/lists/* # Clean up to keep the image size as small as possible

COPY --from=build /opt/lib /opt/lib
COPY --from=build /opt/build /opt/myapp

CMD ["./bin/bash"]
