FROM debian:bookworm-slim
LABEL maintainer="docker@ao-it.net"

## prepare requirements
RUN ["bash", "-exo", "pipefail", "-c", "\
    export DEBIAN_FRONTEND=noninteractive ; \
    apt update ; \
    apt install -y wget gnupg python3 python3-mysqldb; \
    wget -O - https://packages.icinga.com/icinga.key | \
    gpg --dearmor -o /usr/share/keyrings/icinga-archive-keyring.gpg ; \
    source /etc/os-release ; \
    echo \"deb [signed-by=/usr/share/keyrings/icinga-archive-keyring.gpg] https://packages.icinga.com/debian icinga-${VERSION_CODENAME} main\" > /etc/apt/sources.list.d/${VERSION_CODENAME}-icinga.list ; \
    echo \"deb-src [signed-by=/usr/share/keyrings/icinga-archive-keyring.gpg] https://packages.icinga.com/debian icinga-${VERSION_CODENAME} main\" >> /etc/apt/sources.list.d/${VERSION_CODENAME}-icinga.list ; \
    apt clean all ; \
    rm -vrf /var/lib/apt/lists/* "]

## install webserver
RUN ["bash", "-exo", "pipefail", "-c", "\
    export DEBIAN_FRONTEND=noninteractive ; \
    apt update ; \
    apt install -y  --no-install-{recommends,suggests} apache2 ca-certificates libapache2-mod-php8.2 libldap-common locales-all php-{imagick,redis} php8.2-{bcmath,bz2,common,curl,dba,enchant,gd,gmp,imap,interbase,intl,ldap,mbstring,mysql,odbc,opcache,pgsql,pspell,readline,snmp,soap,sqlite3,sybase,tidy,xml,xmlrpc,xsl,zip} ; \
    apt clean all ; \
    rm -vrf /var/lib/apt/lists/* "]


## install icingaweb2 
RUN ["bash", "-exo", "pipefail", "-c", "\
    export DEBIAN_FRONTEND=noninteractive ; \
    apt update ; \
    apt install -y icingaweb2 icingadb-web icingacli ; \
    ln -vs /usr/share/icingaweb2/bin/icingacli /usr/local/bin/ ; \
    rm -f /etc/apache2/conf-enabled/icingaweb2.conf ; \
    icingacli setup config webserver apache --path=/ --file=/etc/apache2/conf-enabled/icingaweb2.conf ; \
    echo 'SetEnvIf X-REMOTE-USER \"(.*)\" REMOTE_USER=$0' > /etc/apache2/conf-enabled/allow-remote-user.conf ; \
    apt clean all ; \
    rm -vrf /var/lib/apt/lists/* "]

## modules
ARG ICINGAWEB_MODULEPATH="/usr/share/icingaweb2/modules"

### install module grafana
ARG MODULE_VERSION="3.1.1"
ARG REPO_URL="https://github.com/NETWAYS/icingaweb2-module-grafana"
ARG TARGET_DIR="${ICINGAWEB_MODULEPATH}/grafana"
ARG URL="${REPO_URL}/archive/v${MODULE_VERSION}.tar.gz"
RUN ["bash", "-exo", "pipefail", "-c", "\
    install -d -m 0755 ${TARGET_DIR} ; \
    wget -q -O - $URL | tar xfz - -C ${TARGET_DIR} --strip-components 1 ; \
"]

### install module for oidc
ARG MODULE_VERSION="0.7.3"
ARG REPO_URL="https://github.com/RISE-GmbH/icingaweb2-module-oidc"
ARG TARGET_DIR="${ICINGAWEB_MODULEPATH}/oidc"
ARG URL="${REPO_URL}/archive/v${MODULE_VERSION}.tar.gz"
RUN ["bash", "-exo", "pipefail", "-c", "\
    install -d -m 0755 ${TARGET_DIR} ; \
    wget -q -O - $URL | tar xfz - -C ${TARGET_DIR} --strip-components 1 ; \
"]

## create persistend data store
RUN ["bash", "-exo", "pipefail", "-c", "\
    mkdir -p /data ; \
    mkdir -p /data-init/etc/ ; \
    mkdir -p /data-init/usr/ ; \
    mv /etc/icingaweb2 /data-init/etc/ ; \
    mv /usr/share/icingaweb2 /data-init/usr/ ; \
    ln -vs /data/etc/icingaweb2 /etc/icingaweb2 ; \
    ln -vs /data/usr/icingaweb2 /usr/share/icingaweb2 ; \
"]

## redirect log
RUN ["bash", "-exo", "pipefail", "-c", "\
    ln -vsf /dev/stdout /var/log/apache2/access.log ; \
    ln -vsf /dev/stderr /var/log/apache2/error.log ; \
    ln -vsf /dev/stdout /var/log/apache2/other_vhosts_access.log "]

EXPOSE 8080

COPY init.sh /root/init.sh
COPY conf_icingaweb2.py /root/conf_icingaweb2.py

VOLUME ["/data"]
WORKDIR /data
USER root
ENTRYPOINT ["/bin/bash", "/root/init.sh"]
CMD ["/usr/sbin/apache2", "-DFOREGROUND"]
