FROM debian:stable

RUN apt-get -y update && apt-get -y install nginx php-fpm php-mysql php-apcu php-mbstring php-xml supervisor python-pip \
                                            mysql-client imagemagick curl wget zip unzip php-pclzip git msmtp msmtp-mta
RUN pip install supervisor-stdout

ENV MW_VERSION REL1_31
ENV MW_SOURCE https://releases.wikimedia.org/mediawiki/1.31/mediawiki-1.31.0.tar.gz
ENV SMF_VERSION 2.0.15
ENV SMF_SOURCE https://download.simplemachines.org/index.php/smf_2-0-15_install.tar.gz

ENV SOURCE_ROOT /application
ENV WIKI_SOURCE $SOURCE_ROOT/w
ENV FORUMS_SOURCE $SOURCE_ROOT/forums

RUN mkdir userdata && mkdir $SOURCE_ROOT && \
    mkdir $WIKI_SOURCE && mkdir $FORUMS_SOURCE

# Install composer
RUN cd /usr/src && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Clone mediawiki and SMF source into temp directory, install and then copy over to application dir
RUN cd /tmp && wget $MW_SOURCE && tar -xvf mediawiki-*.tar.gz && \
    cp -Rv mediawiki-*/* $WIKI_SOURCE && rm -rf mediawiki-* && \
    mkdir smf && cd smf && wget $SMF_SOURCE && tar -xvf smf_*.tar.gz && \
    rm -rf Settings.php attachments smf_*.tar.gz && cp -Rv * $FORUMS_SOURCE

RUN chown -R www-data:www-data $SOURCE_ROOT

RUN cd "$WIKI_SOURCE/maintenance" && \
    sh -c 'for TOOL in *.php; do LINK="/usr/bin/$(basename $TOOL .php)"; echo "#!/bin/sh\nphp $WIKI_SOURCE/maintenance/$TOOL \$@" > $LINK; chmod +x $LINK; done'

# Add our scripts
ADD scripts scripts

# Install additional extensions
# 12 August 2018: Nuke extension is not bundled with MediaWiki 1.31 by mistake.
# It will be added back in the next release.
RUN /scripts/add_extension.sh FlaggedRevs $MW_VERSION && \
    /scripts/add_extension.sh CheckUser $MW_VERSION && \
    /scripts/add_extension.sh Contributors $MW_VERSION && \
    /scripts/add_extension.sh Nuke $MW_VERSION && \
    /scripts/add_extension.sh Auth_remoteuser $MW_VERSION

# Add our stuff
ADD --chown=www-data:www-data application application
RUN cd $WIKI_SOURCE && composer update --no-dev

ADD configuration configuration

RUN ln -sf /configuration/supervisord.conf /etc/supervisor/supervisord.conf && \
    ln -sf /configuration/nginx.conf /etc/nginx/nginx.conf && \
    ln -sf /configuration/php-fpm.conf /etc/php/7.0/fpm/php-fpm.conf && \
    ln -sf /configuration/php-fpm-pool.conf /etc/php/7.0/fpm/pool.d/www.conf && \
    ln -sf /configuration/php.ini /etc/php/7.0/fpm/php.ini

EXPOSE 80

CMD /usr/bin/supervisord -n
