#!/bin/bash

set -e
HM=`pwd`
SD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CD=postgresql.chk

TD=/tmp/epub.compare

# Internal functions
###############################################################################
compareEpub() {
    # Compare texts as html's differ in id's and not significant white-space characters
    # Convert html to text in the same directory for inner links to be the same
    [ -d $TD/epub1 ] && rm -rf $TD/epub1
    cp -R $1 $TD/epub1

    [ -d $TD/epub2 ] && rm -rf $TD/epub2
    cp -R $2 $TD/epub2

    for f in $TD/epub1/*/* $TD/epub2/*/*; do
        [ -f $f ] || continue;
        xmllint --format $f -o $f
        perl -p -i -e 's/((id|idref)="(ftn\.|ientry-)?id)[a-z0-9]+/$1/g' $f;
        perl -p -i -e 's/(href="#(ftn\.|ientry-)?id)[a-z][0-9]+/$1/g' $f;
        perl -p -i -e 's/(\.html#(ftn\.)?id)[a-z][0-9]+/$1/g' $f;
        perl -p -i -e 's/(content="[0-9.]+_id)[a-z][0-9]+/$1/g' $f;
        perl -p -i -e 's/(<dc:identifier[^>]*>[0-9.]+_id)[a-z][0-9]+/$1/g' $f;
    done

    diff -u -b -r $TD/epub1 $TD/epub2
}
###############################################################################
rm -rf $CD
rm -rf $TD
mkdir $TD
# this will create ./postgresql.chk/
git clone git://git.postgresql.org/git/postgresql.git $CD
cd $CD
./configure --enable-nls --without-readline --without-zlib

cd doc/src/sgml
echo 'Original build:'
time make postgres.epub
mkdir $TD/output1
cp postgres.epub $TD/output1/
(cd $TD/output1; unzip -q postgres.epub; rm postgres.epub)
rm postgres.epub

patch -i $SD/epub-build-speedup.patch

echo 'Optimized build:'
time make postgres.epub
mkdir $TD/output2
cp postgres.epub $TD/output2/
(cd $TD/output2; unzip -q postgres.epub; rm postgres.epub)

compareEpub $TD/output1 $TD/output2
###############################################################################

echo 'OK'
