#!/usr/bin/perl -w
# $Id: p0rn-download,v 1.9 2004/05/26 08:59:47 mitch Exp $
#
# download selected p0rn
#
# 2004 (C) by Christian Garbs <mitch@cgarbs.de>
# Licensed under GNU GPL.  See COPYING for details.

use strict;
use P0rn::DB;

=head1 NAME

p0rn-download - download URLs queued in p0rn database

=head1 SYNOPSIS

B<p0rn-download>

=head1 OVERVIEW

p0rn-download starts to download the URLs queued in the p0rn database.

=head1 DESCRIPTION

p0rn-download starts a massive parallel download via L<p0rn-grab(1)>.
Downloaded files appear in B<./out>.

=head1 FILES

All data is stored in a database.  By default, it is located in
B<./p0rn.db> (yes, that's the directory from which you're starting
p0rn-download).  Is you want to change this, set the environment
variable B<P0RNDBLOCATION> (the second letter is a zero) to another
path and filename.

=head1 SEE ALSO

L<p0rn-proxy(1)>, L<p0rn-dbdump(1)>, L<p0rn-dbrestore(1)>, L<p0rn-grab(1)>

=head1 MODULES NEEDED

 use DBM::Deep;

This module can be obtained at <F<http://www.cpan.org>>.

=head1 PROGRAMS NEEDED

=over

=item lynx: L<http://lynx.browser.org/>

=item wget: L<http://wget.sunsite.dk/>

=back

=head1 BUGS

Please report bugs by mail to <F<p0rn-bugs@cgarbs.de>>.

=head1 AUTHOR

p0rn-download was written by Christian Garbs <F<mitch@cgarbs.de>>.

=head1 AVAILABILITY

Look for updates at L<http://www.cgarbs.de/p0rn-comfort.en.html>.

=head1 COPYRIGHT

p0rn-download is licensed under the GNU GPL.

=cut

my $wait_after_download = 10;
my $wait_after_cycle    = 30;
my $done_something = 1;

$SIG{CHLD} = 'IGNORE';

my $hash = opendb('downz');
while ($done_something) {

    $done_something = 0;
    
    foreach my $url (keys %{$hash}) {
	
	if ($hash->{$url} == 0) {

	    if (!fork()) {
		warn "[[ START $url ]]\n";
		exit if ($hash->{$url});
		$hash->{$url} = 1;

		system('./p0rn-grab', $url);

		$hash->{$url} = 2;
		warn "[[ FINISHED $url ]]\n";
		exit;
	    }

	    sleep $wait_after_download;
	    $done_something = 1;

	}

    }
    sleep $wait_after_cycle;

}


wait;
warn "\n[[ FINISHED. ]]\n\n";
