# 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 \
        default-libmysqlclient-dev \
        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

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

# Enable res_config_mysql (PJSIP realtime) and PJSIP 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_mysql --enable app_voicemail \
        --enable res_pjsip --enable res_pjsip_session --enable chan_pjsip \
        menuselect.makeopts

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"]
