# FROM ficklinlab/drupal8
#sudo docker build tripaldocker
# FROM php:7.3-fpm-alpine
FROM ubuntu:18.04

ENV DEBIAN_FRONTEND=noninteractive

# dependencies required for running "phpize"
# (see persistent deps below)
ENV PHPIZE_DEPS \
		autoconf \
		dpkg-dev \
		file \
		g++ \
		gcc \
		libc-dev \
		make \
		pkg-config \
		re2c

# persistent / runtime deps
RUN apt-get update && apt-get install -y \
		$PHPIZE_DEPS \
		ca-certificates \
		curl \
		xz-utils \
	--no-install-recommends && rm -r /var/lib/apt/lists/*

ENV PHP_INI_DIR /usr/local/etc/php

# allow running as an arbitrary user (https://github.com/docker-library/php/issues/743)
RUN set -eux; \
	mkdir -p "$PHP_INI_DIR/conf.d"; \
	# [ ! -d /var/www/html ]; \
	mkdir -p /var/www/html; \
	chown www-data:www-data /var/www/html; \
	chmod 777 /var/www/html

##<autogenerated>##
ENV APACHE_CONFDIR /etc/apache2
ENV APACHE_ENVVARS $APACHE_CONFDIR/envvars

RUN set -eux; \
	apt-get update; \
	apt-get install -y --no-install-recommends apache2; \
	rm -rf /var/lib/apt/lists/*; \
	\
	sed -ri 's/^export ([^=]+)=(.*)$/: ${\1:=\2}\nexport \1/' "$APACHE_ENVVARS"; \
	\
# setup directories and permissions
	. "$APACHE_ENVVARS"; \
	for dir in \
		"$APACHE_LOCK_DIR" \
		"$APACHE_RUN_DIR" \
		"$APACHE_LOG_DIR" \
	; do \
		rm -rvf "$dir"; \
		mkdir -p "$dir"; \
		chown "$APACHE_RUN_USER:$APACHE_RUN_GROUP" "$dir"; \
# allow running as an arbitrary user (https://github.com/docker-library/php/issues/743)
		chmod 777 "$dir"; \
	done; \
	\
# delete the "index.html" that installing Apache drops in here
	rm -rvf /var/www/html/*; \
	\
# logs should go to stdout / stderr
	ln -sfT /dev/stderr "$APACHE_LOG_DIR/error.log"; \
	ln -sfT /dev/stdout "$APACHE_LOG_DIR/access.log"; \
	ln -sfT /dev/stdout "$APACHE_LOG_DIR/other_vhosts_access.log"; \
	chown -R --no-dereference "$APACHE_RUN_USER:$APACHE_RUN_GROUP" "$APACHE_LOG_DIR"

# Apache + PHP requires preforking Apache for best results
RUN a2dismod mpm_event && a2enmod mpm_prefork

# PHP files should be handled by PHP, and should be preferred over any other file type
RUN { \
		echo '<FilesMatch \.php$>'; \
		echo '\tSetHandler application/x-httpd-php'; \
		echo '</FilesMatch>'; \
		echo; \
		echo 'DirectoryIndex disabled'; \
		echo 'DirectoryIndex index.php index.html'; \
		echo; \
		echo '<Directory /var/www/>'; \
		echo '\tOptions -Indexes'; \
		echo '\tAllowOverride All'; \
		echo '</Directory>'; \
	} | tee "$APACHE_CONFDIR/conf-available/docker-php.conf" \
	&& a2enconf docker-php

ENV PHP_EXTRA_BUILD_DEPS apache2-dev
ENV PHP_EXTRA_CONFIGURE_ARGS --with-apxs2 --disable-cgi
##</autogenerated>##

# Apply stack smash protection to functions using local buffers and alloca()
# Make PHP's main executable position-independent (improves ASLR security mechanism, and has no performance impact on x86_64)
# Enable optimization (-O2)
# Enable linker optimization (this sorts the hash buckets to improve cache locality, and is non-default)
# Adds GNU HASH segments to generated executables (this is used if present, and is much faster than sysv hash; in this configuration, sysv hash is also generated)
# https://github.com/docker-library/php/issues/272
ENV PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2"
ENV PHP_CPPFLAGS="$PHP_CFLAGS"
ENV PHP_LDFLAGS="-Wl,-O1 -Wl,--hash-style=both -pie"

ENV GPG_KEYS CBAF69F173A0FEA4B537F470D66C9593118BCCB6 F38252826ACD957EF380D39F2F7956BC5DA04B5D

ENV PHP_VERSION 7.3.5
ENV PHP_URL="https://www.php.net/get/php-7.3.5.tar.xz/from/this/mirror"
ENV PHP_ASC_URL="https://www.php.net/get/php-7.3.5.tar.xz.asc/from/this/mirror"
ENV PHP_SHA256="e1011838a46fd4a195c8453b333916622d7ff5bce4aca2d9d99afac142db2472"
ENV PHP_MD5=""

RUN set -xe; \
	\
	fetchDeps=' \
		wget \
	'; \
	if ! command -v gpg > /dev/null; then \
		fetchDeps="$fetchDeps \
			dirmngr \
			gnupg \
		"; \
	fi; \
	apt-get update; \
	apt-get install -y --no-install-recommends $fetchDeps; \
	rm -rf /var/lib/apt/lists/*; \
	\
	mkdir -p /usr/src; \
	cd /usr/src; \
	\
	wget -O php.tar.xz "$PHP_URL"; \
	\
	if [ -n "$PHP_SHA256" ]; then \
		echo "$PHP_SHA256 *php.tar.xz" | sha256sum -c -; \
	fi; \
	if [ -n "$PHP_MD5" ]; then \
		echo "$PHP_MD5 *php.tar.xz" | md5sum -c -; \
	fi; \
	\
	if [ -n "$PHP_ASC_URL" ]; then \
		wget -O php.tar.xz.asc "$PHP_ASC_URL"; \
		export GNUPGHOME="$(mktemp -d)"; \
		for key in $GPG_KEYS; do \
			gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
		done; \
		gpg --batch --verify php.tar.xz.asc php.tar.xz; \
		command -v gpgconf > /dev/null && gpgconf --kill all; \
		rm -rf "$GNUPGHOME"; \
	fi; \
	\
	apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $fetchDeps

COPY docker-php-source /usr/local/bin/

RUN set -eux; \
	\
	savedAptMark="$(apt-mark showmanual)"; \
	apt-get update; \
	apt-get install -y --no-install-recommends \
		libcurl4-openssl-dev \
		libedit-dev \
		libsodium-dev \
		libsqlite3-dev \
		libssl-dev \
		libxml2-dev \
		zlib1g-dev \
		libpcre3-dev \
		${PHP_EXTRA_BUILD_DEPS:-} \
	;

# https://github.com/docker-library/php/issues/443
	# pecl update-channels; \
	# rm -rf /tmp/pear ~/.pearrc
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:ondrej/php

ENV PHP_VERSION 7.2

RUN apt-get update && apt-get install -yq --no-install-recommends \
    apt-utils \
    curl \
		git \
		zip \
		wget \
		unzip \
		apt-utils \
		gnupg2 \
		sysstat \
    # Install apache
    apache2 \
    # Install php
    libapache2-mod-php$PHP_VERSION \
    php$PHP_VERSION-cli \
    php$PHP_VERSION-json \
    php$PHP_VERSION-curl \
    php$PHP_VERSION-fpm \
    php$PHP_VERSION-gd \
    php$PHP_VERSION-ldap \
    php$PHP_VERSION-mbstring \
    php$PHP_VERSION-mysql \
    php$PHP_VERSION-soap \
    php$PHP_VERSION-sqlite3 \
    php$PHP_VERSION-xml \
    php$PHP_VERSION-zip \
    php$PHP_VERSION-intl \
		php$PHP_VERSION-dev \
		php$PHP_VERSION-pgsql \
		php$PHP_VERSION-gd \
		php$PHP_VERSION-xml \
		php$PHP_VERSION-opcache \
    php-imagick \
    # Install tools
    openssl \
    nano \
    && apt-get clean && rm -rf /var/lib/apt/lists/*
RUN	service apache2 restart;
RUN	 \
		php -v

COPY docker-php-ext-* docker-php-entrypoint /usr/local/bin/

ENTRYPOINT ["docker-php-entrypoint"]
##<autogenerated>##
COPY apache2-foreground /usr/local/bin/


# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html

USER root

# Update the system and add any missing repos
# install the PHP extensions we need
RUN set -ex; \
	\
	if command -v a2enmod; then \
		a2enmod rewrite; \
	fi; \
	\
	savedAptMark="$(apt-mark showmanual)"; \
	\
	apt-get update; \
	apt-get install -y --no-install-recommends \
		libjpeg-dev \
		libpng-dev \
		libpq-dev \
		libzip-dev \
	; \
	\
	docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr;

# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
		echo 'opcache.memory_consumption=128'; \
		echo 'opcache.interned_strings_buffer=8'; \
		echo 'opcache.max_accelerated_files=4000'; \
		echo 'opcache.revalidate_freq=60'; \
		echo 'opcache.fast_shutdown=1'; \
		echo 'opcache.enable_cli=1'; \
	} > /usr/local/etc/php/conf.d/opcache-recommended.ini

# # Install additional software
# RUN set -x && apt-get update && apt-get -y install wget curl git zip unzip apt-utils gnupg2 sysstat

# Install composer and drush
RUN curl --silent --show-error https://getcomposer.org/installer | php

# install drush
RUN php composer.phar global require drush/drush
ENV PATH="/root/.composer/vendor/bin:${PATH}"
RUN drush --version

RUN apt-get update && \
      apt-get -y install sudo

ENV POSTGRES_VERSION 9.6
ENV PATH "/root/.composer/vendor/bin:/usr/pgsql-$POSTGRES_VERSION/bin:$PATH"
ENV PGDATA "/var/lib/pgsql/$POSTGRES_VERSION/data"
ENV PDEXP "/pgexp"
ENV PGUSER "postgres"

RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
RUN apt-get update
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main $POSTGRES_VERSION" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update -y && sudo apt-get upgrade -y
RUN apt-get -y install -f postgresql-$POSTGRES_VERSION postgresql-client-$POSTGRES_VERSION postgresql-contrib-$POSTGRES_VERSION
RUN apt-get clean
RUN echo "host    all    all    0.0.0.0/0    md5" >> /etc/postgresql/$POSTGRES_VERSION/main/pg_hba.conf
RUN echo "listen_addresses='*'" >> /etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf

USER postgres

RUN /etc/init.d/postgresql start && \
    psql --command "CREATE USER drupal WITH SUPERUSER PASSWORD 'drupal';" && \
    createdb -O drupal drupal

# # Scripts
# ADD supervisord-apache2.conf /etc/supervisor/conf.d/supervisord-apache2.conf
# ADD supervisord-apache2.sh /supervisord-apache2.sh
# ADD supervisord-postgresql.conf /etc/supervisor/conf.d/supervisord-postgresql.conf
# ADD supervisord-postgresql.sh /supervisord-postgresql.sh
# ADD start.sh /start.sh
# RUN chmod 755 /*.sh


# https://www.drupal.org/node/3060/release
ENV DRUPAL_VERSION 8.7.1
ENV DRUPAL_MD5 2cf2a1c93ea785c6ff91d29aebef2697

USER root

# Add a testuser user and group for the installation
RUN groupadd testuser \
  && useradd -g testuser --no-log-init --create-home --shell /bin/bash testuser \
	&& usermod -a -G www-data testuser

#USER testuser

# Download console.
RUN curl https://drupalconsole.com/installer -L -o drupal.phar

# Install console.
RUN chmod +x drupal.phar && ./drupal.phar init --override

WORKDIR /var/www/html

RUN ./drupal.phar settings:set checked "true"

RUN  curl -fSL "https://ftp.drupal.org/files/projects/drupal-${DRUPAL_VERSION}.tar.gz" -o drupal.tar.gz \
	&& echo "${DRUPAL_MD5} *drupal.tar.gz" | md5sum -c - \
	&& tar -xz --strip-components=1 -f drupal.tar.gz \
	&& rm drupal.tar.gz

RUN cd /var/www/html/sites/default && cp default.settings.php settings.php

RUN cd /var/www/html \
		&& composer require drupal/console:~1.0 --prefer-dist --optimize-autoloader

RUN /etc/init.d/postgresql start && \
    ./drupal.phar site:install standard --langcode="en" --db-type="pgsql" --db-host="127.0.0.1" \
       --db-name="drupal" --db-user="drupal" --db-pass="drupal" --db-port="5432" \
       --site-name="Drupal 8 Site Install" --site-mail="admin@example.com" \
       --account-name="admin" --account-mail="admin@example.com" --account-pass="admin" \
       --no-interaction && drush cr

		# --db-url=pgsql://drupal:drupal@127.0.0.1/drupal --account-name=drupal --account-pass=drupal -vvv -y \
		# && drush pm-update drupal -y

# Set files directory permissions
RUN chown -R testuser:www-data /var/www/html/sites/default/files

# Expose http and psql port
# EXPOSE 80 5432

# Configuration files
COPY supervisord.conf /etc/supervisord.d/app.ini
COPY apache.conf /etc/httpd/conf.d/apache.conf

# Activation scripts
# COPY init.sh /usr/bin/init.sh
# RUN chmod +x /usr/bin/init.sh

RUN git clone https://github.com/tripal/tripal.git sites/all/modules/contrib/tripal

# Enable tripal 4 module
RUN /etc/init.d/postgresql start \
  && /etc/init.d/apache2 start \
  && drush en -y tripal \
	# Populate the cache
	&& curl localhost > /dev/null

EXPOSE 80 5432

# Tini for signal processing and zombie killing.
ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini


# Define the command and parameters that will be executed when this
# container is first run.
ENTRYPOINT ["/tini", "--"]
