#!/bin/sh

set -e
jing_main=com.thaiopensource.relaxng.util.Driver
jing_test_main=com.thaiopensource.relaxng.util.TestDriver
trang_main=com.thaiopensource.relaxng.translate.Driver
trang_test_main=com.thaiopensource.relaxng.translate.test.CompactTestDriver
a_jing_file=src/com/thaiopensource/relaxng/util/Driver.java
crimson_version=1.1.3
xerces_version=2.3.0
jing_version_properties=src/com/thaiopensource/relaxng/util/resources/Version.properties
trang_version_properties=trang/src/com/thaiopensource/relaxng/translate/resources/Version.properties
crimson_unwanted="
org/w3c
org/xml
org/apache/crimson/tree
org/apache/crimson/jaxp/DocumentBuilderImpl.java
org/apache/crimson/jaxp/DocumentBuilderFactoryImpl.java
javax/xml/parsers/DocumentBuilder.java
javax/xml/parsers/DocumentBuilderFactory.java
META-INF/services/javax.xml.parsers.DocumentBuilderFactory
META-INF/services/javax.xml.transform.TransformerFactory
META-INF/jaxp-javax.manifest
META-INF/manifest.crimson
"
jing_unwanted="
com/thaiopensource/datatype/xsd/regex/jdk1_4
com/thaiopensource/datatype/xsd/regex/xerces
com/thaiopensource/relaxng/jarv
com/thaiopensource/relaxng/util/JingTask.java
com/thaiopensource/validate/xerces
com/thaiopensource/validate/schematron
com/thaiopensource/validate/jarv
META-INF/services/org.iso_relax.verifier.VerifierFactoryLoader
"
jing_unwanted_in_trang="
com/thaiopensource/relaxng/impl
com/thaiopensource/relaxng/util
com/thaiopensource/relaxng/SchemaFactory.java
com/thaiopensource/validate
"
jing_test="dir.xsl exslt.xsl prep.xsl
spectest.xml xsdtest.xml xsdtest.xsl mnstest.xml nrltest.xml"

if [ -f ../${a_jing_file} ]; then
    cd ..
elif [ ! -f ${a_jing_file} ]; then
    echo You are in the wrong directory >&2
    exit 1
fi

if [ $# -gt 0 ]; then
    jing_build_dir=$1
else
    jing_build_dir=build
fi

jing_build_dir=`cd ${jing_build_dir}; pwd`
build_dir=${jing_build_dir}/gcj
dist_dir=${jing_build_dir}/dist/gcj

top_dir=`pwd`

rm -fr ${build_dir}
test -f ${build_dir} || mkdir -p ${build_dir}

. ${jing_version_properties}
jing_version=$version

. ${trang_version_properties}
trang_version=$version

jing_dir=${build_dir}/jing-${jing_version}
trang_dir=${build_dir}/trang-${trang_version}

mkdir -p ${jing_dir} ${trang_dir}

(ls src/META-INF/services/*.*; find src -name '*.java' -or -name '*.jj' -or -name '*.properties' -or -name '*.rng') | cpio --quiet -p -m -d ${jing_dir}

cd ${jing_dir}/src
rm -fr ${jing_unwanted}

f=com/thaiopensource/relaxng/util/resources/Messages.properties
sed -e 's/java com.thaiopensource.relaxng.util.Driver/jing/' $f >tem
mv tem $f

echo com.thaiopensource.datatype.xsd.regex.xerces2.RegexEngineImpl >META-INF/services/com.thaiopensource.datatype.xsd.regex.RegexEngine

cd ${top_dir}
CLASSPATH=lib/JavaCC.zip gij COM.sun.labs.javacc.Main -output_directory=${jing_dir}/src/com/thaiopensource/relaxng/parse/compact  ${jing_dir}/src/com/thaiopensource/relaxng/parse/compact/CompactSyntax.jj >/dev/null

f=${jing_dir}/src/com/thaiopensource/relaxng/parse/compact/CompactSyntaxTokenManager.java
sed -e 's/java.io.IOException/EOFException/g' $f >${build_dir}/tem
mv ${build_dir}/tem $f


cd ${build_dir}
tar xfz ${top_dir}/gcj/crimson-${crimson_version}-src.tar.gz crimson-${crimson_version}/src
cd crimson-${crimson_version}
patch -p0 <${top_dir}/gcj/crimson.patch
cd src

rm -fr ${crimson_unwanted}
find org javax -type f -not -name '*.java' -not -name '*.properties' | xargs rm -f

cd ${top_dir}
cp -a ${build_dir}/crimson-${crimson_version}/src/* ${jing_dir}/src
rm -fr ${build_dir}/crimson-${crimson_version}

cd ${build_dir}

xerces_top_dir=xerces-`echo ${xerces_version} | tr . _`
xerces_src_dir=${xerces_top_dir}/src

tar xfz ${top_dir}/gcj/Xerces-J-src.${xerces_version}.tar.gz ${xerces_src_dir}/org/apache/xerces/impl/xpath/regex
cd ${top_dir}
cp -a ${build_dir}/${xerces_src_dir}/* ${jing_dir}/src
rm -fr ${build_dir}/${xerces_top_dir}
cd ${jing_dir}
patch -p0 <${top_dir}/gcj/xerces-regex.patch
rm -f `find src -name '*~'`

cd ${top_dir}

cp -a ${jing_dir}/src ${trang_dir}

cd dtdinst
find src -name '*.java' -or -name '*.properties' | cpio --quiet -p -m -d ${trang_dir}
cd ../trang
find src -name '*.java' -or -name '*.properties' | cpio --quiet -p -m -d ${trang_dir}

cd ${trang_dir}/src

rm -fr ${jing_unwanted_in_trang}

f=com/thaiopensource/relaxng/translate/resources/Messages.properties
sed -e 's/java com.thaiopensource.relaxng.translate.Driver/trang/' $f >tem
mv tem $f

cd ${top_dir}

setup_prog() {

cd ${prog_src_dir}

sed -e "s/@PROG@/${prog}/" -e "s/@MAIN@/${main}/" -e "s/@TEST_MAIN@/${test_main}/" ${top_dir}/gcj/vars.mk >Makefile.in

echo SOURCES=\\ >>Makefile.in
find src -name '*.java' | sed -e 's;^;  $(srcdir)/;' -e 's/$/ \\/' -e '$s/ \\//' >>Makefile.in

echo RESOURCES=\\ >>Makefile.in
find src -type f -not -name '*.java' -not -name '*.jj' | sed -e 's;^;  ;' -e 's/$/.o/' -e 's/$/ \\/' -e '$s/ \\//' >>Makefile.in

echo DIRS=src \\ >>Makefile.in

find src/* -type d -not -name '*.java' -not -name '*.jj' | sed -e 's;^;  ;' -e 's/$/ \\/' -e '$s/ \\//' >>Makefile.in

cat ${top_dir}/gcj/rules.mk >>Makefile.in
find src -type f -not -name '*.java' -not -name '*.jj' -exec mv "{}" "{}.resource" ";"

sed -e "s/@PROG@/${prog}/" -e "s/@PROG_VERSION@/${prog_version}/" ${top_dir}/gcj/configure.ac >configure.ac

cp -a ${top_dir}/gcj/aclocal.m4 .
cp -a ${top_dir}/gcj/install-sh .
cp -a ${top_dir}/gcj/mkinstalldirs .
cp -a ${top_dir}/gcj/INSTALL .

sed -e "s/@VERSION@/${prog_version}/" ${top_dir}/gcj/${prog}.1 >${prog}.1

autoconf

rm -fr autom4te*

sed -e "/^Version:/s/:.*/: ${prog_version}/" ${top_dir}/gcj/${prog}.spec >${prog}.spec
cat >copying.txt <<EOF
Except as otherwise stated, the following license applies to this software.

EOF
sed -e 's/^/  /' ${copying} >>copying.txt
cat >>copying.txt <<EOF

The XML parser used is Crimson, to which the following license applies.

EOF
sed -e 's/^/  /' ${top_dir}/gcj/crimson.copying.txt >>copying.txt

cat >>copying.txt <<EOF

The W3C XML Schema regular expression implementation is from Xerces,
to which the following license applies.

EOF
sed -e 's/^/  /' ${top_dir}/gcj/xerces.copying.txt >>copying.txt

cp -a ${top_dir}/gcj/README.redhat8 .
mkdir test
}

prog=jing
main=${jing_main}
test_main=${jing_test_main}
prog_version=${jing_version}
prog_src_dir=${jing_dir}
copying=${top_dir}/copying.txt
setup_prog

cd test

for f in ${jing_test}; do cp -a ${top_dir}/test/$f .; done

cp -a ${top_dir}/gcj/test_jing.mk Makefile.in

cd ${top_dir}
prog=trang
main=${trang_main}
test_main=${trang_test_main}
prog_version=${trang_version}
prog_src_dir=${trang_dir}
copying=${top_dir}/trang/copying.txt
setup_prog
sed -e "s/@VERSION@/${trang_version}/g" ${top_dir}/trang/doc/trang-manual.html >trang-manual.html
cd test

cp -a ${top_dir}/trang/test/compacttest.xml .
cp -a ${top_dir}/trang/test/toxsdtest.xml .
cp -a ${top_dir}/test/dir.xsl .
cp -a ${top_dir}/test/exslt.xsl .
cp -a ${top_dir}/trang/test/compactprep.xsl prep.xsl
cp -a ${top_dir}/gcj/test_trang.mk Makefile.in

cd ${top_dir}
test -d ${dist_dir} || mkdir -p ${dist_dir}
cd ${dist_dir}
abs_dist_dir=`pwd`
cd ${top_dir}
cd ${jing_dir}/..
tar cfz ${abs_dist_dir}/jing-${jing_version}.tar.gz jing-${jing_version}
cd ${top_dir}
cd ${trang_dir}/..
tar cfz ${abs_dist_dir}/trang-${trang_version}.tar.gz trang-${trang_version}
