#!/gnu/store/f67gswyh29xbqind9q29h8975a21j64a-profile/bin/perl -w
# namespace-markup.pl

use strict;
use Getopt::Long;

my %options = ();
my %namespaces = ();
my $help;
GetOptions ("namespace|n=s" => \%namespaces,
            "help" => \$help);


if (@ARGV == 0 || $help) {
    print STDERR "usage: namespace-markup [-n basename=namespace] [-n ...] file ...\n";
    print STDERR << 'END';

Creates a namespace in noweb files such that for a file named, say,
'A.nw' all references inside it like 'Function' will be re-labeled
'A:Function'.  However, say I have an appendix file 'A-appendix.nw'
that I want to still be in the same namespace so that when it
references 'Function' it will reference 'A:Function' and not
'A-appendix:Function'.  I can manually specify the namespace through
the arguments of this command that the basename of the filename
'A-appendix' is actually in the 'A' namespace by using the following
option -n 'A-appendix=A'.

END
    exit(2);
}
my $file = 'not-defined';
$namespaces{$file} = 'not-defined';
local *INPUT;
open(INPUT, "/gnu/store/mlcp3sy4hi3n6p9wamh54p4hpmrvyql5-profile/bin/../lib/markup " . join(' ', @ARGV) . " |") or die("noweb/markup failed.\n");
while (my $line = <INPUT>) {
    if ($line =~ /^\@file ([^\.:]+)[^:]*$/) { 
        $file = $1; 
        if (! (defined $namespaces{$file})) { 
            $namespaces{$file} = $file; 
        } 
    } 
    if ($line =~ /^\@(defn|use) ([^+][^:]*)$/) {
        # I keep getting warnings here about an uninitialized value
        # that I can't seem to sort out.
#        no warnings 'uninitialized';
        $line =~ s/^\@(defn|use) ([^+][^:]*)$/\@$1 $namespaces{$file}:$2/;
    }
    print $line;
}

