1#!/usr/local/bin/perl5
2
3# Filename: mkpod
4#
5# Author:	Paul Marquess
6
7# File types
8#
9#    Macro files end with .M
10#    Tagged source files end with .T
11#    Output from the code ends with .O
12#    Pre-Pod file ends with .P
13#    
14# Tags
15#
16#    ## BEGIN tagname
17#     ...
18#    ## END tagname
19#
20#    ## 0
21#    ## 1
22#
23
24# Constants
25
26$TOKEN = '##' ;
27$Verbose = 1 if $ARGV[0] =~ /^-v/i ;
28
29# Macros files first
30foreach $file (glob("*.M"))
31{
32    open (F, "<$file") or die "Cannot open '$file':$!\n" ;
33    print "    Processing Macro file $file\n"  ;
34    while (<F>)
35    {
36        # Skip blank & comment lines
37        next if /^\s*$/ || /^\s*#/ ;
38	
39	# 
40	($name, $expand) = split (/\t+/, $_, 2) ;
41
42	$expand =~ s/^\s*// ;
43        $expand =~ s/\s*$// ;
44
45	if ($expand =~ /\[#/ )
46	{
47	}
48
49	$Macros{$name} = $expand ;
50    }
51    close F ;
52}
53
54# Suck up all the code files
55foreach $file (glob("t/*.T"))
56{
57    ($newfile = $file) =~ s/\.T$// ;
58    open (F, "<$file") or die "Cannot open '$file':$!\n" ;
59    open (N, ">$newfile") or die "Cannot open '$newfile':$!\n" ;
60
61    print "    Processing $file -> $newfile\n"  ;
62
63    while ($line = <F>)
64    {
65        if ($line =~ /^$TOKEN\s*BEGIN\s+(\w+)\s*$/ or
66            $line =~ m[\s*/\*$TOKEN\s*BEGIN\s+(\w+)\s*$] )
67        {
68	    print "    Section $1 begins\n" if $Verbose ;
69	    $InSection{$1} ++ ;
70	    $Section{$1} = '' unless $Section{$1} ;
71        }
72        elsif ($line =~ /^$TOKEN\s*END\s+(\w+)\s*$/ or
73               $line =~ m[^\s*/\*$TOKEN\s*END\s+(\w+)\s*$] )
74        {
75	    warn "Encountered END without a begin [$line]\n"
76		unless $InSection{$1} ;
77
78	    delete $InSection{$1}  ;
79	    print "    Section $1 ends\n" if $Verbose ;
80        }
81        else
82        {
83	    print N $line ;
84	    chop $line ;
85	    $line =~ s/\s*$// ;
86
87	    # Save the current line in each of the sections
88	    foreach( keys %InSection)
89	    {
90		if ($line !~ /^\s*$/ )
91	          #{ $Section{$_} .= "    $line" }
92	          { $Section{$_} .= $line }
93	        $Section{$_} .= "\n" ;
94	    }
95        }
96
97    }
98
99    if (%InSection)
100    {
101        # Check for unclosed sections
102	print "The following Sections are not terminated\n" ;
103        foreach (sort keys %InSection)
104          { print "\t$_\n" }
105	exit 1 ;
106    }
107
108    close F ;
109    close N ;
110}
111
112print "\n\nCreating pod file(s)\n\n" if $Verbose ;
113
114@ppods = glob('*.P') ;
115#$ppod = $ARGV[0] ;
116#$pod = $ARGV[1] ;
117
118# Now process the pre-pod file
119foreach $ppod (@ppods)
120{
121    ($pod = $ppod) =~ s/\.P$// ;
122    open (PPOD, "<$ppod") or die "Cannot open file '$ppod': $!\n" ;
123    open (POD, ">$pod") or die "Cannot open file '$pod': $!\n" ;
124
125    print "    $ppod -> $pod\n" ;
126
127    while ($line = <PPOD>)
128    {
129        if ( $line =~ /^\s*$TOKEN\s*(\w+)\s*$/)
130        {
131            warn "No code insert '$1' available\n"
132	        unless $Section{$1} ;
133    
134	    print "Expanding section $1\n" if $Verbose ;
135	    print POD $Section{$1} ;
136        }
137        else
138        {
139#	    $line =~ s/\[#([^\]])]/$Macros{$1}/ge ;
140	    print POD $line ;
141        }
142    }
143    
144    close PPOD ;
145    close POD ;
146}
147