#!/bin/bash

# This script is used for translations using .po files.
# It merges .xml files per chapter (or appendix).
# The reason files are merged is that individual .xml files
# are often not well-formed xml.

if [ "$1" = "--help" ]; then
    echo "Usage: $0 lang"
    exit 0
fi

language=${1:-pl}

SCRIPTDIR="./scripts"
BUILDDIR="./build"

# Required tools
for tool in gawk egrep iconv ; do
    if [ -z "`which $tool`" ] ; then
        echo "ERROR: $tool is required, please install it."
        exit 1
    fi
done

LANGDIR="./$language"
[ -d $LANGDIR ] || exit 1

if [ -z "$PO_USEBUILD" ] ; then
    WORKDIR="./integrated"
else
    WORKDIR="$BUILDDIR/build.po"
fi
[ -d $WORKDIR ] || mkdir -p $WORKDIR

TEMPDIR=/tmp/merge_xml.$$
LOG=$TEMPDIR/merge_xml.$language.log
[ -d $TEMPDIR ] || mkdir $TEMPDIR

#<!ENTITY bookinfo.xml         SYSTEM "en/bookinfo.xml">
OLD_IFS=$IFS
IFS="
"
:>$TEMPDIR/entlist
echo "Building list of entities..."
for ENT in `grep "<!ENTITY" $BUILDDIR/templates/docstruct.ent` ; do
    echo -n "$(echo $ENT | sed "s/.*ENTITY\ *//" | sed "s/\ *SYSTEM.*$//")" >>$TEMPDIR/entlist
    echo -n ":" >>$TEMPDIR/entlist
    echo    "$(echo $ENT | sed "s/.*SYSTEM\ *\"##SRCPATH##\///" | sed "s/\">//")" >>$TEMPDIR/entlist
    
done
IFS=$OLD_IFS

# Make sure that all files are in UTF-8 first
echo "Converting XML files to UTF-8..."
echo "Converting XML files to UTF-8..." >>$LOG
for FILE in `find $LANGDIR -name "*.xml"` ; do
    SUBDIR=$(dirname $FILE | sed "s:$LANGDIR::");
    XML=$(basename $FILE)
    mkdir -p $TEMPDIR/in/$SUBDIR
    REGEXP="^<?.*encoding="
    if egrep -q $REGEXP $FILE ; then
        echo "Encoded    : $FILE" >>$LOG
        ENC=$(egrep $REGEXP $FILE | sed "s/.*xml.*encoding=\"//" | sed "s/\"?>//")
        iconv -f $ENC -t utf-8 $FILE | egrep -v $REGEXP >$TEMPDIR/in/$SUBDIR/$XML
    else
        echo "Not encoded: $FILE" >>$LOG
        cp $FILE $TEMPDIR/in/$SUBDIR/$XML
    fi
done
echo "" >>$LOG

# Include lower level xml-files for all the main level xml-files
echo "Merging XML files per 'chapter'..."
echo "Merging XML files per 'chapter'..." >>$LOG
gawk -v WORKDIR="$TEMPDIR" -v LOG=$LOG -v ENTLIST="$TEMPDIR/entlist" \
    -f $SCRIPTDIR/merge_xml.awk $BUILDDIR/templates/install.xml.template

# Copy the results to their proper location
TARGET="$WORKDIR/$language"
if [ -d $TARGET ]; then
    rm -r $TARGET
fi
mkdir -p $TARGET
cp -r $TEMPDIR/out/* $TARGET
cp $LOG $TARGET

rm -r $TEMPDIR
exit 0
