#!/bin/sh
#set -xv

###
# # Set script variables
###
WWW_HOME=~www
export WWW_HOME
# read variables used by several scripts
echo "`date`  Sourcing ${WWW_HOME}/bin/nightly-vars"
. ${WWW_HOME}/bin/nightly-vars

###
# # Prepare diffmon files
###
echo "`date`  Making diffmon config file for English HTML files"
#
# 1) Doing this by excluding
#        -name "*_toc.html"
#        -name "*_stoc.html"
#        -name "*_foot.html"
#        -regex ".*_[0-9]+.html"
# is a small kludge, as it assumes there are no manually created
# files with these patterns.  -len tower
#
# If you make a change to this exception list,
# you should consider also changing the list in make.html.TAGS
# They can be (and currently are) different.
#
# 2) Filenames that have colons (":") in them confuse diffmon,
# so we wild card them with the "sed -e 's/:/?/g" lines below.
#
# 3) Without the "sort -u" lines below, there are reports of a file being
# added and deleted in the same diffmon run on a diffmon-web*.cf file.  The
# reason is that `find's output is not sorted.
#
find ${HTMLDIRS} \
    -name "*.html" -type f \
    \
    -a \! -name "*_toc.html" \
    -a \! -name "*_stoc.html" \
    -a \! -name "*_foot.html" \
    -a \! -regex ".*_[0-9]+.html" \
    -a \! -path "/home/www/html/directory/*" \
    \
    -a \! -path "/home/www/html/software/*/*" \
    | sed -e 's/:/?/g' \
    | awk \
      '{ printf "%s : -u : diffmon-web\n",$1 }' \
    | sort -u \
    | sed -f /home/diffmon/diffmon-web.sed \
        > /home/diffmon/diffmon-web.cf
# used WEBLANG here, not TAGSLANG.  the webmasters are responsible for
# the translations for which there are no translation teams. -len tower
for i in $WEBLANG; do
    mv /home/diffmon/diffmon-web.cf /home/diffmon/diffmon-web.cf.IN
    grep -v "\.$i\.html : " \
	< /home/diffmon/diffmon-web.cf.IN \
	> /home/diffmon/diffmon-web.cf
done
rm /home/diffmon/diffmon-web.cf.IN

echo "`date`  Making diffmon config file for translation team:"
echo -n "    "
for i in $WEBLANG; do
    echo -n " $i"
  # Set up for the translated files:
    find ${HTMLDIRS} \
      -name "*.$i.html" -type f \
      -a \! -path "/home/www/html/brave-gnu-world/*" \
      -a \! -path "/home/www/html/server/$i/*" \
      -a \! -path "/home/www/html/software/*/*" \
    | sed -e 's/:/?/g' \
    | sort -u \
    | awk -v LANG=$i \
      '{ printf "%s : -u : diffmon-web-%s-%s\n",$1,LANG,LANG }' \
      > /home/diffmon/diffmon-web-$i-$i.cf
  # Now set up for the English originals that are translated:
  # NOTE: Some files are not translations, they are originals written in
  # that language (e.g. some under /home/www/html/fsfe/), and
  # English translations of them may not exist.   In these cases, 
  # errors will be sent to diffmon-web-errors on those non-existant files.
  # An addition to this script to delete such files from
  # /home/diffmon/diffmon-web-$i.cf  would make monitoring the mail sent to
  # diffmon-web-errors easier.  A file containing the files to delete would
  # be the best way to go, as it means a deliberate decision has been
  # made to not monitor a file.  -tower
    sed -e "s/$i\.html : /html : /" \
        -e "s/diffmon-web-$i-$i/diffmon-web-$i/" \
      < /home/diffmon/diffmon-web-$i-$i.cf \
      > /home/diffmon/diffmon-web-$i.cf
  # Now add the webmaster guideline diffmons
    cat >> /home/diffmon/diffmon-web-$i.cf <<EOF
/home/www/html/server/fsf-html-style-sheet.html : -u : web-translators-$i
EOF
    find /home/www/html/server/standards/ \
	-name "*.html" -type f \
	-printf "%p : -u : web-translators-$i\n" \
    >> /home/diffmon/diffmon-web-$i.cf
done
echo " "

###
# # Run diffmon
###
echo "`date`  Running diffmon for translation team:"
echo -n "    "
for i in $WEBLANG; do
    echo -n " $i"
  # ran with the -n flag - this is a local change to diffmon that
  # prevents cache updates
    /home/diffmon/diffmon -c diffmon-web-$i.cf -e diffmon-web-$i-errors -o /home/diffmon/old_file_dir -p /home/diffmon -n
  # let the e-mail queue empty (to keep web server response reasonable):
    sleep 120
    /home/diffmon/diffmon -c diffmon-web-$i-$i.cf -e diffmon-web-$i-$i-errors -o /home/diffmon/old_file_dir -p /home/diffmon
  # let the e-mail queue empty (to keep web server response reasonable):
    sleep 120
done
echo " "

echo "`date`  Running diffmon for English HTML files"
/home/diffmon/diffmon -c diffmon-web.cf -e diffmon-web-errors -o /home/diffmon/old_file_dir -p /home/diffmon
# let the e-mail queue empty (to keep web server response reasonable):
sleep 120

echo "`date`  Running main diffmon"
/home/diffmon/diffmon -c diffmon.cf -e diffmon-www-errors -o /home/diffmon/old_file_dir -p /home/diffmon
# let the e-mail queue empty (to keep web server response reasonable):
sleep 120

# this compile-command lets you syntax check the script.  -len
#
# local variables:
# eval: (make-local-variable 'compile-command)
# eval: (setq compile-command (concat "/bin/sh -n " buffer-file-name))
# quote-region-quote: "# "
# end:
