#!/bin/bash

# Copyright 2002 Raphaël Hertzog
# Copyright 2007-2008 Stefano Zacchiroli
# This file is distributed under the terms of the General Public License
# version 2 or (at your option) any later version.

verbose=

PATH="/bin:/sbin:/usr/bin:/usr/sbin"

if [ -d "incoming" ]; then
    root="$PWD"
elif [ -d "../incoming" ]; then
    root="$PWD/.."
else
    root="/org/packages.qa.debian.org/www"
fi

if [ "$1" = "-v" -o "$2" = "-v" ]; then
    verbose=1
fi
if [ "$1" = "-f" -o "$2" = "-f" ]; then
    force="yes"
fi

cd $root/base
date=`LC_ALL=C date -u`

while read package
do
    hash=${package:0:1}
    if [ "${package:0:3}" = "lib" ]; then
	hash=${package:0:4}
    fi
    dir=$hash/$package
    htmlfile=$root/web/$dir.html
    rssfile=$root/web/$dir/news.rss20.xml
    needupdate="no"
    param=""
    param="$param --stringparam dir $dir"
    param="$param --stringparam package $package"
    param="$param --stringparam hash $hash"
    # If the file doesn't exist, then require a generation !
    if [ ! -f $htmlfile ]; then
	needupdate="yes"
    fi
    # Force regeneration
    if [ "$force" = "yes" ]; then
	needupdate="yes"
    fi
    # Check if force-rebuild exists
    if [ -e $dir/force-rebuild ]; then
	needupdate="yes"
    fi
    # If the XSL file is newer than the HTML file, then force update
    if [ "../xsl/pts.xsl" -nt "$htmlfile" ]; then
	needupdate="yes"
    fi
    # Create parameters and check if an update is needed
    # XXX order _does_ matter, the latter has precedence over the former
    for i in news excuse other \
      mentors volatile \
      stable-proposed-updates testing-proposed-updates \
      oldstable \
      oldstable-security stable-security testing-security \
      experimental stable testing unstable
    do
	if [ -f "$dir/$i.xml" ]; then
	    # Input is the main xml file used by xsltproc to generate the HTML
	    # page. By default it is unstable.xml but it has many
	    # fallbacks (testing.xml, stable.xml, experimental.xml, ...)
	    if [ "$i" != "mentors" ] ; then
	      # mentors.d.n needs special handling, because we don't want a PTS
	      # page to exist for packages only available on mentors.d.n
	      input=$dir/$i.xml
	    fi
	    param="$param --stringparam has$i yes"
	fi
	if [ "$needupdate" = "no" -a "$dir/$i.xml" -nt "$htmlfile" ]; then
	    needupdate="yes"
	fi
    done
    # If no distribution-related input files has been found, 
    # then the package has been removed
    if [ "$input" = "$dir/other.xml" ]; then
	input="../xsl/default.xml"
	param="$param --stringparam removed yes"
	if [ "$needupdate" = "no" -a "../xsl/default.xml" -nt "$htmlfile" ]; then
	    needupdate="yes"
	fi
    else
	param="$param --stringparam removed no"
    fi
    if [ ! -d ../web/$dir/static ]; then
	mkdir -p ../web/$dir/{static,news} || true
    fi
    if [ "$needupdate" = "yes" ]; then
	if [ -n "$verbose" ]; then
	    echo "Doing $package ..."
	fi
        errors="no"
	xsltproc -o $htmlfile \
	  --stringparam date "$date" $param \
	  ../xsl/pts.xsl $input
        test "$?" -ne 0 && errors="yes"
	if [ -f "$dir/news.xml" ]; then
	  xsltproc -o $rssfile \
	    --stringparam date "$date" $param \
	    ../xsl/news2rss.xsl $dir/news.xml
          test "$?" -ne 0 && errors="yes"
	fi
        test "$errors" = "yes" && \
          echo "PTS: non-0 exit code while generating output for package $package"
	rm -f $dir/force-rebuild || true
    fi
done

