Correct misspellings
[project-aon.git] / common / scripts / gbtoxhtml-less-simple.pl
1 #!/usr/bin/perl -w
2 #
3 # gbtoxhtml-less-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-less-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 $CP         = '/bin/cp';
17 my $MV         = '/bin/mv';
18 my $RM         = '/bin/rm';
19 my $RXP        = '/usr/bin/rxp';
20 my $ZIP        = '/usr/bin/zip';
21 my $JAVA       = '/usr/bin/java';
22 my $XALAN_JAR  = '/usr/share/java/xalan2.jar';
23
24 # Check that all the binaries are were want them
25
26 my @BINARIES;
27 push @BINARIES, ($RXP, $CP, $MV, $ZIP, $JAVA, $XALAN_JAR, $RM);
28
29 foreach (@BINARIES) {
30     if ( ! -e $_ ) {
31             die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
32     }
33 }
34
35 ###
36
37 my $bookCode     = '';
38 my $bookXML      = '';
39 my $metaFile     = '';
40 my $xhtmlXSL     = 'common/xsl/xhtml-less-simple.xsl';
41 my $language     = 'en';
42
43 my $verbose = 0;
44
45 while( $#ARGV > -1 ) {
46     my $cmdLineItem = shift @ARGV;
47     if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
48         $bookXML = $1;
49     }
50     elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
51         $metaFile = $1;
52     }
53     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
54         $xhtmlXSL = $1;
55     }
56     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
57         $language = $1;
58     }
59     elsif( $cmdLineItem =~ /^--verbose/ ) {
60         $verbose = 1;
61     }
62     else { 
63         $bookCode = $cmdLineItem;
64     }
65 }
66
67 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
68 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/simple"; }
69 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
70 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
71
72 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
73     open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
74 }
75 else { die qq{Improper metadata file ($metaFile)\n}; }
76
77 my $meta = '';
78 while( my $line = <META> ) {
79     $meta .= $line if $line !~ /^[[:space:]]*#/;
80 }
81 close META;
82
83 my $rulesString = '';
84 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
85     $rulesString = $1;
86 }
87 else {
88     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
89 }
90
91 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
92 my %rulesHash;
93 foreach my $rule (@rules) {
94     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
95         $rulesHash{ $1 } = $2;
96     }
97     else {
98         die "Unrecognized rule syntax:\n$rule\n";
99     }
100 }
101
102 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
103     $bookXML = $1;
104     if( -e $bookXML && -f $bookXML && -r $bookXML ) {
105             system( $RXP, '-Vs', $bookXML ) == 0
106             or die( "XML validation failed\n" );
107     }
108     unless( defined $rulesHash{'language'} ) {
109         die "Metadata file leaves language unspecified\n";
110     }
111     unless( defined $rulesHash{'book-series'} ) {
112         die "Metadata file leaves book series unspecified\n";
113     }
114     unless( defined $rulesHash{'images'} ) { 
115         die "Metadata file leaves image directories unspecified\n";
116     }
117
118     my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR .
119                  'xhtml-less-simple' . $FILENAME_SEPARATOR .
120                  $rulesHash{'book-series'} . $FILENAME_SEPARATOR .
121                  $bookCode;
122     unless( -e $outPath && -d $outPath ) {
123             my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
124         my $dirPath = '';
125             for( my $i = 0; $i <= $#dirs; ++$i ) {
126                 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
127             if( -e $dirPath && ! -d $dirPath ) {
128                 die "Output directory name exists and is not a directory\n";
129             }
130                 unless( -e $dirPath ) {
131                         mkdir $dirPath or die( "Unable to create output directory ($outPath): $!\n" );
132             }
133             }
134     }
135     unless( -e $outPath && -d $outPath ) {
136             die "Unknown error creating output directory\n";
137     }
138
139     my $bookPath = "$outPath${FILENAME_SEPARATOR}";
140
141     print qx{$RM ${bookPath}*} if -e $bookPath."/title.htm";
142     print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "${bookXML}" -XSL "${xhtmlXSL}" -OUT "${bookPath}title.htm" -PARAM use-illustrators "$rulesHash{'use-illustrators'}"};
143
144     foreach my $imagePath (split( /:/, $rulesHash{'images'} )) {
145         unless( -e $imagePath && -d $imagePath ) {
146                 die "Image path ($imagePath) does not exist or is not a directory\n";
147             }
148         print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
149     }
150
151     print qx{$ZIP -8 -q ${bookCode}.zip ${bookPath}*};
152     print qx{$MV ${bookCode}* $bookPath};
153 }
154
155 print "Success\n" if $verbose;