Fix double quoting, suggested by shellcheck
[drupal-init-tools.git] / libexec / bootstrap.sh
index 74547af..42911cb 100755 (executable)
@@ -20,13 +20,14 @@ set -e
 
 usage() {
   cat <<EOF
 
 usage() {
   cat <<EOF
-usage: drin $(basename $0 .sh) [--devel|-h|--help]
+usage: drin $(basename "$0" .sh) [--devel|-h|--help]
 
 Bootstrap a Drupal project, using settings from a 'bootstrap.conf' file.
 
 Options:
 
 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
+  --devel              install drupal/devel and use a settings.local.php file
+  --overwrite-profile  allow overwriting the current installation profile
+  -h, --help           display this usage message and exit
 
 EOF
 }
 
 EOF
 }
@@ -41,6 +42,9 @@ do
     --devel)
       DEVEL_MODE="true"
       ;;
     --devel)
       DEVEL_MODE="true"
       ;;
+    --overwrite-profile)
+      OVERWRITE_PROFILE="true"
+      ;;
     -*)
       echo "Error: Unknown option '${1}'" 1>&2
       ;;
     -*)
       echo "Error: Unknown option '${1}'" 1>&2
       ;;
@@ -90,18 +94,30 @@ DRUPAL_CONSOLE="${PWD}/vendor/bin/drupal"
 # 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
 # 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)$";
+if ! echo "$INSTALLATION_PROFILE" | egrep -q "^(minimal|standard)$";
 then
 then
-  if [ -d $SITE_LOCAL_PATH/profiles/$INSTALLATION_PROFILE ];
+  if [ -d "${SITE_LOCAL_PATH}/profiles/${INSTALLATION_PROFILE}" -a "$OVERWRITE_PROFILE" != "true" ];
   then
   then
-    echo "Installation profile '$INSTALLATION_PROFILE' already there."
+    echo "Installation profile '$INSTALLATION_PROFILE' already there." 1>&2
+    echo "Use --overwrite-profile to copy over it." 1>&2
+    exit 1
   else
   else
-    cp -a $INSTALLATION_PROFILE $SITE_LOCAL_PATH/profiles
+    cp -a "$INSTALLATION_PROFILE" "${SITE_LOCAL_PATH}/profiles"
   fi
 fi
 
 pushd "$SITE_LOCAL_PATH"
 
   fi
 fi
 
 pushd "$SITE_LOCAL_PATH"
 
+# Update the install_profile if it's already there
+if grep -q "^\\\$settings\['install_profile'\] =" sites/default/settings.php;
+then
+  chmod 755 sites/default
+  chmod 644 sites/default/settings.php
+  sed -i -e "s/^\(\$settings\['install_profile'\]\) = '[^']*';/\1 = '$INSTALLATION_PROFILE';/g" sites/default/settings.php
+  chmod 444 sites/default/settings.php
+  chmod 555 sites/default
+fi
+
 $DRUSH --verbose --yes \
   site-install \
   --db-su=root \
 $DRUSH --verbose --yes \
   site-install \
   --db-su=root \
@@ -133,6 +149,7 @@ sudo chgrp -R "$WEB_SERVER_GROUP" sites/default/files
 # Enables clean URLs
 sed -i "s@# RewriteBase /drupal\$@RewriteBase ${SITE_BASE_PATH}@" .htaccess
 
 # Enables clean URLs
 sed -i "s@# RewriteBase /drupal\$@RewriteBase ${SITE_BASE_PATH}@" .htaccess
 
+chmod 755 sites/default
 chmod 644 sites/default/settings.php
 
 # Add some basic settings to settings.php
 chmod 644 sites/default/settings.php
 
 # Add some basic settings to settings.php
@@ -152,10 +169,16 @@ then
   composer --working-dir=../ require drupal/devel
   $DRUSH --yes en devel
 
   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 [ ! -e sites/default/settings.local.php ];
+  then
+    cp sites/example.settings.local.php sites/default/settings.local.php
+
+    # Disable some overly permissive settings
+    sed -i -e "s/^\(\$settings\['rebuild_access'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
+    sed -i -e "s/^\(\$settings\['skip_permissions_hardening'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
+
+    chmod 444 sites/default/settings.local.php
+  fi
 
   if ! grep -q "^include \$app_root . '/' . \$site_path . '/settings.local.php';" sites/default/settings.php;
   then
 
   if ! grep -q "^include \$app_root . '/' . \$site_path . '/settings.local.php';" sites/default/settings.php;
   then
@@ -164,6 +187,7 @@ then
 fi
 
 chmod 444 sites/default/settings.php
 fi
 
 chmod 444 sites/default/settings.php
+chmod 555 sites/default
 
 # If using a git checkout of Drupal core, set up a diff alias.
 #
 
 # If using a git checkout of Drupal core, set up a diff alias.
 #