LC_CTYPE=C
LC_COLLATE=C
LC_TIME=C
LC_NUMERIC=C
LC_MONETARY=C
LC_MESSAGES=C
LANG=C

export LC_CTYPE LC_COLLATE LC_TIME LC_NUMERIC LC_MONETARY LC_MESSAGES LANG

BGDTD=$1
ENDTD=$2

BGTMP=`mktemp`
ENTMP=`mktemp`

cat $BGDTD |\
sed 's/^[\t ]*<!ENTITY[\t ]*//' |\
sed 's/"[\t ]*>$//' |\
sed 's/[\t ]*"/|/' |\
sort -t '|' -k 1,1 > $BGTMP

cat $ENDTD |\
sed 's/^[\t ]*<!ENTITY[\t ]*//' |\
sed 's/"[\t ]*>$//' |\
sed 's/[\t ]*"/|/' |\
nl |\
sed 's/^[\t ]*\([0-9][0-9]*\)[\t ]*\(.*\)$/\2|\1/' |\
sort -t '|' -k 1,1 > $ENTMP

RESTMP=`mktemp`


join $ENTMP $BGTMP  -t '|' -o 1.3,1.1,1.2,2.2 > $RESTMP
join $ENTMP $BGTMP  -t '|' -o 1.3,1.1,1.2,2.2 -v 1 >> $RESTMP

cat $RESTMP |\
sort -t '|' -k 1,1 -n |\
sed 's/\([^|]*\)|$/\1|\1/' |\
cut -d '|' -f 2,4 |\
sed 's/^/<!ENTITY /' |\
sed 's/$/">/' |\
sed 's/|/ "/'

rm $BGTMP $ENTMP $RESTMP 
