#!/usr/bin/perl -w

$| = 1;

foreach my $file (@ARGV) {
    &fixup_file ($file);
}

sub fixup_file {
    my ($file) = @_;

    my $result = '';
    local (*FIL);
    open (*FIL, "<$file") or die "$0: Cannot read $file: $!\n";
    print STDERR "Looking at $file...";

    my $spinp = 0;
    my $any = 0;
    while (<FIL>) {
	if (/^\s*<widget class="([a-zA-Z0-9]+)"/) {
	    $spinp = ($1 eq 'GtkSpinButton');
	}

	if ($spinp) {
	    if (/^(\s*<property name="adjustment">)(.*)(<.property>)\s*$/) {
		my $pre = $1;
		my $post = $3;
		my @props = split (' ', $2);
		if ($props[-1] ne '0') {
		    $props[-1] = '0';
		    $any = 1;
		    $_ = $pre . join (' ', @props) . $post;
		}
	    }
	}

	$result .= $_;
    }
    close (*FIL);

    if ($any) {
	open (FIL, ">$file.tmp") or die "$0: Cannot write $file.tmp: $!\n";
	print FIL $result;
	close (FIL);
	rename "$file.tmp", $file;

	print STDERR "  fixed.\n";
    } else {
	print STDERR "  unchanged.\n";
    }
}
