Check that all binaries needed are installed before running
[project-aon.git] / common / scripts / gbtoxhtml.pl
1 #!/usr/bin/perl -w
2 #
3 # gbtoxhtml.pl
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';
12 my $USAGE           = "$PROGRAM_NAME [options] book-code\n\t--meta=[metadata file]\n\t--xml=[book XML]\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 my $CP         = '/bin/cp';
18 my $MV         = '/bin/mv';
19 my $TAR        = '/bin/tar';
20 my $ZIP        = '/usr/bin/zip';
21 my $BZIP2      = '/bin/bzip2';
22 my $JAVA       = '/usr/bin/java';
23 my $XALAN_JAR  = '/usr/share/java/xalan2.jar';
24 my $RM         = '/bin/rm';
25 my $CHMOD      = '/bin/chmod';
26
27 # Check that all the binaries are were want them
28
29 my @BINARIES;
30 push @BINARIES, ($RXP, $CP, $MV, $TAR, $ZIP, $BZIP2, $JAVA, $XALAN_JAR, $RM, $CHMOD);
31
32 foreach (@BINARIES) {
33     if ( ! -e $_ ) {
34         die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
35     }
36 }
37
38 ###
39
40 my $bookCode     = '';
41 my $metaFile     = '';
42 my $bookXML      = '';
43 my $xhtmlXSL     = 'common/xsl/xhtml.xsl';
44 my $language     = 'en';
45
46 my $verbose = 0;
47
48 while( $#ARGV > -1 ) {
49     my $cmdLineItem = shift @ARGV;
50     if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
51         $metaFile = $1;
52     }
53     elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
54         $bookXML = $1;
55     }
56     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
57         $xhtmlXSL = $1;
58     }
59     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
60         $language = $1;
61     }
62     elsif( $cmdLineItem =~ /^--verbose/ ) {
63         $verbose = 1;
64     }
65     else { 
66         $bookCode = $cmdLineItem;
67     }
68 }
69
70 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
71 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
72 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
73 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
74
75 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
76     open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
77 }
78 else { die qq{Improper metadata file ($metaFile)\n}; }
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     unless( -e $bookXML && -f $bookXML && -r $bookXML ) {
108         die "XML does not exist or is not readable\n";
109     }
110     if( -e $RXP ) {
111             system( $RXP, '-Vs', $bookXML ) == 0
112             or die "XML validation failed\n";
113     }
114     else {
115         warn "XML Validator ($RXP) not installed - validate before publication\n";
116     }
117
118     unless( defined $rulesHash{'language'} ) { die "Metadata file leaves language unspecified\n"; }
119     unless( defined $rulesHash{'book-series'} ) { die "Metadata file leaves book series unspecified\n"; }
120     unless( defined $rulesHash{'images'} ) { die "Metadata file leaves image directories unspecified\n"; }
121     unless( defined $rulesHash{'csst'} ) { die "Metadata file leaves CSS templates unspecified\n"; }
122
123     my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'xhtml' . $FILENAME_SEPARATOR . $rulesHash{'book-series'} .$FILENAME_SEPARATOR . $bookCode;
124     unless( -e $outPath && -d $outPath ) {
125         my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
126         my $dirPath = '';
127         for( my $i = 0; $i <= $#dirs; ++$i ) {
128             $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
129             if( -e $dirPath && ! -d $dirPath ) { die "Output directory name exists and is not a directory\n"; }
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     print qx{$RM ${bookPath}*} if -e $bookPath."/toc.htm";
141     print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "$bookXML" -XSL "$xhtmlXSL" -OUT "${bookPath}foo.xml" -PARAM background-color "$rulesHash{'background-color'}" -PARAM text-color "$rulesHash{'text-color'}" -PARAM link-color "$rulesHash{'link-color'}" -PARAM use-illustrators "$rulesHash{'use-illustrators'}" -PARAM language "$rulesHash{'language'}"};
142     print qx{$RM ${bookPath}foo.xml};
143
144     foreach my $cssTemplate (split( /:/, $rulesHash{'csst'} )) {
145         $cssTemplate =~ m/([^${FILENAME_SEPARATOR}]+)t$/;
146         my $templateFilename = $1;
147         open( TEMPLATE, '<', $cssTemplate ) or die "Unable to open CSS template file ($cssTemplate): $!\n";
148         open( STYLESHEET, '>', "$bookPath${templateFilename}" ) or die "Unable to open stylesheet file ($bookPath${templateFilename}) for writing: $!\n";
149         while( my $templateLine = <TEMPLATE> ) {
150             while( $templateLine =~ /%%([^%[:space:]]+)%%/ ) {
151                 my $name = $1;
152                 $templateLine =~ s/%%${name}%%/$rulesHash{$name}/g;
153             }
154             print STYLESHEET $templateLine;
155         }
156     }
157     close TEMPLATE;
158     close STYLESHEET;
159
160     foreach my $imagePath (split( /:/, $rulesHash{'images'} )) {
161         unless( -e $imagePath && -d $imagePath ) {
162             die "Image path ($imagePath) does not exist or is not a directory\n";
163         }
164         print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
165     }
166
167     #print qx{$TAR cf ${bookCode}.tar ${bookPath}*};
168     print qx{$ZIP -8 -q ${bookCode}.zip ${bookPath}*};
169     #print qx{$BZIP2 -9 ${bookCode}.tar};
170     print qx{$MV ${bookCode}* $bookPath};
171 }
172
173 print "Success\n" if $verbose;