FROM mediawiki:latest AS base
ENV DEBIAN_FRONTEND="noninteractive"

COPY ./config.json /tmp/config.json
COPY ./build /tmp

RUN apt update && apt install --yes wget gpg \
    && wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg \
    && echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | tee /etc/apt/sources.list.d/elastic-8.x.list \
    && apt update && apt install --yes elasticsearch

# download extensions
RUN apt install wget \
  && /tmp/scripts/mediawiki-ext-search.sh AdvancedSearch    REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh CirrusSearch      REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh DynamicSidebar    REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh Elastica          REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh MobileFrontend    REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh Mpdf              REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh Popups            REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh TimedMediaHandler REL1_41 | xargs wget -P /tmp/mediawiki_extensions \
  && /tmp/scripts/mediawiki-ext-search.sh UserFunctions     REL1_41 | xargs wget -P /tmp/mediawiki_extensions

RUN find /tmp/mediawiki_extensions -name "*.tar.gz" -exec sh -c 'tar -xzf {} -C /var/www/html/extensions' \; \
    && find /tmp/mediawiki_skins -name "*.tar.gz" -exec sh -c 'tar -xzf {} -C /var/www/html/skins' \; \
    && find /tmp/mediawiki_override -type f -exec php /tmp/scripts/config_placeholder_replace.php "/tmp/config.json" "{}" \; \
    && cp -R /tmp/mediawiki_override/* /var/www/html \
    && chown www-data:www-data /var/www/html -R \
    && /tmp/scripts/mediawiki-ext-patch.sh \
    && rm -R /tmp/*
