#!/usr/bin/env perl
#
# SlimRat 
# v0.9.1 (2008-09-20)
# Přemek Vyhnal <premysl.vyhnal gmail com> 2009 
# public domain
#
# Thanks:
# hasant.di.kampung.edan - wget options
#


use FindBin qw($RealBin);
use lib $RealBin;

use Getopt::Long;
use Term::ANSIColor qw(:constants);
use Plugin;
use Toolbox;
use strict;
use warnings;
use Data::Dumper;

my ($list, $link, @links, $check, @failedlinks, @oklinks, $download_to, $wget_opts, $unpack, @pass);

sub usage { print "
SlimRat 

Downloads files from rapidshare and other servers to your working directory. Files to download are specified by download links like 'http://rapidshare.com/files/012345678/somefile.xxx' as arguments on command line or one per line in file given as --list option ('-' for stdin works too).

USAGE:
  $0 [options] [link [link [...]]]
		
OPTIONS:
  -h | --help              this help
  -l | --list   filename   load links from file (one per line)
  -c | --check             only check links, don't download them
  -t | --to     directory  destination directory (where to download)
  -w | --wget              wget options, enclosed in quotes
                           slimrat -w '--limit-rate=20k' -l file-list
"; exit; }

#  -u | --unpack            unpack downloaded archives
#  -p | --password          archive password


	GetOptions (
			"help!"		=> \&usage,
			"check!"	=> \$check,
			"list=s"	=> \$list,
			"to=s"		=> \$download_to,
			"wget=s"    => \$wget_opts,
			"unpack!"	=> \$unpack,
			"pass=s@"	=> \@pass,
			);

&usage if(!scalar @ARGV && !$list); 

if($list){ # load links from file
	if(open (LIST, $list)){
		m/^\s*(\S+)\s*/ and push @links, $1 while(<LIST>);
		close LIST;
	} else {warn "Can't open '$list'\n";}
}

push @links, $link while($link = shift); # read links from command line



my $return = 0; # navratova hodnota

print &ptime."Slimrat started\n";

if($check){
	foreach $link (@links) {
		check($link);
		print YELLOW, $link, RESET, " (", Plugin::get_name($link), ")\n";
	}
} else {
	$SIG{'INT'} = 'quit';
	foreach $link (@links) {
		if (download($link)) {
			push @oklinks, $link;
			# after-one-download command from config file
		} else {
			push @failedlinks, $link;
		}
	}
	# after-all-downloads command from config file
	&quit;
}

print "\n".&ptime."Slimrat finished\n";





sub quit {
	if(scalar @oklinks){
		print GREEN, "\nDOWNLOADED:\n", RESET;
		print $_,"\n" foreach @oklinks;
		print "to '",YELLOW, $download_to, RESET, "'\n" if ($download_to);
	}
	if(scalar @failedlinks){
		print RED, "\nFAILED:\n", RESET;
		print $_,"\n" foreach @failedlinks;
	}
}



sub check{
	$link = shift;
	my $pluginname = Plugin::get_name($link);
	my $check = eval $pluginname."::check('$link')";
	$check ||=0;
	if($check>0) {print GREEN,  "[ALIVE] ", RESET;}
	elsif($check<0) {print RED, "[DEAD]  ", RESET; $return++}
	else {print GREEN, "[?]     ", RESET;}
	return $check;
}

sub download{
	$link = shift;

	my $pluginname = Plugin::get_name($link);
	print &ptime."Downloading ", YELLOW, $link, RESET, " ($pluginname) ";

	if (check($link) < 0){
		print RED "\n".&ptime."Download failed (dead link)\n";
		return 0;
	}
	print "\n";
	if((my $fileurl) = eval $pluginname."::download('$link')"){ # rename download to get_link????
		my $command = 'wget ' .
			"-U '$useragent' ".
			($wget_opts ? qq($wget_opts ) : '') .
			($download_to? " -P '$download_to' " : "") .
			" '$fileurl'";

		if(!system($command)){ #success
			print GREEN &ptime."File downloaded", RESET, "\n\n";
			return 1;
		} else { # wget failed
			print RED &ptime."Download failed\n\n";
			return 0;
		}
	} else { # get fileurl failed
		print RED &ptime."Plugin failed\n\n";
		return 0;
	}
}


exit($return);
