#!/bin/bash
# WF 2018-12-31

#
# get a docker compose file
#
get_docker_compose() {
cat << EOF
#
#  ProfiWiki based on Semantic Media Wiki based on Mediawiki
#
# see
# https://www.semantic-mediawiki.org/wiki/Help:Using_Docker
# https://github.com/SemanticMediaWiki/SemanticMediaWiki/issues/1218
# https://www.semantic-mediawiki.org/wiki/SMWCon_Fall_2018/Benefits_of_running_MediaWiki_in_a_pertinent_dockerized_setup
# "Official" Mediawiki image
# https://hub.docker.com/_/mediawiki/
# WF 2018-12-29
#
# MediaWiki with MariaDB
#
# Access via "http://localhost:8080"
#   (or "http://docker-machine ip:8080" if using docker-machine)
version: "3"

# 2 services
#   db - database
#   mw - mediawiki
services:

#  MySQL compatible relational database
  db:
# use original image
    image: mariadb:10.3
    restart: always
    environment:
      MYSQL_DATABASE: wiki
      MYSQL_USER: wikiuser
      MYSQL_PASSWORD: "$MYSQL_PASSWORD"
      MYSQL_RANDOM_ROOT_PASSWORD: "yes"
    ports:
      - 3306:3306
    volumes:
      - mysqldata:/var/lib/mysql

# mediawiki
  mw:
    #image: mediawiki:$MEDIAWIKI_VERSION
    # use the Dockerfile in this directory
    build: .
    restart: always
    ports:
      - $MEDIAWIKI_PORT:80
    links:
      - db
    volumes:
      - wikiimages:/var/www/html/images
    # After initial setup, download LocalSettings.php to the same directory as
    # this yaml and uncomment the following line and use compose to restart
    # the mediawiki service
    # - ./LocalSettings.php:/var/www/html/LocalSettings.php
volumes:
  mysqldata:
    driver: local
  wikiimages:
    driver: local
EOF
}

#
# get the docker file
#
get_dockerfile() {
  timestamp=$(date -u +"%Y-%m-%d %H:%M:%S")
cat << EOF
#
# This dockerfile was generated by the profiwiki-install script
# at $timestamp by $USER on $(hostname)
#
FROM mediawiki:$MEDIAWIKI_VERSION
# responsible for this image setup
# see also http://www.bitplan.com/index.php/ProfiWiki
MAINTAINER Wolfgang Fahl <wf@bitplan.com>
# install some more utilities
RUN apt-get update && apt-get install -y \
  dialog \
  imagemagick \
  graphviz \
  libjpeg62-turbo-dev \
  libfreetype6-dev \
  libpng-dev \
  libzip-dev \
  mariadb-client-core-10.1 \
  vim \
  unzip \
  zip && \
  docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
  docker-php-ext-install gd

# openjdk-8-jdk may be needed later but see
# https://github.com/puckel/docker-airflow/issues/182

# make sure we use PHP 7.2 under Debian 9
# see https://dominicpratt.de/php-7-2-unter-debian-9/
#RUN wget -q -O- https://packages.sury.org/php/apt.gpg | apt-key add -
#RUN echo "deb https://packages.sury.org/php/ stretch main" | tee /etc/apt/sources.list.d/php.list

# enable rewrite module
RUN a2enmod rewrite

# change ownership of files
RUN chown -R www-data.www-data /var/www/html

# COPY the wiki-config script holding the configuration parameters
COPY wiki-config.sh /root

# COPY the wiki-pwconfig script holding the password parameters
COPY wiki-pwconfig.sh /root

# COPY the install script
COPY profiwiki-install.sh /root
EOF
}

# name of the directory
target="$1"
mkdir -p $target
get_docker_compose > $target/docker-compose.yml
get_dockerfile > $target/Dockerfile
