#!/usr/bin/perl -w
# (c) 2003 by Gerfried Fuchs <alfie@ist.org>
#
# splits the current users pages into small translateable and manageable files.

use strict;

my $original = '1.122';
my $original_plus_1 = '1.123';

open (R, "<index.wml")          || die "$0: can't read index.wml: $!\n";
open (W, ">index-splitted.wml") || die "$0: can't write index-splitted.wml: $!\n";

mkdir 'edu' || die "$0: can't create dir edu: $!\n";
mkdir 'com' || die "$0: can't create dir com: $!\n";
mkdir 'org' || die "$0: can't create dir org: $!\n";
mkdir 'gov' || die "$0: can't create dir gov: $!\n";

my ($section, $entry, $revision) = ();

my @tocs = ('edu', 'com', 'org', 'gov');
my @sections = ('edu', 'com', 'org', 'gov');

while (<R>) {
    if ( m|#include "\$\(ENGLISHDIR\)/users/index\.include"| ) {
        print W <<"EOF";
#include wml::debian::toc TOCNAME=EDU
#include wml::debian::toc TOCNAME=COM
#include wml::debian::toc TOCNAME=ORG
#include wml::debian::toc TOCNAME=GOV
#include "\$(ENGLISHDIR)/users/index-splitted.include" TOCNAME=EDU
#include "\$(ENGLISHDIR)/users/index-splitted.include" TOCNAME=COM
#include "\$(ENGLISHDIR)/users/index-splitted.include" TOCNAME=ORG
#include "\$(ENGLISHDIR)/users/index-splitted.include" TOCNAME=GOV
EOF
        next;
    }

    if ( m|#use wml::debian::translation-check .*translation="([^"]+)"| ) {
        $revision = $_;
        my $rev = ( $1 eq $original ) ? '1.1' : '1.0'; 
        $revision =~ s/translation="$1"/translation="$rev"/;
        s/translation="$original"/translation="$original_plus_1"/ if $rev eq '1.1';
    }

    print W $_ if !defined $section;

    if ( m#<h2>[^<]+</h2># && $#tocs >= 0 ) {
        my $toc = shift @tocs;
        print W <<"EOF";
<toc-display\U$toc\E />
EOF
        while ( <R> !~ m#</ul># ) { }
        next;

    } elsif ( m#<h3>[^<]+</h3># ) {
        $section = shift @sections;
        print "  Now in section `$section'\n";
        print W <<"EOF";

#   English items
#include '\$(ENGLISHDIR)/users/$section/*.wml' IPP_SORT=name TOCNAME=\U$section\E
#   Overwrite localized items
#include '$section/*.wml' IPP_SORT=name

EOF
        next;

    } elsif ( m#<hr ?/?># ) {
        print W $_ if defined $section;
        $section = undef;
        next;

    }

    next unless defined $section;

    ## here we have to extract the entries
    $entry .= $_;

    if ( m#</userdetail># ) {
        ## end of entry, write it out

        my ($name, $desc, $url) = $entry =~ m#<userdetail +"([^"]+)" +"([^"]+)"(?: +"([^"]+)")?>#s;

        $entry =~ s#^[ \t\n]+##;
        $entry =~ s#\n[ \t\n]+$##;
        $entry =~ s#(</?userdetail)#$1\$(TOCNAME)#g;
        $entry =~ s#^#$revision\n# if defined $revision;

        if ( defined $name ) {
            open (ENTRY, ">$section/$name.wml")
                || die "$0: can't write to $section/$name.wml: $!\n";
            print ENTRY $entry;
            close (ENTRY);

        } else {
            print STDERR "   NO MATCH for userdetail in >>${entry}<<!\n\n";
        }

        $entry = undef;
    }
}

close (W);
# rename ('index-splitted.wml', 'index.wml');
