#!/bin/sh

# SYNTAX: sh jade_run.sh ~/Db/Homepage/Ao/3  ___.xml

# When new articles added, attempt to keep old versions of existing
# articles; if old versions kept (same modification time!), will
# prevent upload by homepage.py.


if [ -d "$1" ]; then work_dir=$1; pushd $work_dir;
else
	echo "No such subdirectory: $1"
	exit 1
fi
if [ -f "$2" ]; then xml_file=$2;
else
	echo "No such file: $2"
	exit 1
fi
# -----------------------------------------------------------------------------

jadestor="/tmp/Jadestor.$$"
mkdir ${jadestor}

echo "Moving existing files into ${jadestor}..."
mv *.html ${jadestor}
#
# New version of Jade and no more manifest!
# mv HTML.manifest ${jadestor}

echo "Executing jade..."
echo ""
# Previous version:
# jade -d /usr/lib/dsssl/stylesheets/docbook/html/docbook.dsl \
#      -t sgml /usr/lib/sgml/declaration/xml.decl ao2.xml 2>| jade.rpt
#

# Current version:
c="jade"
V="-V %use-id-as-filename%"
d="-d /usr/lib/sgml/stylesheet/dsssl/docbook/nwalsh/html/docbook.dsl"
t="-t sgml"
l="/usr/lib/sgml/declaration/xml.decl"
echo " $c $V"
echo "        $d"
echo "        $t  $l"
echo "        $xml_file"
jade $V $d $t $l $xml_file >| jade_run.rpt 2>&1

echo ""
echo "Comparing before & after versions..."
found_diff=
for file in *.html
do
    diff="${jadestor}/${file}.dif"
    diff_unc="${diff}.unc"
    diff "${jadestor}/$file" "$file" > $diff

    sed -n -e 's/[0-9]/#/g' -e'/^[><]/p' $diff | \
	egrep -v '^[><] NAME="AEN####"$' \
	> $diff_unc

    if [ ! -s $diff_unc ]; then
	rm -f "$file"
	rm -f "$diff"
    else
	echo "Different: $file"
	rm -f "${jadestor}/$file"
	found_diff=1
    fi

    rm -f $diff_unc
done

echo "Moving files from directory ${jadestor} back to ${work_dir}..."
mv ${jadestor}/*.html $work_dir

echo ""
if [ $found_diff ]; then echo "DIFFERENCES:"; find "${jadestor}" -type f;
else
    echo "NO DIFFERENCES!  Removing directory ${jadestor}..."
    rmdir ${jadestor}
fi

popd
###
#