#!/bin/sh
#
# This script is meant to find all Drupal directories in a structured subtree
# and call the cron php script in each Drupal installation.
#
# By default the script assumes that the directories inside the {wwwroot}
# directory wear the name (actually the FQDN) of the virtualhosts that they
# represent, and that inside these directories there's a {virtual_subdir}
# directory which is the documentroot of the virtualhost.
# The Drupal installations should reside in a child directory of these
# {virtual_subdir} directories.
#
# Eg.
#   /var/www/domain1.com/www/cron.php
#   /var/www/domain2.com/www/subdir1/cron.php
#   /var/www/domain2.com/www/subdir2/cron.php
#   /var/www/www.domain3.com/www/cron.php
#
# If you do not use any subdirectories for www content in your virtualhost
# directories, then set the {virtual_subdir} to an empty string.
#
# If you do not use virtualhosts at all (eg. default Apache installations come
# usually with a DocumentRoot of /var/www), then set {use_virtual_dirs} to "no".
#
# You should execute this script from some system-wide cron mechnism
# periodically, eg. you could put this script into /etc/cron.hourly
# or call it from /etc/crontab or some user's crontab. The script produces
# output only in case of an error, thus if your system is set up correctly,
# the system cron should send you an email containing a detailed report for all
# erroneous cron.php calls.

wwwroot=/var/www
use_virtual_dirs=yes
virtual_subdir=/www

umask 0077

if ! which wget > /dev/null 2>&1; then
  echo "Wget is not installed."
  exit 1
fi

if ! which host > /dev/null 2>&1; then
  echo "The \"host\" command is not installed."
  exit 2
fi

[ "${use_virtual_dirs}" != "yes" ] && virtual_subdir=""

search_dir() {
  # process only directories where the hostname is found by the "host" command
  if host "${1}" > /dev/null 2>&1; then
    for site in $(find . -type f -name 'cron.php' -print | cut -c 3-); do
      dir=$(dirname "${site}")
      if [ \( -f "${dir}/update.php" -a -f "${dir}/index.php" -a -d "${dir}/sites" \) -o -f "${dir}/.run_cron" ]; then
        if [ "${dir}" != "." ]; then
          dirpath="/${dir}"
        else
          dirpath=""
        fi
        cronuri="http://${1}${dirpath}/cron.php"
        #echo "Calling wget for \"${PWD}${dirpath}/cron.php\" and \"${cronuri}\" ..."
        cronoutputfile=$(mktemp)
        wgetoutput=$(wget --bind-address 127.0.0.1 -O "${cronoutputfile}" -t 1 "${cronuri}" 2>&1)
        ret=$?
        if [ $ret -ne 0 -o -s "${cronoutputfile}" ]; then
          cat <<EOH1

--------------------------------------------------------------------------------

Cron job at "${cronuri}" did not complete successfully or quietly.
EOH1
          if [ $ret -ne 0 ]; then
            cat <<EOH2

Wget procuced the following output:
${wgetoutput}
EOH2
          fi
          if [ -s "${cronoutputfile}" ]; then
            cat <<EOH3

The output of the cron script (probably HTML):
EOH3
            cat "${cronoutputfile}"
          fi
        fi
        [ -f "${cronoutputfile}" ] && rm "${cronoutputfile}"
      fi
    done
  fi
}

if [ -d "${wwwroot}" ]; then
  cd "${wwwroot}"
  if [ "${use_virtual_dirs}" = "yes" ]; then
    for fqdn in *; do
      if [ -d "${wwwroot}/${fqdn}${virtual_subdir}" ]; then
        cd "${wwwroot}/${fqdn}${virtual_subdir}"
        search_dir "${fqdn}"
      fi
    done
  else
    search_dir $(hostname --long)
  fi
else
  echo "The path at \"${wwwroot}\" is not a directory."
  exit 3
fi


