Fix /usr/bin/env shebang
[project-aon.git] / common / scripts / gbtosvg.pl
1 #!/usr/bin/env perl
2 #
3 # gbtosvg.pl
4
5 use strict;
6 use warnings;
7
8 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
9
10 my $PROGRAM_NAME    = 'gbtodot';
11 my $USAGE           = "$PROGRAM_NAME [options] book-code\n\t--meta=[metadata file]\n\t--xml=[book XML]\n\t--xsl=[XSL transformation]\n\t--baseURI=[URI of linked XHTML book]\n\t--language=[language area of input data (output determined by meta file)]\n\t--verbose\n";
12
13 my $FILENAME_SEPARATOR = '/';
14
15 my $RXP        = '/usr/bin/rxp';
16 my $JAVA       = '/usr/bin/java';
17 my $XALAN_JAR  = '/usr/share/java/xalan2.jar';
18 my $DOT        = '/usr/local/bin/dot';
19
20
21 # Check that all the binaries are were want them
22
23 my @BINARIES;
24 push @BINARIES, ($RXP, $JAVA, $XALAN_JAR, $DOT);
25
26 foreach (@BINARIES) {
27     if ( ! -e $_ ) {
28             die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
29     }
30 }
31
32
33 ###
34
35 my $bookCode     = '';
36 my $metaFile     = '';
37 my $bookXML      = '';
38 my $dotXSL       = 'common/xsl/dot.xsl';
39 my $language     = 'en';
40 my $baseURI      = '';
41
42 my $verbose = 0;
43
44 while( $#ARGV > -1 ) {
45     my $cmdLineItem = shift @ARGV;
46     if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
47         $metaFile = $1;
48     }
49     elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
50         $bookXML = $1;
51     }
52     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
53         $dotXSL = $1;
54     }
55     elsif( $cmdLineItem =~ /^--base-uri=(.+)$/ ) {
56         $baseURI = $1;
57     }
58     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
59         $language = $1;
60     }
61     elsif( $cmdLineItem =~ /^--verbose/ ) {
62         $verbose = 1;
63     }
64     else { 
65         $bookCode = $cmdLineItem;
66     }
67 }
68
69 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
70 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
71 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
72
73 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
74     open( META, '<', $metaFile ) 
75         or die qq{Unable to open metadata file ($metaFile): $!\n};
76 }
77 else { 
78     die qq{Improper metadata file ($metaFile)\n}; 
79 }
80
81 my $meta = '';
82 while( my $line = <META> ) {
83     $meta .= $line if $line !~ /^[[:space:]]*#/;
84 }
85 close META;
86
87 my $rulesString = '';
88 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
89     $rulesString = $1;
90 }
91 else {
92     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
93 }
94
95 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
96 my %rulesHash;
97 foreach my $rule (@rules) {
98     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
99         $rulesHash{ $1 } = $2;
100     }
101     else {
102         die "Unrecognized rule syntax:\n$rule\n";
103     }
104 }
105
106 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
107     $bookXML = $1;
108     if( -e $bookXML && -f $bookXML && -r $bookXML ) {
109         system( $RXP, '-Vs', $bookXML ) == 0 
110             or die( "XML validation failed\n" );
111     }
112
113     unless( defined $rulesHash{'book-series'} ) { 
114         die "Metadata file leaves book series unspecified\n";
115     }
116     unless( defined $rulesHash{'language'} ) { 
117         die "Metadata file leaves language unspecified\n";
118     }
119
120     my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'dot' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
121     &make_path( $outPath );
122     unless( -e $outPath && -d $outPath ) {
123             die "Unknown error creating output directory\n";
124     }
125
126     $baseURI = "http://www.projectaon.org/$rulesHash{'language'}/xhtml/$rulesHash{'book-series'}/$bookCode/" if( $baseURI eq '' );
127     my $dotPath = "$outPath$FILENAME_SEPARATOR$bookCode.dot";
128     print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "$bookXML" -XSL "$dotXSL" -OUT "$dotPath" -PARAM base-URI "$baseURI" -PARAM language "$rulesHash{'language'}"};
129
130     my $svgPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'svg' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
131     &make_path( $svgPath );
132     unless( -e $svgPath && -d $svgPath ) {
133             die "Unknown error creating output directory\n";
134     }
135     $svgPath .= "$FILENAME_SEPARATOR$bookCode.svgz";
136     print qx{$DOT -Tsvgz -o $svgPath $dotPath};
137 }
138
139 print "Success\n" if $verbose;
140
141 ###
142
143 sub make_path {
144     my ( $path ) = ( @_ );
145
146         my @dirs = split ( /$FILENAME_SEPARATOR/, $path );
147         my $dirPath = '';
148         for( my $i = 0; $i <= $#dirs; ++$i ) {
149             $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
150             if( -e $dirPath && ! -d $dirPath ) { 
151             die "Output directory name exists and is not a directory\n";
152         }
153             unless( -e $dirPath ) {
154             mkdir $dirPath 
155                 or die( "Unable to create output directory ($path): $!\n" );
156             }
157         }
158 }