reorganizing the repository
[project-aon.git] / common / scripts / gbtoxhtml-simple.pl
1 #!/usr/bin/perl -w
2 #
3 # gbtoxhtml-simple.xsl
4 #
5 ######################################################################
6
7 use strict;
8
9 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
10
11 my $PROGRAM_NAME    = 'gbtoxhtml-simple.pl';
12 my $USAGE           = "$PROGRAM_NAME [options] book-code\n\t--xml=[book XML]\n\t--meta=[metadata file]\n\t--xsl=[XSL transformation]\n\t--language=[language area of input data (output determined by meta file)]\n\t--verbose\n";
13
14 my $FILENAME_SEPARATOR = '/';
15
16 my $RXP        = '/usr/bin/rxp';
17 unless( -e $RXP && -x $RXP ) { 
18     # try somewhere else
19     $RXP = '/usr/local/bin/rxp';
20 }
21 my $ZIP        = '/usr/bin/zip -q';
22 my $JAVA       = '/usr/bin/java';
23 # latest binary download names the relevant jar "xalan.jar"
24 # older installations may have "xalan2.jar"
25 my $XALAN_JAR  = '/usr/share/java/xalan.jar';
26 unless( -e $XALAN_JAR ) { 
27     # try somewhere else
28     $XALAN_JAR = '/usr/share/java/xalan2.jar';
29 }
30
31 ###
32
33 my $bookCode     = '';
34 my $bookXML      = '';
35 my $metaFile     = '';
36 my $xhtmlXSL     = 'common/xsl/xhtml-simple.xsl';
37 my $language     = 'en';
38
39 my $verbose = 0;
40
41 while( $#ARGV > -1 ) {
42     my $cmdLineItem = shift @ARGV;
43     if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
44         $bookXML = $1;
45     }
46     elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
47         $metaFile = $1;
48     }
49     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
50         $xhtmlXSL = $1;
51     }
52     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
53         $language = $1;
54     }
55     elsif( $cmdLineItem =~ /^--verbose/ ) {
56         $verbose = 1;
57     }
58     else { 
59         $bookCode = $cmdLineItem;
60     }
61 }
62
63 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
64 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
65 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
66 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
67
68 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
69     open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
70 }
71 else { die qq{Improper metadata file ($metaFile)\n}; }
72
73 my $meta = '';
74 while( my $line = <META> ) {
75     $meta .= $line if $line !~ /^[[:space:]]*#/;
76 }
77 close META;
78
79 my $rulesString = '';
80 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
81     $rulesString = $1;
82 }
83 else {
84     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
85 }
86
87 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
88 my %rulesHash;
89 foreach my $rule (@rules) {
90     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
91         $rulesHash{ $1 } = $2;
92     }
93     else {
94         die "Unrecognized rule syntax:\n$rule\n";
95     }
96 }
97
98 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
99     $bookXML = $1;
100     if( -e $bookXML && -f $bookXML && -r $bookXML ) {
101         system( $RXP, '-Vs', $bookXML ) == 0 or die( "XML validation failed\n" );
102     }
103     unless( defined $rulesHash{'language'} ) { die "Metadata file leaves language unspecified\n"; }
104     unless( defined $rulesHash{'book-series'} ) { die "Metadata file leaves book series unspecified\n"; }
105
106     my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'xhtml-simple' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
107     unless( -e $outPath && -d $outPath ) {
108         my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
109         my $dirPath = '';
110         for( my $i = 0; $i <= $#dirs; ++$i ) {
111             $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
112             if( -e $dirPath && ! -d $dirPath ) { die "Output directory name exists and is not a directory\n"; }
113             unless( -e $dirPath ) {
114                 mkdir $dirPath or die( "Unable to create output directory ($outPath): $!\n" );
115             }
116         }
117     }
118     unless( -e $outPath && -d $outPath ) {
119         die "Unknown error creating output directory\n";
120     }
121
122     my $bookPath = "$outPath${FILENAME_SEPARATOR}${bookCode}";
123
124     print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "${bookXML}" -XSL "${xhtmlXSL}" -OUT "${bookPath}.htm" -PARAM use-illustrators "$rulesHash{'use-illustrators'}"};
125     print qx{$ZIP $bookPath.zip $bookPath.htm};
126 }
127
128 print "Success\n" if $verbose;