:
#!/bin/sh -- # -*- perl -*-
#
# postprocessing of tex4ht-generated html files from the MuPAD
# documentation

eval 'exec perl -w -S $0 ${1+"$@"}'
    if 0;
# This is a NOP for perl, but shells parse line-oriented. Standard hack.
#
# step 1: read all files and remember the unique ids

use lib '/wiwianka/user/cube/share/lib/perl';

use File::Find;
use IO::File;
use HTML::Parser;

my %ids = ();
my $currfile;

# open tag
sub start {
#    my($self, $tagname, $attr, $attrseq, $origtext) = @_;
    my($tagname, $attr) = @_;
    
    return unless $tagname eq 'a';
    return unless $attr->{'id'};
    my $base = $_;
    $base =~ s|^.*/||;
    $base =~ s|_.*\.html$||i;
    $base =~ s|\d*\.html$||i;
    if(defined($ids{$attr->{'id'}})) {
	unless ($ids{$attr->{'id'}} eq $currfile) {
#	    warn "id $attr->{'id'} used twice!\n";
	    $ids{$attr->{'id'}} = '';
	}
    } else {
	$ids{$attr->{'id'}} = $currfile;
    }
    my $target = $base.'#'.$attr->{'id'};
    if(defined($ids{$target})) {
	unless ($ids{$target} eq $currfile) {
	    warn "id $attr->{'id'} used twice in $base!\n";
	    $ids{$target} = '';
	}
    } else {
	$ids{$target} = $currfile;
    }
}

my $p = HTML::Parser->new(start_h => [\&start, 'tagname, attr']);

sub parse_file {
    /\.html$/ or return;
    $currfile = $_;
    $p->parse_file($_);
}

File::Find::find(\&parse_file, @ARGV||'.');

# step 2: adjust all hrefs

# strip all "menubuttons" sections except the first
my $do_print = 1;
my $buttons_seen = 0;

sub check_comment {
    my $in = shift;
    if($in eq '<!-- start menubuttons -->') {
	$do_print = 0 if $buttons_seen;
	$buttons_seen = 1;
    }
    print $in if $do_print;
    $do_print = 1 if $in eq '<!-- end menubuttons -->';
}

my $title = 0;
sub print_text {
    return unless $do_print;
    my $text = shift;
    if ($title) {
	$text =~ s/&#8211;/-/g;
	$text =~ s/&#8220;/``/g;
	$text =~ s/&#8221;/''/g;
	$text =~ s/(?<!\\)({|})//g;
	$text =~ s/\\//g;
    }
    print $text;
}

my $base;
sub adjust_start {
    my ($tagname, $attr, $text) = @_;

    return unless $do_print;

    if ($tagname eq 'a' and 
	    defined($attr->{'href'})) {
	my $target = $attr->{'href'};
	my $lastpart = $target;
	$lastpart =~ s/(.*)#//;
	if ($ids{$target}) {
	    $text =~ s/$attr->{'href'}/$ids{$target}.'#'.$lastpart/e;
	} else {
	    if ($ids{$base.'#'.$target}) {
		$text =~ s/$attr->{'href'}/$ids{$base.'#'.$target}.'#'.$lastpart/e;
	    }
	}
    }
    if ($tagname eq 'title') {
	$title=1;
    }
    print $text;
}

sub adjust_end {
    my ($tag, $text) = @_;
    $title=0 if $tag eq 'title';
    print $text;
}

$p = HTML::Parser->new(api_version => 3,
		       handlers => {
			   default => [\&print_text, 'text'],
			   start   => [\&adjust_start, 'tagname, attr, text'],
			   end     => [\&adjust_end, 'tagname, text'],
			   comment => [\&check_comment, 'text'],
# for whatever reason tex4ht introduces junk here...
			   declaration => [sub {$do_print = 1; print shift}, 'text']});

sub convert_file {
    m/\.html$/ or return;

    $do_print = 0;
    $buttons_seen = 0;
# no button list at the top of the toc file
    $buttons_seen = 1 unless m/\d\.html/;

    rename($_, $_.'.orig') or die "can't rename $_: $!\n";

    $base = $_;
    $base =~ s|^.*/||;
    $base =~ s|\d*\.html$||i;

    open(NEW, '> '.$_) or die "can't open $_ for writing: $!\n";
    select NEW;
    $p->parse_file($_.'.orig');
    unlink $_.'.orig';
    select STDOUT;
}

File::Find::find(\&convert_file, @ARGV||'.');
