#!/bin/bash
# This file is provided by the wikibase/wikibase docker image.

# Test if required environment variables have been set
REQUIRED_VARIABLES=(MW_ADMIN_NAME MW_ADMIN_PASS MW_ADMIN_EMAIL MW_WG_SECRET_KEY DB_SERVER DB_USER DB_PASS DB_NAME)
for i in ${REQUIRED_VARIABLES[@]}; do
    eval THISSHOULDBESET=\$$i
    if [ -z "$THISSHOULDBESET" ]; then
    echo "$i is required but isn't set. You should pass it to docker. See: https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file";
    exit 1;
    fi
done

# Install composer
EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php

php composer.phar install

set -eu

# Wait for the db to come up
/wait-for-it.sh $DB_SERVER -t 300
# Sometimes it appears to come up and then go back down meaning MW install fails
# So wait for a second and double check!
sleep 1
/wait-for-it.sh $DB_SERVER -t 300


# Do the mediawiki install (only if LocalSettings doesn't already exist)
if [ ! -e "/var/www/html/LocalSettings.php" ]; then
    php /var/www/html/maintenance/install.php \
    --dbuser $DB_USER \
    --dbpass $DB_PASS \
    --dbname $DB_NAME \
    --dbserver $DB_SERVER \
    --lang $MW_SITE_LANG \
    --pass $MW_ADMIN_PASS \
    --server $MW_SERVER \
    $MW_SITE_NAME $MW_ADMIN_NAME
    php /var/www/html/maintenance/resetUserEmail.php --no-reset-password $MW_ADMIN_NAME $MW_ADMIN_EMAIL

    # Run update.php to install Wikibase
    php /var/www/html/maintenance/update.php --quick

    cat /extraSettings.php.fragment >> /var/www/html/LocalSettings.php

fi

# Run the actual entry point
docker-php-entrypoint apache2-foreground
