#!/usr/bin/perl -w
# -----------------------------------------------------------------------------

# This script tries to normalize gnumeric files, i.e., remove insignificant
# differences due to versions, environment, or hash ordering.
#
# It is a perl script mucking with an xml file.  Think it over.  It is not
# hard to cheat this, but for purposes of testing code, it is fine.

my @items;
my $item = '';

while (<STDIN>) {
    # "x" out version numbers.
    if (m|^\s*<gnm:Version\s+Epoch="\d+"\s+Major="\d+"\s+Minor="\d+"\s+Full="[.0-9]+"/>\s*$|) {
	s/="[.0-9]+"/="x"/g;
    }

    if (m|^\s*<gnm:PrintInformation>\s*$| .. m|^\s*</gnm:PrintInformation>\s*$|) {
        # "x" out margins.
	if (m|^\s*<gnm:Margins>\s*$| .. m|^\s*</gnm:Margins>\s*$|) {
	    s/="[-.0-9a-zA-Z]+"/="x"/g;
	}

	# "x" out information from cups.
	s{(<gnm:(paper|orientation)>).*(</gnm:\2>)}{$1xxx$3};
    }

    # Sort names.
    if (0 && m|^\s*<gnm:Sheets>\s*$| .. m|^\s*</gnm:Sheets>\s*$|) {
	if (m|^\s*<gnm:Names>\s*$| .. m|^\s*</gnm:Names>\s*$|) {
	    if (m|^\s*<gnm:Names>\s*$|) {
		# Zip.
	    } elsif (m|^\s*</gnm:Names>\s*$|) {
		print sort @items;
		@items = ();
	    } else {
		$item .= $_;

		if (m|^\s*</gnm:Name>\s*$|) {
		    push @items, $item;
		    $item = '';
		}
		next;
	    }
	}
    }

    print;
}
