FROM ubuntu:16.04

LABEL maintainer="Reuben N. S. Rowe (r.n.s.rowe@kent.ac.uk)"

ARG DEBIAN_FRONTEND=noninteractive

ENV TERM linux

RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get update && apt-get install -y --no-install-recommends \
  camlp4-extra \
  git \
  less \
  libpcre3-dev \
  m4 \
  ocaml \
  ocaml-native-compilers \
  opam \
  patchutils \
  pkg-config \
  rlwrap \
  rsync \
  software-properties-common \
  vim-tiny \
&& rm -rf /var/lib/apt/lists/*

RUN apt-get update && apt-get install -y --no-install-recommends curl && \
  (curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash) && \
  apt-get update && \
  apt-get install -y --no-install-recommends git-lfs && \
  apt-get remove -y curl && \
  rm -rf /var/lib/apt/lists/*

RUN useradd -m rotor

USER rotor

RUN opam init --auto-setup --comp 4.04.0

WORKDIR /home/rotor

COPY --chown=rotor refactorer/ refactorer/

RUN cd refactorer && \
  eval `opam config env` && \
  opam install -y coq && \
  opam pin add rotor . && \
  make

RUN opam switch rotor-testbed --alias-of 4.04.0 && \
  opam install -y jbuilder.1.0+beta11 ocaml-migrate-parsetree octavius re

WORKDIR /home/rotor

RUN eval `opam config env`; \
  mkdir testbeds && \
  mkdir testbeds/jane-street && \
  tar -xzf ./refactorer/test/jane-street/js-testbed.tar.gz -C testbeds/jane-street && \
  cd testbeds/jane-street && \
  jbuilder build

RUN opam switch 4.04.0

WORKDIR /home/rotor/testbeds

RUN eval `opam config env`; \
  git clone https://github.com/ocaml/ocaml.git && \
  cd ocaml && \
  git checkout 4.04.0 && \
  ./configure --prefix /home/rotor/ocaml && \
  make world.opt

ENV JANE_STREET_PATH /home/rotor/testbeds/jane-street

WORKDIR /home/rotor

COPY --chown=rotor proofs/ coq-formalisation/
COPY --chown=rotor testbed-configs/ testbeds/.configs/
COPY --chown=rotor scripts/ scripts/

ENTRYPOINT ["/bin/bash", "-l"]
