#!/usr/bin/perl
# psl-init - set up files and directories for psl                 -*- perl -*-

# (c) 1994-95 Olaf Titz <olaf.titz@inka.de> and INKA e.V.
# $Id: psl-init,v 2.5 1996/02/23 00:21:38 olaf Exp $

# Usage: psl-init server...
# Set up the necessary files for a feed from "server".
# Read the server's active file, etc. Also used to update the active
# file (leaving the articles file alone).

# 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 "getopts.pl";
require "lockfiles.pl";
require "psl-parm.pl";

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

&Getopts("s");
srand;
while ($server=shift) {
    if (!chdir("$workdir/$server")) {
       if (mkdir("$workdir/$server", 0755)) {
           chdir("$workdir/$server");
       } else {
           print STDERR "mkdir($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";
           if (++$waits{$server} < $maxwaits) {
               push(@ARGV, $wait, $server);
           }
       }
       next;
    }
    if (!open(LOG, ">>$logfile")) {
       print STDERR "Can't open logfile for $server\n";
    }
    &process_server;
    unlink("LOCK.server");
}

exit 0;

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

sub process_server
{
    $count=0;
    print STDOUT "$server ";
    @_ = localtime; $_[5]+=1900 if ($_[5]<1901);
    printf (LOG "\n+++ psl-init %02d.%02d.%04d %02d:%02d\n",
       $_[3], ++$_[4], $_[5], $_[2], $_[1]);
    select(SACT); $|=1;
    socket(NNTP, $AF_INET, $SOCK_STREAM, $IPPROTO_TCP);
    ($serv,$port) = split(/:/, $server);
    $port = $PORT_NNTP unless $port;
    @_ = gethostbyname($serv); $? && exit(1); # die "gethostbyname($serv)";
    connect(NNTP, pack("S n a4 x8", $AF_INET, $port, $_[4]))
        || exit(1); # die "connect($server)";
    select(NNTP); $|=1;
    select(STDOUT); $|=1;
    $_ = <NNTP>; y/\r//; split;
    $_[0] =~ /^20[01]/
        || die "server failed ($_)";
    print NNTP "LIST ACTIVE\r\n";
    $_ = <NNTP>; y/\r//d; split;
    if ($_[0] == 215) {
       open(SACT, ">$sactfile+")
           || die "open new active file failed: $!\n";
       undef @sact;
       while ($_ = <NNTP>, y/\r//d, $_ ne ".\n") {
           split;
           eof(NNTP) && die "Lost connection";
           if ($_[3] =~ /^[ynm]/) {
               ++$count;
               if ($opt_s) {
                   if ($_[0] =~ /^control/) {
                       $o=$count;
                   } else {
                       $o=int(rand(800000))+100000;
                   }
                   push(@sact, sprintf("%06d %s", $o, $_));
               } else {
                   push(@sact, $_);
               }
           }
           if ((++$progress)>50) {
               print STDOUT "."; $progress=0;
           }
        }
       if ($opt_s) {
           foreach $_ (sort @sact) {
               s/^[0-9]+\s+//;
               print SACT;
           }
       } else {
           foreach $_ (@sact) {
               print SACT;
           }
       }
       close SACT;
       unlink($sactfile); rename("$sactfile+", $sactfile);
    } else {
       die "$server: $_";
    }
    close NNTP;
    print STDOUT "$count\n";
    print LOG "   server has $count groups.\n";
    close LOG;
    if (! -f $subfile) {
       open(SUB, ">$subfile") || die "open $subfile";
       print SUB "!*\n";
       print STDOUT "Remember to edit $server/$subfile as desired!\n";
       close SUB;
    }
}

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