#!/usr/bin/perl

#
# Example unitinfo.txt:
#
# Current Work Unit
# -----------------
# Name: Mini chaperonin
# Tag: P782R16C86F3
# Download time: November 12 17:21:24
# Due time: April 15 17:21:24
# Progress: 38%  [|||_______]
#

use strict;
use vars qw($VERSION %IRSSI);

use Irssi qw(command_bind active_win);
$VERSION = "0.5";
%IRSSI = (
    authors     =>  'Christer Edwards',
    contact     =>  'christeredwards@gmail.com',
    name        =>  'Origami Status',
    Description =>  'Displays current origami work unit progress',
    License     =>  'Public Domain',
    changed     =>  'Nov 20, 2008',
);

opendir DIR, '/var/lib/origami/foldingathome/';
my @procs = grep { /^CPU\d/ } readdir DIR;
closedir DIR;

Irssi::command_bind('origami', 'cmd_origami');

sub print_msg_origami {
    Irssi::active_win()->Irssi::print("@_");
    }

sub cmd_origami {    

my ($option) = @_;
foreach my $number ( @procs ) {
    open(CPU, "<", "/var/lib/origami/foldingathome/$number/unitinfo.txt") or
        die "Unable to read statusfile: $!";
    my @status = <CPU>;

    if ( !$option ) { print_msg_origami "$number: $status[6]"; }
    elsif ( $option eq 'progress' ) { print_msg_origami "$number: $status[6]"; }
    elsif ( $option eq 'full' ) { print_msg_origami "@status"; }
    elsif ( $option eq 'name' ) { print_msg_origami "$number: @status[2,6]"; }
    elsif ( $option eq 'due' ) { print_msg_origami "$number: @status[5,6]"; }
    else { print "Options required: progress|due|name|full"; 
           print "default option: progress"; return 1; }
}
};
