#!/usr/bin/perl # Copyright (C) 2005 by ott # Part of the Battle for Wesnoth Project http://wesnoth.org/ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY. # # See the COPYING file for more details. # check for updated campaigns # compare previous list of campaigns with current list # if changed, store the new list use strict; my $OLD = '~/.wesnoth/data/campaigns'; sub readfile { my $FILE = shift; my $refa = shift; my $trunc = shift; # is the file in short form? my ($id,$name,$size,$ver); open(IFILE, "$FILE" || "-") or return 0; while () { chomp; if ($trunc) { ($id,$name,$ver,$size) = split(/\|/); } else { ($id,$name,$size,$ver) = (split(/\|/))[0,1,3,4]; } $refa->{"$id|$name"} = "$ver|$size"; } 1; } sub writefile { my $FILE = shift; my $refa = shift; open(OFILE, ">$FILE") or return 0; foreach my $x ( sort keys %{$refa} ) { print OFILE "$x|" . $refa->{$x} . "\n"; } 1; } sub checkdiff { my $refa = shift; my $refb = shift; my @b = sort keys %{$refb}; my ($x, @onlyina, @onlyinb, @changed); my $y = shift @b; LOOP: foreach $x (sort keys %{$refa}) { #print ":$x|" . $refa->{$x} . ":$y|" . $refb->{$y} . "\n"; if ($x eq $y) { if ($refa->{$x} ne $refb->{$x}) { push @changed, ("$x|" . $refa->{$x} . '|' . $refb->{$x}); } $y = shift @b; } elsif ($x gt $y and $y) { # y does not exist in a push @onlyinb, "$y|" . $refb->{$y}; $y = shift @b; redo LOOP; } else { # $x lt $y or $y is null # x does not exist in b push @onlyina, "$x|" . $refa->{$x}; } } # deal with leftovers in b while ($y) { push @onlyinb, "$y|" . $refb->{$y}; $y = shift @b; } return ( \@onlyina, \@onlyinb, \@changed ); } sub printeach { my $r = shift; foreach my $x (@$r) { print "$x\n" } } sub reportdiff { my $ra = shift; my $rb = shift; my $ch = shift; if (@$ra) { print "Deleted:\n"; printeach $ra; } if (@$rb) { print "New:\n"; printeach $rb; } if (@$ch) { print "Changed:\n"; printeach $ch; } } my (%old, %new, $refonlya, $refonlyb, $refch); if (! -f $OLD) { readfile("", \%old); writefile($OLD, \%old) } else { readfile($OLD, \%old, 1); readfile("", \%new); ($refonlya, $refonlyb, $refch) = checkdiff(\%old, \%new); if ($refonlya || $refonlyb || $refch) { reportdiff($refonlya, $refonlyb, $refch); writefile($OLD, \%new); } }