#!/bin/sh
if [ $# != 2 ]
then if [ $# != 3 ]
     then echo "USAGE: $(basename $0) [-O] <input_file> <output_file>";
          exit 1;
     elif [ $1 != "-O" ]
     then echo "USAGE: $(basename $0) [-O] <input file> <output_file>";
          exit 1;
     fi
fi

FLEXOPTS=""
FILE1=$1;
FILE2=$2;

if [ $# = 2 ]
then if [ ! -e $1 ] 
     then echo "ERROR: '$1' file not found";
          exit 1;
     fi 
fi

if [ $# = 3 ]
then if [ ! -e $2 ]
     then echo "ERROR: '$2' file not found";
          exit 1;
     fi
     FLEXOPTS="-Cfer";
     FILE1=$2;
     FILE2=$3;
fi
/opt/android/v15-arm/bin/xmllint --dtdvalid /tmp/v15/share/apertium/format.dtd --noout $FILE1 && \
/usr/bin/xsltproc /tmp/v15/share/apertium/reformat.xsl $FILE1 >/tmp/$$.reformat.l && \
/usr/bin/flex $FLEXOPTS -o/tmp/$$.lex.cc /tmp/$$.reformat.l && \
arm-linux-androideabi-g++ -DGENFORMAT -Wall -Wextra -O3 -fPIC -I/opt/android/v15-arm/include -I/opt/android/v15-arm/include/libxml2 -I/tmp/v15/include/lttoolbox-3.3 -I/tmp/v15/include/apertium-3.4 -w -I/opt/android/v15-arm/tmp/v15/include/lttoolbox-3.3 -I/opt/android/v15-arm/tmp/v15/lib/lttoolbox-3.3/include -I/opt/android/v15-arm/opt/android/v15-arm/opt/android/v15-arm/include/libxml2 -I/opt/android/v15-arm/opt/android/v15-arm/include/libxml2 -I/opt/android/v15-arm/opt/android/v15-arm/include   -I /tmp/v15/include/apertium-3.4 -o $FILE2 /tmp/$$.lex.cc -L/opt/android/v15-arm/tmp/v15/lib -L/opt/android/v15-arm/opt/android/v15-arm/opt/android/v15-arm/lib -L/opt/android/v15-arm/opt/android/v15-arm/lib -llttoolbox3 -lxml2 -lpcre   2>/dev/null &&\
rm /tmp/$$.reformat.l /tmp/$$.lex.cc
