#!/usr/local/bin/perl5 -s
#
# 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.
#

if ($#ARGV < 0)
{
    die "Usage: runbatch \"command argument ...\" < file\n";
}

$counter = 0;

while (<STDIN>)
{
    unless (/^\#!\s*rnews\s+(\d+)/)
    {
	die "runbatch: cannot find rnews tag at STDIN line $.\n";
    }

    $size = $1;
    warn "runbatch: $size bytes\n" if ($debug);
    $counter++;

    unless (($rsize = read(STDIN, $buffer, $size)) == $size)
    {
	die "runbatch: read: $rsize/$size bytes: $!\n";
    }

    warn "runbatch: running: @ARGV\n" if ($debug);

    unless (open(PIPE, "| @ARGV"))
    {
	die "runbatch: popen: @ARGV: $!\n";
    }
    print PIPE $buffer;
    close(PIPE);

    warn "runbatch: @ARGV: exit status $?\n" if ($?);
}

warn "runbatch: $counter batches\n" if ($debug);

exit 0;


