#!/bin/sh
# $Id: update-recent-releases,v 1.13 2021/02/04 16:32:02 ineiev Exp $
# Public domain.  Originally written by Karl Berry, January 2013.
# This script is run (more than daily) from gnun@fencepost cron.
# 
# Construct list of recent GNU releases from info-gnu archives.  The
# idea is to keep two .html files, one named "lastmonth.html" for the
# previous month's announcements and one named YYYY-MM.html for the
# current month's.  When a new month rolls in, current becomes previous,
# naturally.
# 
# We extract out the list of announcements from the full web pages to be
# SSIncluded in http://www.gnu.org/software/recent-releases.html.
# 
# The very first time it is run in a given hierarchy, it is necessary to
# set up lastmonth.html by hand and a dummy YYYY-MM.html.
# 
# We assume GNU date and a decent sed.

if test $# -gt 0; then
  www_software_dir=$1
else
  mydir=`dirname $0`  # WWW/server/source
  www_software_dir=`cd $mydir/../../software/ && pwd`
fi
cd $www_software_dir || exit 1

www_software_dir=`pwd`

var_dir="$www_software_dir/recent-releases-tmp"

if test $# -gt 1; then
  var_dir=$2
fi

# Need to work in tmp dir.
test -d "$var_dir" || mkdir "$var_dir"
cd "$var_dir" || exit 1

wget="wget -q"
info_gnu=http://lists.gnu.org/archive/html/info-gnu

lastmonth=`date +%Y-%m -d "$(date +%Y-%m-15) -1 month"`
thismonth=`date +%Y-%m`
$wget $info_gnu/$thismonth/index.html -O $thismonth.html.new

if test -f $lastmonth.html; then
  # First run in a new month.
  echo "$0: seems $thismonth is a new month, resetting lastmonth=$lastmonth."
  mv $lastmonth.html lastmonth.html  # static name
fi

if test -s $thismonth.html.new; then
  touch $thismonth.html
  # Continuing on in current month.  See if we have anything new.
  if cmp -s $thismonth.html $thismonth.html.new; then
    echo "$0: no changes, goodbye."
    rm -f $thismonth.html.new
    exit 0
  fi
  echo "$0: new releases in $thismonth, merging."
  diff $thismonth.html $thismonth.html.new
else
  echo "$0: no releases in this month yet."
  exit 0
fi

mv $thismonth.html.new $thismonth.html

# Given html info-gnu archive file in $1 and month reference in $2, get
# rid of the leading and trailing junk, remove the anchors, and fix the
# href's.  Output to stdout.
# 
fix_month () {
  infile=$1
  month=$2
  sed -e '1,/<hr size=/d' -e '/<hr size=/,$d' \
      -e 's, name="[0-9]\{5\}", ,' \
      -e "s, href=\",href=\"$info_gnu/$month/," \
      "$1"
}

(fix_month $thismonth.html $thismonth; fix_month lastmonth.html $lastmonth) \
  > "$www_software_dir/recent-releases-include.html"

# Make HTML elements like links invisible for the translators.
sed '1 i\
<!--#set var="LINK_CLOSE" value="</a>, <i>" -->
/<li><a / {
  s,http:,,
  s,<a href="\([^"]*\)">,<!--#set var="LINK" \
value='"'"'<a href="\1">'"'"' --><!--#echo \
encoding="none" var="LINK" -->,
  s:</a>, <i>:<span class="gnun-split"></span><!--#echo \
encoding="none" var="LINK_CLOSE" -->:
  s:\(</i>,\) <tt>\(.*\)</tt>:<span class="gnun-split"></span><!--#set \
var="TIME" value="\1 \2" --><!--#echo encoding="none" var="TIME" -->:
}' -i ../recent-releases-include.html

cd "$www_software_dir" || exit 1

validate_file () {
  (echo '<html>'; cat "$1"; echo '</html>') | xmllint --noout - 2> /dev/null
}

# Try to fix validation errors.
if validate_file recent-releases-include.html; then
  echo "$0: the file validates."
else
  echo "$0: fixing validation errors."
  sed 's,^</li></ul>$,</ul></li>,' recent-releases-include.html \
    > "$var_dir/recent-releases-include.html"
  if validate_file "$var_dir/recent-releases-include.html"; then
    echo "$0: the errors were fixed."
    cp "$var_dir/recent-releases-include.html" .
  else
    echo "$0: failed to fix."
  fi
fi

cvs commit -m"auto-commit: new $thismonth releases" \
  recent-releases-include.html
