#!/usr/bin/perl

# command line todo script

$tdfile="/Users/31d1/.todo.txt";
$dnfldr="/Users/31d1/Documents/done/";

sub help() {
 $fn = $0;
 $fn =~ s/.*\///;
 print "$fn -[hlxz] | -ap [priority] text | -dg regex\n";
 print "     -a [0-9_] <text> - add text with optional priority\n";
 print "     -p [0-9_] <regex> - set priority\n";
 print "     -d regex - mark matching lines as done\n";
 print "     -g regex - only show lines starting with regex\n";
 print "     -h - this help message\n";
 print "     -l - status\n";
 print "     -x - archive done items\n";
 print "     -z - show done items\n";
}

if( $ARGV[0] eq "-h" ) {
 help();
 exit;
}

$dt = `date +'%Y %b %d'`; 
$dt =~ s/\n//;
open(F,"$tdfile");
@lines = <F>;
close F;

# add
if( $ARGV[0] eq "-a" ) {
 shift;
 if( "@ARGV" eq "" ) {
  help();
  exit;
 }
 if( $ARGV[0] =~ /^[0-9]$/ ) {
  $st = $ARGV[0];
  shift;
  push(@lines, "$st $dt @ARGV\n");
 } else {
  push(@lines, ": $dt @ARGV\n");
 }
 %seen = ();
 foreach $line (@lines) {
  $item = $line;
  $item =~ s/^. .... ... .. //;
  push(@uniq, $line) unless $seen{$item}++;
 }
 @sorted = sort @uniq;
 open(F,">$tdfile");
 print F @sorted;
 close F;
}

# mark done
if( $ARGV[0] eq "-d" ) {
 shift;
 if( "@ARGV" eq "" ) {
  help();
  exit;
 }
 foreach $line (@lines) {
  if( $line !~ /@ARGV/ ) {
   push(@same, $line);
  } else {
   push(@done, $line);
  }
 }
 open(F,">$tdfile") || die("Cannot Open File");
 print F @same;
 close F;
 open(F,">>$tdfile") || die("Cannot Open File"); 
 foreach $dn (@done) {
  $dn =~ s/^. .... ... ../_ $dt/;
  print F $dn;
 }
 close F;
}

# prioritize
if( $ARGV[0] eq "-p" ) {
 shift;
 if( $ARGV[0] =~ /^[0-9:]$/ ) {
  $nwstat = $ARGV[0];
 } else {
  help();
  exit;
 }
 shift;
 foreach $line (@lines) {
  if( $line =~ /@ARGV/ ) {
   $line =~ s/^./$nwstat/;
  }
  push(@uniq, $line);
 }
 @sorted = sort @uniq;
 open(F,">$tdfile") || die("Cannot open file"); 
 print F @sorted;
 close F;
}

# stats
if( $ARGV[0] eq "-l" ) {
 for $line (@lines) {
  if( $line =~ /^[0-9]/ ) {
   $a++;
  } elsif( $line =~ /^:/ ) {
   $b++;
  } else {
   $c++;
  }
 }
 if( ($a + $b + $c ) == 0 ) {
  exit;
 }
 $j = int((100 * ($a/($a + $b + $c))) + .5);
 $k = int((100 * ($b/($a + $b + $c))) + .5);
 $l = int((100 * ($i/($a + $b + $c))) + .5);
 $m = 100 - ($j +$k);
 if( $a != 1 ) {
  $s = "s";
 } 
 if( $a == 0 ) {
  $a = "no";
 }
 print "$a urgent item$s, $m% done [";
 while( $n < ($j/2) ) {
  print "<";
  $n++;
 } 
 while( $n < (($j +$k)/2) ) {
  print ">";
  $n++;
 } 
 while( $n < 50 ) {
  print " ";
  $n++;
 } 
 print "]\n";
 exit;
}

open(F,"$tdfile");

# show matching
if( $ARGV[0] eq "-g" ) {
 shift;
 while(<F>) {
  print if( /^@ARGV/ );
 }
 close F; 
 exit;
}

# show done
if( $ARGV[0] eq "-z" ) {
 while(<F>) {
  print if( /^_/ );
 }
 close F; 
 exit;
}

# archive
if( $ARGV[0] eq "-x" ) {
 $dm = `date +'%Y%b'`; 
 open(F,">$tdfile") || die("Cannot open file");
 open(G,">>$dnfldr$dm") || die("Cannot open file");
 for $line (@lines) {
  if( $line =~ /^_/ ) {
   print G $line;
   $i++;
  } else {
   print F $line;
  }
 }
 close F; 
 close G; 
 if( $i != 1 ) {
  $s = "s";
 } 
 if( $i == 0 ) {
  $a = "no";
 } 
 print "$i$a item$s archived\n";
 exit;
}

while(<F>) {
 print if( ! /^_/ );
}
close F;