# Build context: docker/asterisk/ (see docker-compose.yml)
#
# NOTE: This compiles Asterisk from source, which takes several minutes.
# It has NOT been build-tested in the environment that produced this file —
# asterisk.org's download domain isn't reachable from there. Build this on
# your actual VPS and report back any errors; this mirrors the manual
# install steps we walked through and verified conceptually earlier, but the
# container-specific packaging (this Dockerfile) needs a real build to confirm.

FROM debian:bookworm-slim

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential wget ca-certificates subversion \
        libxml2-dev libncurses5-dev libsqlite3-dev libssl-dev \
        libjansson-dev libedit-dev libcurl4-openssl-dev \
        uuid-dev pkg-config unixodbc unixodbc-dev odbc-mariadb \
        default-libmysqlclient-dev libasound2-dev openssl \
        php-cli php-mysql \
        inotify-tools gettext-base \
        default-mysql-client \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src
RUN wget -q https://downloads.asterisk.org/pub/telephony/asterisk/asterisk-20-current.tar.gz \
    && tar xzf asterisk-20-current.tar.gz \
    && rm asterisk-20-current.tar.gz \
    && mv asterisk-20*/ asterisk-src

WORKDIR /usr/src/asterisk-src
RUN contrib/scripts/install_prereq install || true
RUN ./configure --with-jansson-bundled

# menuselect/menuselect doesn't exist until this target builds it (and
# generates the default menuselect.makeopts file) — running the binary
# directly before this step fails with "not found".
RUN make menuselect.makeopts

# Enable res_config_odbc (PJSIP realtime via ODBC — far more stable under
# concurrent load than res_config_mysql, which has a long-documented history
# of crashes in libmariadb/libmysqlclient when many realtime queries fire
# simultaneously, e.g. during PJSIP's startup config load) and PJSIP itself,
# without the interactive menuselect UI — this uses menuselect's command-line
# mode to flip specific categories on. If a future Asterisk version renames
# these targets, the build will fail loudly here rather than silently missing
# a module.
RUN menuselect/menuselect --enable res_config_odbc --enable res_odbc --enable app_voicemail \
        --enable res_pjsip --enable res_pjsip_session --enable chan_pjsip \
        menuselect.makeopts

RUN make -j1 third-party
RUN make -j"$(nproc)" && make install && make samples && make config && ldconfig

RUN groupadd -r asterisk && useradd -r -g asterisk -d /var/lib/asterisk asterisk \
    && chown -R asterisk:asterisk /etc/asterisk /var/lib/asterisk /var/log/asterisk /var/spool/asterisk /usr/lib/asterisk

COPY conf/ /etc/asterisk-templates/
COPY scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
COPY scripts/voicemail-reload-watcher.sh /usr/local/bin/voicemail-reload-watcher.sh
RUN chmod +x /usr/local/bin/entrypoint.sh /usr/local/bin/voicemail-reload-watcher.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["asterisk", "-f"]
