#!/usr/bin/perl
# Copyright (C) 2005-2006 Joshua D. Abraham (jabra@spl0it.org)
#
# 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 2 of the License, or
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# todo.pl
# 1.2
#
# Date: Decemeber 1, 2004
# Purpose: script that keeps track of items on a todo list
# 
# the latest version can be found at:
# http://www.spl0it.org/files/todo.pl
#
use strict;
use warnings;

use IO::All;
my $todo="/home/jabra/svn/misc/todo";
my $count=0;
my @lines = io($todo)->slurp;
foreach (@lines){
    next if /^X\#/;
    $count++ if /^\#/;
}
print "ToDo $count items \n";


while (1){
    my @lines = io($todo)->slurp;
    print << 'END';

* * * * * * * * * * * * * * * * * * *  
*   A)dd Item to ToDo List          *
*   R)emove Item from ToDo List     *
*   L)ist Items on the ToDo List    *
*   C)lean ToDo List                *
*   Q)uit                           *
* * * * * * * * * * * * * * * * * * * 
END
    my $menu = <STDIN>;
    chomp($menu);
    &add_item if ($menu eq 'a' || $menu eq 'A');
    &remove_item if ($menu eq 'r' || $menu eq 'R');
    &show_items if ($menu eq 'l' || $menu eq 'L');
    &clean if ($menu eq 'c' || $menu eq 'C');
    &quit if ($menu eq 'q' || $menu eq 'Q');
}
sub show_items{
    my $count=0;
    my @lines = io($todo)->slurp;
    my $line_num=1;
    foreach (@lines){
            next if /^X\#/ ;
            if ($_ =~ /^\#/){
            print "$line_num $_";
            $line_num++
        }
    }
}
sub clean{
    my $line_num=1;
    foreach (@lines){
        next if /^\#/;
        next unless /^X\#/;
        $_="";
        $line_num++;
    }
    io($todo)->print(@lines);
}
sub remove_item{
    print "Enter the number of item you wish to remove: ";
    my $remove_item=<STDIN>;
    chomp($remove_item);
    my $line_num=1;
    foreach (@lines){
        next if /^X\#/;
        next unless  /^\#/;
        if ($remove_item == $line_num) {
            $_ = "X" . $_;
        }
        $line_num++;
    }
    io($todo)->print(@lines);
}
sub add_item{
    my $count=0;
    my @lines = io($todo)->slurp;
    print "Enter the item you wish to add\n";
    my $add_item=<STDIN>;
    $add_item = '#' .' '. $add_item ;
    $add_item >> io($todo);
}
sub quit{
    exit;
}
