#!/bin/bash

function create_validate {
    echo -n `date +"%H:%M:%S"` : new/$1.xml created
    java -Xmx300m -jar $SAXONJAR -xsl:$MAKEENTRIES -s:new/$1.xml -o:new/$1-entries.xml
    echo -n ";" transformed to new/$1-entries.xml
    java -jar jing.jar -c gcide.rnc new/$1-entries.xml
    if [ $? == 0 ]; then 
        echo "; valid !"
    else
        echo "; NOT valid!!"
    fi
}

MAKEENTRIES="make-entries-with-incl.xsl"
SAXONJAR="saxon9he.jar"

### we taken for granted that we have saved the excel file gcide-tags-old2new.xsl in csv format
### in Windows format separated by ;
TAGSCONVERSION="gcide-tags-old2new.csv"
CREATENEWTAGS="gcide-gen-conv"  
RNCSCHEMA="gcide.rnc"

echo `date +"%H:%M:%S"` : start of conversion of gcide files

# create gcide-conv.xsl to be included in the transformation stylesheet $MAKEENTRIES
#        gcide-conv.rnc: to be included in the rnc schema for tags conversion 
java -jar $SAXONJAR -xsl:gcide-gen-conv.xsl -it:main -o:gcide-conv.xsl file=$TAGSCONVERSION out_rnc=gcide-conv.rnc
echo gcide-conv.xsl and gcide-conv.rnc created

## create useful auxiliary files
cat >doctype-entries <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entries [
<!ENTITY % gcide-ent SYSTEM "gcide.ent">
%gcide-ent;
]>

<!-- added reference to "gcide.ent" and transformed to well-formed XML
     by Guy Lapalme (lapalme@iro.umontreal.ca)
     Université de Montréal - Université de Marseille
     October 2009 
-->

EOF
echo "<entries>" >entries-start
echo "</entries>" >entries-end

cat >doctype-div0 <<EOF
<?xml version="1.0" encoding="UTF-8"?>

<!-- added reference to "gcide.ent" 
     by Guy Lapalme (lapalme@iro.umontreal.ca)
     Université de Montréal - Université de Marseille
     October 2009 
-->

<!DOCTYPE div0 [
<!ENTITY % gcide-ent SYSTEM "gcide.ent">
%gcide-ent;
]>
EOF

## create xsl and rnc files from the content of the csv file 
### $$$$$

## clean new
rm -f new/*

## create gcide.ent from the start of gcide.xml
cat >new/gcide.ent <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!-- extracted entities from  gcide.xml 
     by Guy Lapalme (lapalme@iro.umontreal.ca)
     Université de Montréal - Université de Marseille
     October 2009 
-->
EOF
# did not find an easy way to specify a regexp keep only the first comment of the file with sed...
sed -n -e '3,15p' old/gcide.xml >>new/gcide.ent
# ignore DOCTYPE...
sed -n -e '/<!-- I have used/,/ENTITY dotted3_/p' old/gcide.xml >>new/gcide.ent

## create the front matter from the original by adding reference to the entities file
files="gcide_abbreviations gcide_authorities"
for f in $files; do
    cat doctype-div0 old/$f.xml >new/$f.xml
    create_validate $f 
done

## extract the numbers part from the A file
cat doctype-entries entries-start >new/gcide_numbers.xml
sed -n -e'1,/^<p><!-- p. 1 -->/p' old/gcide_a.xml >>new/gcide_numbers.xml
cat entries-end >> new/gcide_numbers.xml
create_validate gcide_numbers

## remove the numbers part from the A file
cat doctype-entries entries-start >new/gcide_a.xml
sed -n -e'1,/^ -->/p'  old/gcide_a.xml >>new/gcide_a.xml
sed -n -e'/^<p><centered><point26>A./,$p' old/gcide_a.xml >>new/gcide_a.xml
cat entries-end >>new/gcide_a.xml
create_validate gcide_a

## create well-formed XML file for the GCIDE
##   for each letter
letters="b c d e f g h i j k l m n o p q r s t u v w x y z"
for l in $letters; do
    cat doctype-entries entries-start old/gcide_$l.xml entries-end >new/gcide_$l.xml
    create_validate gcide_$l
done

## create new/gcide.xml with xinclude instead of entities... 
sed -n -e '1,/^-->/p' old/gcide.xml > new/gcide.xml
cat >>new/gcide.xml <<EOF

<!-- modified to use XInclude
     by Guy Lapalme (lapalme@iro.umontreal.ca)
     Université de Montréal - Université de Marseille
     October 2009 
-->
<?oxygen RNGSchema="gcide.rnc" type="compact"?>
<dictionary>
    <front>
        <xi:include href="gcide_authorities-entries.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
        <xi:include href="gcide_abbreviations-entries.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
    </front>
    <body>
        <xi:include href="gcide_numbers-entries.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
EOF
for f in new/gcide_?-entries.xml; do
    fn=`basename $f`
    echo "       <xi:include href='$fn' xmlns:xi='http://www.w3.org/2001/XInclude'/>" >>new/gcide.xml
done
cat >>new/gcide.xml <<EOF
    </body>
</dictionary>
EOF
echo `date +"%H:%M:%S"` : created new/gcide.xml

## create the gcide that includes everything else
## create a single XML file with all included files
# useful for processors not dealing with xi:include
# because of the size of the file and the Xinclude processing we do not use the "create_validate" function
java -Xmx500m -jar $SAXONJAR -xi:on -xsl:copy.xsl -s:new/gcide.xml -o:new/gcide-entries.xml
echo -n `date +"%H:%M:%S"` : created new/gcide-entries.xml
java -jar jing.jar -c gcide.rnc new/gcide-entries.xml
if [ $? == 0 ]; then 
    echo "; valid !"
else
    echo "; NOT valid!!"
fi
# copy the schema files into the new directory
cp gcide.rnc gcide-conv.rnc new

# create the final product directory and zip it
rm -f new-entries.zip
mkdir new-entries
cp new/*-entries.xml new/gcide.ent new/gcide.xml gcide.rnc gcide-conv.rnc show-entries.xsl new-entries
zip -r new-entries.zip new-entries
echo new-entries.zip created

echo `date +"%H:%M:%S"` : end of conversion, validation and creation of the zip archive of the gcide files
