#!/bin/bash

# Copyright 2002 Raphaël Hertzog
# 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
    needupdate="no"
    param="--stringparam dir $dir --stringparam package $package"
    # 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
    for i in news excuse other s-p-u t-p-u 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, ...)
	    input=$dir/$i.xml
	    param="$param --stringparam has$i yes"
	fi
	if [ "$needupdate" = "no" -a "$dir/$i.xml" -nt "$htmlfile" ]; then
	    needupdate="yes"
	fi
    done
    if [ ! -d ../web/$dir/static ]; then
	mkdir -p ../web/$dir/{static,news} || true
    fi
    if [ -n "$verbose" ]; then
	echo "Doing $package ..."
    fi
    if [ "$needupdate" = "yes" ]; then
	xsltproc -o $htmlfile --stringparam date "$date" \
	    $param ../xsl/pts.xsl $input
	rm -f $dir/force-rebuild || true
    fi
done

