Committing some of Javier's changes.
[project-aon.git] / scripts / gbtolatex.pl
1 #!/bin/perl -w
2 #
3 # gbtolatex.pl
4 # 10 April 2002
5 #
6 # Creates LaTeX gamebook from XML source. This should subsequently be
7 # used to create a PDF or PostScript version.
8 #####
9
10 use strict;
11
12 my $PROGRAM_NAME      = "gbtolatex";
13 my $XML_PATH          = "xml";
14 my $XML_SOURCE        = "";
15 my $PWD               = `pwd`;
16 chomp($PWD);
17 my $BOOK_PATH         = $PWD."/latex";
18 my $TITLE_COLOR       = "";
19 my $USE_ILLUSTRATORS  = "";
20
21 # Programs
22 #
23 my $XMLPROC = "xalan";
24 # my $XMLPROC = "/usr/local/bin/xmlto";
25
26 # Check AONPATH
27 my $DATADIR=$ENV{'AONDATA'} || $ENV{'AONPATH'}."/data";
28 if ( ! -d "$DATADIR" ) {
29         if ( ! defined($ENV{'AONPATH'}) && ! defined ($ENV{'AONDATA'}) ) {
30                 print STDERR "AONPATH environment variable not set, it should be defined to\n";
31                 print STDERR "wherever the AON files are.\n";
32         } else {
33                 print STDERR "Cannot find $DATADIR !\n";
34         }
35         exit 1;
36 }
37 ##
38
39 unless( $ARGV[ 0 ] ) { die "Usage:\n\t${PROGRAM_NAME} book-code [LANGUAGE]\n"; }
40
41 print "Reminder:\n\tDid you uncomment the LaTeX special character\n\tdeclarations in the book's XML file?\n";
42
43 my $bookCode = $ARGV[ 0 ];
44 my $language = $ARGV[ 1 ] || "";
45
46 # TODO:
47 # - convert the bookcode's if then else to a hash array
48 # - allow usage of unknown book codes
49
50 if( $bookCode eq "01fftd" ) {
51     $XML_SOURCE        = "01fftd.xml";
52     $BOOK_PATH         .= "/lw/01fftd";
53     $TITLE_COLOR       = "0.0,0.4,0.2";
54     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
55 }
56 elsif( $bookCode eq "02fotw" ) {
57     $XML_SOURCE        = "02fotw.xml";
58     $BOOK_PATH         .= "/lw/02fotw";
59     $TITLE_COLOR       = "0.0,0.6,0.6";
60     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
61 }
62 elsif( $bookCode eq "03tcok" ) {
63     $XML_SOURCE        = "03tcok.xml";
64     $BOOK_PATH         .= "/lw/03tcok";
65     $TITLE_COLOR       = "0.0,0.6,0.8";
66     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
67 }
68 elsif( $bookCode eq "04tcod" ) {
69     $XML_SOURCE        = "04tcod.xml";
70     $BOOK_PATH         .= "/lw/04tcod";
71     $TITLE_COLOR       = "0.0,0.0,0.6";
72     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
73 }
74 elsif( $bookCode eq "05sots" ) {
75     $XML_SOURCE        = "05sots.xml";
76     $BOOK_PATH         .= "/lw/05sots";
77     $TITLE_COLOR       = "0.8,0.6,0.0";
78     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
79 }
80 elsif( $bookCode eq "06tkot" ) {
81     $XML_SOURCE        = "06tkot.xml";
82     $BOOK_PATH         .= "/lw/06tkot";
83     $TITLE_COLOR       = "0.6,0.6,0.0";
84     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
85 }
86 elsif( $bookCode eq "07cd" ) {
87     $XML_SOURCE        = "07cd.xml";
88     $BOOK_PATH         .= "/lw/07cd";
89     $TITLE_COLOR       = "0.0,0.8,0.4";
90     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
91 }
92 elsif( $bookCode eq "08tjoh" ) {
93     $XML_SOURCE        = "08tjoh.xml";
94     $BOOK_PATH         .= "/lw/08tjoh";
95     $TITLE_COLOR       = "0.4,0.6,0.4";
96     $USE_ILLUSTRATORS  = ":Gary Chalk:JC Alvarez & Jonathan Blake:Jonathan Blake:";
97 }
98 elsif( $bookCode eq "09tcof" ) {
99     $XML_SOURCE        = "09tcof.xml";
100     $BOOK_PATH         .= "/lw/09tcof";
101     $TITLE_COLOR       = "1.0,0.6,0.0";
102     $USE_ILLUSTRATORS  = ":Brian Williams:JC Alvarez & Jonathan Blake:Jonathan Blake:";
103 }
104 elsif( $bookCode eq "10tdot" ) {
105     $XML_SOURCE        = "10tdot.xml";
106     $BOOK_PATH         .= "/lw/10tdot";
107     $TITLE_COLOR       = "1.0,0.0,0.0";
108     $USE_ILLUSTRATORS  = ":Brian Williams:JC Alvarez & Jonathan Blake:Jonathan Blake:";
109 }
110 elsif( $bookCode eq "11tpot" ) {
111     $XML_SOURCE        = "11tpot.xml";
112     $BOOK_PATH         .= "/lw/11tpot";
113     $TITLE_COLOR       = "0.5,0.5,0.4";
114     $USE_ILLUSTRATORS  = ":Brian Williams:JC Alvarez & Jonathan Blake:Jonathan Blake:";
115 }
116 elsif( $bookCode eq "12tmod" ) {
117     $XML_SOURCE        = "12tmod.xml";
118     $BOOK_PATH         .= "/lw/12tmod";
119     $TITLE_COLOR       = "0.6,0.0,0.0";
120     $USE_ILLUSTRATORS  = ":Brian Williams:JC Alvarez & Jonathan Blake:Jonathan Blake:";
121 }
122 else{ die "Error:\n\tUknown book code.\n"; }
123
124 chdir( "$DATADIR" ) or die( "Cannot open Project Aon data directory \"$DATADIR\": $!" );
125
126 # If there is a LANGUAGE set use it
127 if ( $language ne "" ) {
128 # Languages are prefixes to the booknames in the files
129         $XML_SOURCE =~ s/\.xml$/\.${language}.xml/;
130 }
131
132 # Check that the XML file is there
133 if ( ! -r $DATADIR."/".$XML_SOURCE ) {
134         die "Could not find source file $XML_SOURCE in $DATADIR!";
135 }
136 if ( ! -r $DATADIR."/latex.xsl" ) {
137         die "Could not find LaTeX stylesheet (latex.xsl) in $DATADIR!";
138 }
139
140
141 # Create the output directory if it does not exist already
142 print "Checking directory $BOOK_PATH...";
143 if ( ! -d "$BOOK_PATH" ) {
144         print "...creating";
145         `mkdir -p "$BOOK_PATH"` || die "Could not create output directory $BOOK_PATH: $!";
146 }
147 print "..done.\n";
148
149 my $OUTPUTFILE="$BOOK_PATH/$bookCode.tex";
150 if ( $language ne "" ) {
151 # Languages are prefixes to the booknames in the files
152         $OUTPUTFILE =~ s/\.tex$/\.${language}.tex/;
153 }
154
155 # Run the XML preprocessor
156 # TODO: use system() properly here and check return value
157 print "Processing book $bookCode and storing result in $OUTPUTFILE...";
158 # For Xmlto, which uses xsltproc:
159 # (Does not work)
160 # `$XMLPROC -v -o $BOOK_PATH -x ${DATADIR}/latex.xsl dvi ${DATADIR}/${XML_SOURCE}`;
161 # Apache's Xalan:
162 `$XMLPROC  -xsl ${DATADIR}/latex.xsl -in  ${DATADIR}/${XML_SOURCE} -out $OUTPUTFILE`;
163 # Apache Xalan, Java version:
164 # print qx{$JAVA org.apache.xalan.xslt.Process -IN $XML_PATH/$XML_SOURCE -XSL $XML_PATH/latex.xsl -OUT $BOOK_PATH/$bookCode.tex -PARAM title-color \"$TITLE_COLOR\" -PARAM use-illustrators \"$USE_ILLUSTRATORS\"};
165 #
166 print "...done\n";
167
168
169 # End of script
170 exit 0;
171