#!/usr/bin/perl

use LWP::Simple;
use XML::Simple;

my $rss = get("http://woot.com/salerss.aspx");
my $xml = new XML::Simple;
my $data = $xml->XMLin($rss);
my $ret = "";

my $img = grep(/^img$|^image$/i, @ARGV);
my $buy = grep(/^buy$|^buynow$|^purchase$|^purch$/i, @ARGV);
my $pct = grep(/^pct$|^percent$/i, @ARGV);
my $help = grep(/^help$/i, @ARGV);

if ($help) {
	my $fname = $0;
	$fname =~ s/.*\///;
	print "Usage: $fname [img|buy|pct|help]\n";
	print "  options: img - show image URL (alt: image)\n";
	print "           buy - show \"Buy Now\" URL (alt: buynow|purchase|purch)\n";
	print "           pct - show percent sold (alt: percent)\n";
	print "           help - show this help\n";
	print "\n";

	exit 0;
}

if ($$data{"channel"}{"item"}{"woot:wootoff"} eq "True") {
	$ret .= "WOOT OFF!\n";
} else {
	$ret .= "Daily Woot:\n";
}
$ret .= $$data{"channel"}{"item"}{"title"} . ": " . $$data{"channel"}{"item"}{"woot:price"} ."\n";
if ($pct) {
	if ($$data{"channel"}{"item"}{"woot:soldout"} eq "True") {
        	$ret .= "SOLD OUT!\n";
	} else {
		my $pct = (eval $$data{"channel"}{"item"}{"woot:soldoutpercentage"} * 100);
		$ret .= "$pct% sold\n";
	}
}
if ($img) { $ret .= $$data{"channel"}{"item"}{"woot:standardimage"} . "\n"; }
if ($buy) { $ret .= $$data{"channel"}{"item"}{"woot:purchaseurl"} ."\n"; }

binmode STDOUT, ":utf8";
print $ret;

