+
+
+
+##################
+# the program flow
+##################
+
+# first work out when the last time we were up to date is and
+# find present time.
+my $timeoflastupdate = findLastUpdateTime();
+
+if ($verbose) {
+ print STDERR "timenow is ".time2str("%c",$timenow)." \n";
+ print STDERR "timeoflastupdate is ".time2str("%c",$timeoflastupdate)." \n\n";
+}
+
+# Now we know which days' changes we need to get from the server
+my @changesfiles = getChangesFileList($timeoflastupdate, $timenow);
+if ($verbose or $listchangefiles) {
+ foreach my $file (@changesfiles) { print STDERR "using changes file $file\n"; }
+ exit 0 if $listchangefiles;
+}
+
+# get the changes files
+my @changesfilecontent;
+foreach my $file (@changesfiles) { push @changesfilecontent, getChangesFile($file); }
+
+# if the file has not changed (response code 304) then ignore it
+
+# iterate over all the fetched files, building up a list of files
+# to fetch/delete
+my %files;
+foreach my $changes (@changesfilecontent) {
+ my @changes = split /[\r\n]+/, $changes;
+ foreach my $change (@changes) {
+ my ($time, $op, $path) = split ' ', $change;
+ # Strip scheme and host from absolute URLs
+ $path =~ s{^[a-z]+://[a-z0-9\-\.]+/}{/};
+ # TODO: Ignore changes prior to $timeoflastupdate
+ # TODO: Ignore malformed lines, especially wacky paths that could be malicious
+ $files{$path} = $op;
+ print STDERR "Marked $path as '$op'\n" if $verbose;
+ }
+}
+
+# Fetch all files whose last operation was "add" or "change"
+# Delete all files whose last operation was "delete"
+while (my ($file, $op) = each %files) {
+ if ($op eq "delete") {
+ if (-e "$workingdir/$file") {
+ # delete: if the file exists, remove it
+ print STDERR "deleting $workingdir/$file\n" if $verbose;
+ unlink "$workingdir/$file" or die "Can't delete $workingdir/$file ($!)";
+ }
+ else {
+ print STDERR "not deleting $workingdir/$file beacuse it doesn't exist\n" if $verbose;
+ }
+ }
+ elsif ($op eq "add" or $op eq "change" or $op eq "Modification") {
+ # add/change: re-fetch the file
+ my $content = fetchFile("$remoteroot/$file","$workingdir/$file");
+ }
+ else {
+ die "Unknown operation '$op'";
+ }
+}
+
+
+# update the last "up-to-date" time
+saveLastUpdateTime();
+
+# finish
+exit 0;
+
+