#!/usr/local/bin/perl
# Add navigation "bars" containing a link for next and previous LINKs
# plus one for any url given in argument
#
# ToDo: compare URLs to find shortest relative path
#
# Arnaud Le Hors - lehors@w3.org
# Modified by Bert Bos <bert@w3.org>
# $Id: addnavbar,v 2.5 2006-10-09 18:55:52 ihickson Exp $

use Getopt::Long;
use lib 'bin';
use utils;

my $PROG = substr($0, rindex($0, "/") + 1);
my $USAGE = "Usage: $PROG [options] [file [output]]
\t-c=config   to give configuration file (default Project.cfg)
\t-r=realpath to give real name of file (default is file itself)
\t-top        to suppress navigation bar at the top of the page
\t-bottom     to suppress navigation bar at the bottom of the page
\t-hr         to suppress horizontal rules
\t-prev       to suppress link to previous page
\t-next       to suppress link to next page
\t-toc        to suppress link to table of contents
\t-omit=rel   to omit other relations in config file (can be repeated)\n";


### main

my %options;

GetOptions(\%options, 'top', 'bottom', 'hr', 'prev', 'next', 'toc',
	   'omit=s@', 'r=s', 'c=s') or die $USAGE;
if ($#ARGV >= 0) {$input = $ARGV[0]; shift;} else {$input = '-';}
if ($#ARGV >= 0) {$output = $ARGV[0]; shift;} else {$output = '-';}
if ($#ARGV >= 0) {die $USAGE;}

my $name = defined $options{'r'} ? $options{'r'} : $input;
my $configfile = defined $options{'c'} ? $options{'c'} : 'Project.cfg';

# Read configuration and find info about current file
read_config($configfile);
defined $lookup{$name} or die "$PROG: file $name not found in config file\n";
$chapno = $lookup{$name};

open(IN, $input) or die "$PROG: cannot read file $input\n";
open(OUT, ">$output") or die "$PROG: cannot write to file $output\n";
my $done = 0;
my $i;

# Build the navigation bar
my $navbar = "\n<div class=\"navbar\">\n<p>";
$navbar .= "<a href=\"$chapter[$chapno-1]\">previous</a> &nbsp;\n"
    unless $chapno < 1 or defined $options{'prev'};
$navbar .= "<a href=\"$chapter[$chapno+1]\">next</a> &nbsp;\n"
    unless $chapno >= $#chapter or defined $options{'next'};
$navbar .= "<a href=\"$contents\">contents</a> &nbsp;\n"
    unless !defined $contents or defined $options{'toc'};
for ($i = 0; $i <= $#links; $i++) {
    $navbar .= "<a href=\"$links[$i]\">$tonavbar[$i]</a> &nbsp;\n"
	unless $tonavbar[$i] eq '' or grep(/$relations[$i]/, $options{'omit'});
}
$navbar .= "</div>\n";

# Insert the navigation bar after the <BODY> tag. (There better be one!)
if (! defined $options{'top'}) {
    while (<IN>) {
	/<body[^>]*>/io and do {
	    print OUT $`;
	    print OUT $&;
	    print OUT $navbar;
	    print OUT "<hr class=\"navbar\">\n" unless defined $options{'hr'};
	    print OUT $';
	    $done++;
	    last;
	};
	print OUT;
    }
}

# Insert the navigation bar before the </BODY> tag
if (! defined $options{'bottom'}) {
    while (<IN>) {
	/<\/body[^>]*>/io and do {
	    print OUT $`;
	    print OUT "<hr class=\"navbar\">\n" unless defined $options{'hr'};
	    print OUT $navbar;
	    print OUT $&;
	    print OUT $';
	    $done++;
	    last;
	};
	print OUT;
    }
}

# Copy the rest of the file
while (<IN>) {
    print OUT;
}

if (!defined $options{'top'}) {$done--;}
if (!defined $options{'bottom'}) {$done--;}
if ($done < 0) {
    warn "$PROG: $input had no <BODY>/</BODY> tags; no navbar inserted\n";
}

# Finalize
close(IN);
close(OUT);
