#!/usr/local/bin/perl5 
#
# This file is part of SnarfNews
# Copyright (C) 1991,1992,1993,1994,1995,1996 Alec Muffett
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

$files = 0;
$bytes = 0;
$spool = $ENV{"SNARFSPOOL"};
$blksz = 8192;

unless (chdir($spool))
{
    die "gronkfiles: chdir: $spool: $!\n";
}

while (<>)
{
    ($file, @crud) = split(" ");

    unless (open(FILE, $file))
    {
	warn "gronkfiles: open: $file: $!\n";
	next;
    }

    $fsize = (stat(FILE))[7];

    if ($fsize)
    {
	print "#! rnews $fsize\n";
	$files++;
	$bytes += $fsize;
	$remain = $fsize;

	while ($remain > 0)
	{
	    if ($rsize = read(FILE, $buffer, $blksz))
	    {
		print $buffer;
	    }
	    $remain -= $rsize;
	    last unless ($rsize);
	}

	if ($remain < 0)
	{
	    $over = 0 - $remain;
	    die "gronkfiles: read too much of $file: $over\n"
	}
	elsif ($remain > 0)
	{
	    die "gronkfiles: read too little of $file: $remain\n"
	}
    }

    close(FILE);
}

warn "gronkfiles: files=$files bytes=$bytes\n" if ($debug);

exit 0;
