- fix Mason profiling to pass-through images (for graph/)
[freeside.git] / bin / masonize
1 #!/usr/bin/perl
2
3 foreach $file ( split(/\n/, `find . -depth -print | grep cgi\$`) ) {
4   open(F,$file) or die "can't open $file for reading: $!";
5   @file = <F>;
6   #print "$file ". scalar(@file). "\n";
7   close $file;
8   system("chmod u+w $file");
9   open(W,">$file") or die "can't open $file for writing: $!";
10   select W; $| = 1; select STDOUT;
11   $newline = ''; #avoid prepending extraneous newlines
12   $all = join('',@file);
13
14   $mode = 'html';
15   while ( length($all) ) {
16
17     if ( $mode eq 'html' ) {
18
19       if ( $all =~ /^(.+?)(<%=?.*)$/s && $1 !~ /<%/s ) {
20         print W $1;
21         $all = $2;
22         next;
23       } elsif ( $all =~ /^<%=(.*)$/s ) {
24         print W '<%';
25         $all = $1;
26         $mode = 'perlv';
27         #die;
28         next;
29       } elsif ( $all =~ /^<%(.*)$/s ) {
30         print W $newline; $newline = "\n";
31         $all = $1;
32         $mode = 'perlc';
33         next;
34       } elsif ( $all !~ /<%/s ) {
35         print W $all;
36         last;
37       } else {
38         warn length($all); die;
39       }
40       die;
41
42     } elsif ( $mode eq 'perlv' ) {
43
44       if ( $all =~ /^(.*?%>)(.*)$/s ) {
45         print W $1;
46         $all=$2;
47         $mode = 'html';
48         next;
49       }
50       die 'unterminated <%= ???';
51
52     } elsif ( $mode eq 'perlc' ) {
53
54       if ( $all =~ /^([^\n]*?)%>(.*)$/s ) {
55         print W "%$1\n";
56         $all=$2;
57         $mode='html';
58         next;
59       }
60       if ( $all =~ /^([^\n]*)\n(.*)$/s ) {
61         print W "%$1\n";
62         $all=$2;
63         next;
64       }
65
66     } else { die };
67
68   }
69
70   close W;
71 }