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