FROM debian:bookworm-20250428-slim AS builder
RUN apt-get update \
    && apt-get install -y \
        autoconf \
        automake \
        build-essential \
        curl \
        git \
        libtool \
        pkg-config \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /opt/src
RUN git config --global http.version HTTP/1.1 \
    && git config --global http.postBuffer 524288000 \
    && git config --global http.lowSpeedLimit 0 \
    && git config --global http.lowSpeedTime 999999 \
    && git clone https://github.com/openvenues/libpostal.git \
    && cd libpostal \
    && ./bootstrap.sh \
    && ./configure --datadir=/usr/local/share \
    && make -j4 \
    && make install

FROM hexpm/elixir:1.18.4-erlang-27.3.4-debian-bookworm-20250520-slim AS elixir
RUN apt-get update \
    && apt-get install -y \
        build-essential \
    && rm -rf /var/lib/apt/lists/* \
    && mix do local.rebar, local.hex
COPY --from=builder /usr/local/include/libpostal /usr/local/include/libpostal
COPY --from=builder /usr/local/lib/libpostal.a /usr/local/lib/
COPY --from=builder /usr/local/lib/libpostal.la /usr/local/lib/
COPY --from=builder /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/
COPY --from=builder /usr/local/lib/pkgconfig/libpostal.pc /usr/local/lib/pkgconfig/
COPY --from=builder /usr/local/share/libpostal /usr/local/share/libpostal
COPY --from=builder /usr/local/bin/libpostal_data /usr/local/bin/libpostal_data
RUN ln -s /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/libpostal.so.1 \
    && ln -s /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/libpostal.so \
    && ldconfig
WORKDIR /opt/src/app
VOLUME ["/opt/src/app/_build", "/opt/src/app/deps"]
COPY ./ex .
RUN mix do deps.get, deps.compile

FROM python:3.13.3-slim-bookworm AS python
ENV PATH /root/.local/bin:${PATH}
RUN apt-get update \
    && apt-get install -y \
        build-essential \
        curl \
    && rm -rf /var/lib/apt/lists/* \
    && curl -sSL https://install.python-poetry.org | python3 -
COPY --from=builder /usr/local/include/libpostal /usr/local/include/libpostal
COPY --from=builder /usr/local/lib/libpostal.a /usr/local/lib/
COPY --from=builder /usr/local/lib/libpostal.la /usr/local/lib/
COPY --from=builder /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/
COPY --from=builder /usr/local/lib/pkgconfig/libpostal.pc /usr/local/lib/pkgconfig/
COPY --from=builder /usr/local/share/libpostal /usr/local/share/libpostal
COPY --from=builder /usr/local/bin/libpostal_data /usr/local/bin/libpostal_data
RUN ln -s /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/libpostal.so.1 \
    && ln -s /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/libpostal.so \
    && ldconfig
WORKDIR /opt/src/app
COPY ./py/pyproject.toml .
COPY ./py/poetry.lock .
COPY ./py/README.md .
RUN poetry install
COPY ./py .

FROM golang:1.24.3-bookworm AS go
COPY --from=builder /usr/local/include/libpostal /usr/local/include/libpostal
COPY --from=builder /usr/local/lib/libpostal.a /usr/local/lib/
COPY --from=builder /usr/local/lib/libpostal.la /usr/local/lib/
COPY --from=builder /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/
COPY --from=builder /usr/local/lib/pkgconfig/libpostal.pc /usr/local/lib/pkgconfig/
COPY --from=builder /usr/local/share/libpostal /usr/local/share/libpostal
COPY --from=builder /usr/local/bin/libpostal_data /usr/local/bin/libpostal_data
RUN ln -s /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/libpostal.so.1 \
    && ln -s /usr/local/lib/libpostal.so.1.0.1 /usr/local/lib/libpostal.so \
    && ldconfig
WORKDIR /opt/src/config/data
COPY ./config/data/addresses.csv ./
WORKDIR /opt/src/app
COPY ./go .
RUN go mod download \
    && go build
