+#!/bin/bash
+# Bootstrap a Drupal project
+#
+# Copyright (C) 2017 Antonio Ospite <ao2@ao2.it>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+set -e
+
+usage() {
+ cat <<EOF
+usage: drin $(basename $0 .sh) [--devel|-h|--help]
+
+Bootstrap a Drupal project, using settings from a 'bootstrap.conf' file.
+
+Options:
+ --devel install drupal/devel and use a settings.local.php file
+ -h, --help display this usage message and exit
+
+EOF
+}
+
+while [ $# -gt 0 ];
+do
+ case "$1" in
+ -h|--help)
+ usage
+ exit 0
+ ;;
+ --devel)
+ DEVEL_MODE="true"
+ ;;
+ -*)
+ echo "Error: Unknown option '${1}'" 1>&2
+ ;;
+ esac
+ shift
+done
+
+. bootstrap.conf
+
+declare -p DB_NAME
+declare -p DB_USER
+declare -p DB_PASS
+
+declare -p ACCOUNT_NAME
+declare -p ACCOUNT_PASS
+declare -p ACCOUNT_MAIL
+
+declare -p SITE_NAME
+declare -p SITE_MAIL
+declare -p SITE_BASE_PATH
+
+declare -p TRUSTED_HOSTS
+
+declare -p WEB_SERVER_GROUP
+
+[ "x$INSTALLATION_PROFILE" = "x" ] && { echo "INSTALLATION_PROFILE not specified, using the \"standard\" profile!"; INSTALLATION_PROFILE="standard"; }
+
+if [ "x$MYSQL_ROOT_PASSWORD" = "x" ];
+then
+ read -s -p "MySQL root password: " MYSQL_ROOT_PASSWORD
+ echo
+fi
+
+SITE_LOCAL_PATH="${PWD}/web"
+
+command -v composer &> /dev/null || { echo "Aborting, 'composer' not available." 1>&2; exit 1; }
+command -v git &> /dev/null || { echo "Aborting, 'git' not available." 1>&2; exit 1; }
+
+[ -d "$SITE_LOCAL_PATH" ] || composer install
+
+# TODO check if the commands are available
+DRUSH="${PWD}/vendor/bin/drush"
+DRUPAL_CONSOLE="${PWD}/vendor/bin/drupal"
+
+[ -x "$DRUSH" ] || { echo "Aborting, '$DRUSH' not available." 1>&2; exit 1; }
+[ -x "$DRUPAL_CONSOLE" ] || { echo "Aborting, '$DRUPAL_CONSOLE' not available." 1>&2; exit 1; }
+
+# This becomes unnecessary if the installation profile gets pulled in by
+# composer.json, like suggested in
+# https://github.com/drupal-composer/drupal-project/issues/249
+if ! echo $INSTALLATION_PROFILE | egrep -q "^(minimal|standard)$";
+then
+ if [ -d $SITE_LOCAL_PATH/profiles/$INSTALLATION_PROFILE ];
+ then
+ echo "Installation profile '$INSTALLATION_PROFILE' already there."
+ else
+ cp -a $INSTALLATION_PROFILE $SITE_LOCAL_PATH/profiles
+ fi
+fi
+
+pushd "$SITE_LOCAL_PATH"
+
+$DRUSH --verbose --yes \
+ site-install \
+ --db-su=root \
+ --db-su-pw="$MYSQL_ROOT_PASSWORD" \
+ --db-url="mysql://${DB_USER}:${DB_PASS}@localhost/${DB_NAME}" \
+ --site-name="$SITE_NAME" \
+ --site-mail="$SITE_MAIL" \
+ --account-name="$ACCOUNT_NAME" \
+ --account-pass="$ACCOUNT_PASS" \
+ --account-mail="$ACCOUNT_MAIL" \
+ "$INSTALLATION_PROFILE"
+
+if $DRUSH pm-info --fields=status locale | grep -q enabled;
+then
+ # This is necessary for multi-language sites, it fixes some issues like:
+ # "The Translation source field needs to be installed."
+ $DRUSH --yes entity-updates
+
+ # Update translations of contrib modules
+ $DRUSH --yes locale-update
+fi
+
+# This fixes permissions when installing under $HOME/public_html/
+chmod 775 sites/default/files
+sudo chgrp -R "$WEB_SERVER_GROUP" sites/default/files
+
+[ -d ../config/sync ] && sudo chgrp -R "$WEB_SERVER_GROUP" ../config/sync
+
+# Enables clean URLs
+sed -i "s@# RewriteBase /drupal\$@RewriteBase ${SITE_BASE_PATH}@" .htaccess
+
+chmod 644 sites/default/settings.php
+
+# Add some basic settings to settings.php
+if ! grep -q "^\\\$settings\['trusted_host_patterns'\] =" sites/default/settings.php;
+then
+ echo "\$settings['trusted_host_patterns'] = [" >> sites/default/settings.php
+ for host in "${TRUSTED_HOSTS[@]}"
+ do
+ echo " '^${host}\$'," >> sites/default/settings.php
+ done
+ echo "];" >> sites/default/settings.php
+fi
+
+if [ "$DEVEL_MODE" = "true" ];
+then
+ # NOTE: don't run composer under web/ but in the project dir
+ composer --working-dir=../ require drupal/devel
+ $DRUSH --yes en devel
+
+ chmod 755 sites/default
+ cp sites/example.settings.local.php sites/default/settings.local.php
+ chmod 444 sites/default/settings.local.php
+ chmod 555 sites/default
+
+ if ! grep -q "^include \$app_root . '/' . \$site_path . '/settings.local.php';" sites/default/settings.php;
+ then
+ echo "include \$app_root . '/' . \$site_path . '/settings.local.php';" >> sites/default/settings.php
+ fi
+fi
+
+chmod 444 sites/default/settings.php
+
+# If using a git checkout of Drupal core, set up a diff alias.
+#
+# This is useful because the Automated Test infrastructure of drupal.org does
+# not expect patches to be created from a split core directory.
+if [ -d core/.git ];
+then
+ git -C core/ config --local alias.core-diff "diff --src-prefix=a/core/ --dst-prefix=b/core/"
+ echo "Added a 'git core-diff' to the drupal/core repository clone."
+ echo "This command helps creating core patches ready for upstream."
+fi
+
+popd