FROM rust:1.82 AS builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    gettext \
    postgresql-client \
    libpq-dev \
    git \
    curl \
    gcc \
    make \
    openssl \
    libssl-dev \
    clang

RUN ln -s /usr/bin/python3 /usr/bin/python & \
    ln -s /usr/bin/pip3 /usr/bin/pip

WORKDIR /scratch
COPY script/wasm-deps.sh .
RUN chmod a+x ./wasm-deps.sh && sleep 1 && ./wasm-deps.sh

WORKDIR /app

COPY . .
RUN echo "=== TOOLCHAIN ===" && \
    rustc --version && \
    cargo --version && \
    rustup show && \
    echo "=== TIME-CORE IN LOCK ===" && \
    grep -A5 -B2 'name = "time-core"' Cargo.lock

#RUN cargo install wasm-pack
RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
RUN chmod a+x ./script/plume-front.sh && sleep 1 && ./script/plume-front.sh

RUN cargo install --locked --path ./ --force --no-default-features --features postgres
RUN cargo install --locked --path plume-cli --force --no-default-features --features postgres
#RUN cargo install --path ./ --force --no-default-features --features postgres
#RUN cargo install --path plume-cli --force --no-default-features --features postgres
RUN cargo clean

FROM debian:stable-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    libpq5

WORKDIR /app

COPY --from=builder /app /app
COPY --from=builder /usr/local/cargo/bin/plm /bin/
COPY --from=builder /usr/local/cargo/bin/plume /bin/

CMD ["plume"]

EXPOSE 7878
