#!/usr/bin/perl -w

# 04/28/2001 - Michael Carey
# this mighty application is released under
# the GPL
#
# CONFIGURATION REQUIRED...  SEE THE VARIABLE
# DECLARATIONS.
#
# simple freenet requester client.  If called
# with one parm, "favorites", it will read
# in keys from a text file.  I have had good luck
# scheduling this application to download MSK's.
# I typically don't have to wait for MSK's I
# visit often like Snarfoo.
#
# As a simple requester client, it tries an HTL100
# every time...  not sophisticated, but
# very functional.
#

#


# to schedule in MS Windows NT/2000, make sure
# the scheduler service is running
#
# Microsoft Windows 2000 [Version 5.00.2195]
# (C) Copyright 1985-2000 Microsoft Corp.
# C:\>at 6:00pm /every:M,T,W,Th,F,S,Su c:/usr/local/bin/freenet_request.pl favorites
# Added a new job with job ID = 1
#
# C:\>at
# Status ID   Day                     Time          Command Line
# -------------------------------------------------------------------------------
#         1   Each M T W Th F S Su    6:00 PM       c:/usr/local/bin/freenet_request.pl favorites
#
# C:\>


# sample UNIX config
# my $pathtojava = "/usr/local/bin/IBMJava2-13/jre/bin/java";
# my $classpath = "/home/extractoman/Freenet/freenet.jar";
# my $fnetport = "32285";
# my $pathtokeys = "/usr/local/bin";

# sample windows config
my $pathtojava = "C:/jdk1.3/bin/java.exe";
my $classpath = "c:/progra~1/freenet/freenet.jar";
my $fnetport = "32285";
my $pathtokeys = "c:/usr/local/bin";

my @favorites;

chomp @ARGV;

if ($ARGV[0]) {

	if ($ARGV[0] eq "favorites" or $ARGV[0] eq "FAVORITES") {

		# get favorites from keys.txt and
		# push onto an array
		undef(@favorites);
		open(KEYLIST, "$pathtokeys/keys.txt");
		while (<KEYLIST>) {
			push(@favorites, $_);
			}
		close(KEYLIST);

		# get all favorites
		foreach (@favorites) {
			chomp($_);
			print "GETTING:  $_\n";
			sleep(3);
			system("$pathtojava -classpath $classpath Freenet.client.RequestClient -serverAddress tcp/127.0.0.1:$fnetport -htl 100 -logging minor $_ c:/progra~1/freenet/key");
			sleep(3);
			}
		} else {
		if ($ARGV[1]) {
			print "GETTING:  $ARGV[0], placing in file $ARGV[1]\n";
			system("$pathtojava -classpath $classpath Freenet.client.RequestClient -serverAddress tcp/127.0.0.1:$fnetport -htl 100 -logging minor $ARGV[0] $ARGV[1]");
			} else {
			print "GETTING:  $ARGV[0] for the screen.\n";
			system("$pathtojava -classpath $classpath Freenet.client.RequestClient -serverAddress tcp/127.0.0.1:$fnetport -htl 100 -logging debugging $ARGV[0]");
			}
		}

	} else {

	print "USAGE: freenet_request.pl <key>\n";
	print "       freenet_request.pl <key> <filename for key data>\n";
	print "       freenet_request.pl favorites\n";

	}
