#!/usr/bin/perl
# psl-salvage - restore corrupt psl subscription files             -*- perl -*-

# (c) 1994-95 Olaf Titz <olaf.titz@inka.de> and INKA e.V.
# $Id: psl-salvage,v 2.2 1995/11/05 13:10:52 olaf Exp $

# Usage: psl-salvage server...
# If a psl-run process crashes, you are possibly left with an incomplete
# articles+ file and the old one unchanged.
# This program can then be run to at least incorporate those updates
# into the articles file that are in the articles+ file.
# DO NOT run this after a crash of psl-subscribe - re-run psl-subscribe
# instead.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

require "lockfiles.pl";
require "psl-parm.pl";

# ----------------------------------------------------------------------------

while ($server=shift) {
    if (!chdir("$workdir/$server")) {
       print STDERR "chdir($workdir/$server) failed\n"; next;
    }
    if (!&lockfile("LOCK.", "server")) {
       if ($?<0) {
           print STDERR "Can't establish lock for $server";
       } else {
           print STDERR "$server is locked by pid $?\n";
       }
       next;
    }
    if (!open(LOG, ">>$logfile")) {
       print STDERR "Can't open logfile for $server\n";
    }
    select(LOG); $|=1;
    &process_server;
    unlink("LOCK.server");
}

exit 0;

# ----------------------------------------------------------------------------

sub process_server
{
    @_ = localtime;
    printf (LOG "\n+++ psl-salvage %02d.%02d.%02d %02d:%02d\n",
       $_[3], ++$_[4], $_[5], $_[2], $_[1]);
    if (!open(OSUB, "<$artfile")) {
       print LOG "   open old file failed: $!\n";
       return;
    }
    if (!open(SUB, "<$artfile+")) {
       print LOG "   open crash file failed: $!\n";
       return;
    }
    if (!open(NSUB, ">$artfile++")) {
       print LOG "   open new file failed: $!\n";
       return;
    }

    print STDOUT "$server ";
    while (<SUB>) {
       $o = <OSUB> || die "Can't read old file";
       if (/^[a-zA-Z0-9._+-]+[:!]\s*[0-9,-]+\n$/) {
           print NSUB;
           ++$ncount;
       } else {
           print NSUB $o;
           ++$ocount;
           last;
       }
    }
    while (<OSUB>) {
       ++$ocount;
       print NSUB;
    }
    print STDOUT "$ocount/$ncount\n";
    print LOG "  $ocount lines from old file, $ncount from new file\n";
    close OSUB; close SUB; close NSUB; close LOG;
    unlink("$artfile.old");
    rename("$artfile", "$artfile.old");
    rename("$artfile++", "$artfile");
    unlink("$artfile+");
}

# ----------------------------------------------------------------------------
