#!/usr/bin/perl -w
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    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, see <http://www.gnu.org/licenses/>.
#
#  Folding @ Home script for irssi; requires curl installed
#
#*********************************** 
# IMPORTANT: SET UP YOUR F@H-PATH! *
#***********************************

use strict;
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = '1.10';
%IRSSI = (
    authors     => 'Haui',
    contact     => 'haui45@web.de',
    name        => 'fah stats',
    description => 'This script allows ' .
                   'you to print your ' .
                   'folding @ home stats in irssi',
    license     => 'GPL',
);

#********************************************************************
my $fahpath="/path/to/fah/";	 	    #change to your f@h-path*
#********************************************************************


my ($wu, $dltime, $duetime, $progress, $name, $team, @stats, $string, $line);
`which curl` or die "fatal...curl executable not found....";

Irssi::print("fah stats successfully loaded! Response time of the script may vary, see code for details.");

 sub irssi_stuff {
	# server - the active server in window
	# witem - the active window item (eg. channel, query)
	#         or undef if the window is empty
	my ($data, $server, $witem) = @_;

	if (!$server || !$server->{connected}) {
		Irssi::print("Not connected to server");
		return;
	}
	my $ret = fah();
	if ($ret == -1){
		Irssi::print ("Please set your f\@h path!");
		return;
	}
	elsif ($ret == -2){
		Irssi:print("Problem with unitinfo.txt...is your path correct?");
		return;
	}
	elsif ($ret == -3){
		Irssi:print("Problem with client.cfg...is your path correct?");
		return;
	}
	elsif ($ret == -4){
		Irssi:print("something went wrong....fix it! :-) ");
		return;
	}
	if ($witem && ($witem->{type} eq "CHANNEL" ||
			$witem->{type} eq "QUERY")) {
		# there's query/channel active in window
		$witem->command("SAY $string");
	} else {
		Irssi::print("No active channel/query in window");
	}    
}

sub fah {
	my $curl = "curl &> /dev/null http://vspx27.stanford.edu/teamstats/team";
	my $unitinfo = $fahpath . "/unitinfo.txt";
	my $cfg = $fahpath . "/client.cfg";

	if ($unitinfo =~ m/^\/path\/to\/fah/){		#just in case...
		return -1;
	}

	open(INFILE, "<", $unitinfo) or return -2;

	while ($line = <INFILE>){
		if ($line =~ m/^Name:/) {$wu=$line; chomp($wu);}
		elsif ($line =~ m/^Download/)  {$dltime=$line; chomp($dltime);}
		elsif ($line =~m/^Due/) {$duetime=$line; chomp($duetime);}
		elsif ($line =~m/^Progress/) {$progress=$line; chomp($progress);}
	}
	close INFILE;

	open (INFILE,"<", $cfg) or return -3;

	while($line = <INFILE>){
		if ($line =~ m/^username/){	#get username
			$name = $line;
			$name =~ s/username=//;
			chomp($name);
		}
		if ($line =~ m/^team/){		#get Team-Id
			$team=$line;
			$team =~ s/team=//;
			chomp($team);
			last;
		}
	}
	close INFILE;
	if ($team == 446 || $team == 0){			#remove this if your connection is fast enough...
		die "teamstats file is too large ;p\n";		#
	}							#

	$curl=$curl.$team.".txt > /tmp/team.txt";
	system($curl); 

	 
	open(INFILE, "<", "/tmp/team.txt") or return -4;
	my @array = <INFILE>;
	@array = grep /\t$name\t/, @array;
	@stats = split /\t/, $array[0];



$string = "[stats] User: $name - Team: $team - team rank: $stats[1] - overall rank: $stats[0] - credit: $stats[3] || [wu] $wu - $dltime - $duetime - $progress";

unlink("/tmp/team.txt");
}

Irssi::command_bind('fah', 'irssi_stuff');
