#!/usr/bin/perl -w

# This script 
#
#   make_makefile_reports.pl - Create dependencies for events pages with reports
#   Copyright (c) 2002  Software in the Public Interest, Inc.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# $Id: make_ics.pl,v 1.3 2003/01/06 10:22:10 peterk Exp $

die "Not enough arguments" if ($#ARGV < 0);
$LANG = $ARGV[0]; shift;

$ENGLISHDIR = '';
if ($#ARGV > -1) {
    $ENGLISHDIR = $ARGV[0];
}

$output = '';

opendir (DIR, ".") || die "Can't open current directory, huh?";
@files = sort (grep (/^.*-report\.wml$/,readdir(DIR)));
closedir (DIR);

for $f (@files) {
    $event = $f;
    $event =~ s/-report//;

    if (-r $event) {
	$report{$event} = $f;
	$event =~ s/\.wml/.$LANG.html/;
	$output .= "$event: $f\n";
    }
}

if (length ($ENGLISHDIR)) {
    opendir (DIR, $ENGLISHDIR) || die "Can't open english directory.\n";
    @files = sort (grep (/^.*-report\.wml$/,readdir(DIR)));
    closedir (DIR);

    for $f (@files) {
	$event = $f;
	$event =~ s/-report//;

	if (!exists $report{$event} && -r "$ENGLISHDIR/$event") {
	    $report{$event} = "$ENGLISHDIR/$f";
	    $event =~ s/\.wml/.$LANG.html/;
	    $output .= "$event: $ENGLISHDIR/$f\n";
	}
    }
}

if (length ($output)) {
    open (M, ">Makefile.reports") || die "Can't write to Makefile.reports, huh?";
    printf M "# Don't edit this file, automatically generated by make_makefile_reports.pl\n\n%s", $output;
    printf M "\nindex.%s.html: %s\n", $LANG, join (" ", values %report);
    close (M);
}
