# ============================================================
# Этап 1: Сборка
# ============================================================
FROM erlang:28-alpine AS builder

RUN apk add --no-cache \
    git curl make gcc musl-dev \
    rust cargo openssl-dev libsodium-dev

WORKDIR /app
COPY rebar.config ./
RUN rebar3 lock || true
RUN rebar3 get-deps

COPY include/ include/
COPY src/ src/

# Копируем sys.config из src/config/ в config/
COPY src/config/sys.config ./config/sys.config
COPY src/config/vm.args ./config/vm.args

RUN rebar3 as prod release
RUN rebar3 as prod tar
RUN mkdir -p /app/release && \
    tar -xzf _build/prod/rel/eventhub/eventhub-*.tar.gz -C /app/release

# ============================================================
# Этап 2: Финальный образ
# ============================================================
FROM alpine:3.20

RUN apk add --no-cache openssl libstdc++ libgcc ncurses-libs libsodium
COPY --from=builder /app/release /app
COPY --from=builder /usr/lib/libcrypto.* /usr/lib/
COPY --from=builder /usr/lib/libssl.* /usr/lib/

RUN mkdir -p /app/data && chmod 777 /app/data

WORKDIR /app
EXPOSE 8080 8081 8445 8446

ENV PATH="/app/erts-16.3.1/bin:$PATH"

ENV RELX_REPLACE_OS_VARS=true

CMD /app/bin/eventhub foreground
# COPY docker/entrypoint.sh /app/entrypoint.sh
# RUN chmod +x /app/entrypoint.sh
#
# ENTRYPOINT ["/app/entrypoint.sh"]