# COmanage Match Dockerfile
#
# Portions licensed to the University Corporation for Advanced Internet
# Development, Inc. ("UCAID") under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information
# regarding copyright ownership.
#
# UCAID licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM php:8.1.26-apache-bullseye

# Official PHP image with Apache HTTPD includes
# --with-openssl
# --with-mbstring
# but intl, pdo, pdo_pgsql, pgsql, 
# extensions must be built.
RUN apt-get update && apt-get install -y \
         libicu-dev \
         libpq-dev \
         patch \
         ssl-cert \
         wget \
         zlib1g \
         libpcre3-dev \
      && docker-php-ext-configure intl \
      && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
      && docker-php-ext-install intl pdo pdo_pgsql pgsql \
      && docker-php-source delete \
      && apt-get purge -y \
         libicu-dev \
         libpq-dev \
      && apt-get clean

ARG COMANAGE_MATCH_VERSION
ENV COMANAGE_MATCH_VERSION ${COMANAGE_MATCH_VERSION:-develop}
LABEL comanage_match_version=${COMANAGE_MATCH_VERSION}

ENV COMANAGE_MATCH_SRC_URL=https://github.internet2.edu/COmanage/match/archive/${COMANAGE_MATCH_VERSION}.tar.gz

ARG COMANAGE_MATCH_DIR
ENV COMANAGE_MATCH_DIR ${COMANAGE_MATCH_DIR:-/srv/comanage-match}
LABEL comanage_match_dir=${COMANAGE_MATCH_DIR}

WORKDIR $COMANAGE_MATCH_DIR

RUN mkdir -p ${COMANAGE_MATCH_DIR} \
      && wget -O comanage.tar.gz ${COMANAGE_MATCH_SRC_URL} \
      && tar -zxf comanage.tar.gz -C ${COMANAGE_MATCH_DIR} --strip-components=1 \
      && rm -f comanage.tar.gz \
      && cd /var/www/html \
      && ln -s ${COMANAGE_MATCH_DIR}/app/webroot match

RUN a2enmod headers \
      && a2enmod ssl \
      && a2enmod rewrite \
      && a2dissite 000-default.conf \
      && a2disconf other-vhosts-access-log \
      && cd /etc/apache2 \
      && ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem cert.pem \
      && ln -s /etc/ssl/private/ssl-cert-snakeoil.key privkey.pem

COPY apache-include-directory-match /etc/apache2/
COPY apache-include-virtual-host-port443-base /etc/apache2/
COPY apache-include-virtual-host-port80-redirect /etc/apache2/

COPY comanage_utils.sh /usr/local/lib/
COPY comanage_shibboleth_sp_utils.sh /usr/local/lib/
COPY docker-comanage-match-entrypoint /usr/local/bin/

# Patch to configure console logging. The patch is
# applied by the entry point script when appropriate.
#
# The patch is the output of the command
#
# diff -Naur app.php.original app.php > comanage_match_console_logging.patch
COPY comanage_match_console_logging.patch /usr/local/src/

# Add commands for testing database connectivity and setup status until
# they are part of Match source.
COPY DatabaseConnectivityTestCommand.php ${COMANAGE_MATCH_DIR}/app/src/Command/
COPY DatabaseSetupAlreadyCommand.php ${COMANAGE_MATCH_DIR}/app/src/Command/

EXPOSE 80 443

# Allow values for first administrator bootstrapped into the
# platform to be specified at image build time, in addition to
# being injected at run time through the entrypoint script.
ARG COMANAGE_MATCH_ADMIN_USERNAME

# Set simple defaults for first administrator bootstrapped into the
ENV COMANAGE_MATCH_ADMIN_USERNAME ${COMANAGE_MATCH_ADMIN_USERNAME:-match.admin}

ENTRYPOINT ["docker-comanage-match-entrypoint"]

CMD ["apache2-foreground"]