1: #!/usr/bin/perl -w
   2: # Simple notes script.
   3: 
   4: use XML::Simple;
   5: 
   6: print "\nWelcome to jot\n";
   7: print "Type 'h' for help.\n";
   8: do {
   9:     print "%> ";
  10:     my $command = <STDIN>;
  11:     chomp($command);
  12:     $command = short($command);
  13:     &q() if ($command =~ m/^q/);
  14:     &{$command}();
  15: } until $command eq 'q';
  16: 
  17: sub a {
  18:     # Add to a file
  19: 
  20:     while (<*.idea>) {
  21: 	print $_,"\n";
  22:     }
  23:     print "Name of file to edit : ";
  24:     my $cfile = <STDIN>;
  25:     chomp($cfile);
  26:     if (! -e $cfile) {
  27: 	warn "jot : $cfile doesn't exist\n";
  28:     } else {
  29: 	my $simple = XML::Simple->new();
  30: 	
  31: 	$cdata = $simple->XMLin($cfile, searchpath => ".");
  32: 
  33: 	print "Project name(s) : ", $cdata->{name};
  34: 	my $cname = <STDIN>;
  35: 	chomp($cname);
  36: 
  37: 	print "Description : ", $cdata->{desc};
  38: 	my $cdesc = <STDIN>;
  39: 	chomp($cdesc);
  40: 
  41: 	my $oname = $cdata->{name}; # get all the old to concat with new
  42: 	my $odesc = $cdata->{desc};
  43: 
  44: 	%cdata = (name => "$oname$cname",
  45: 		  desc => "$odesc$cdesc");
  46: 
  47: 	my $xml = $simple->XMLout(\%cdata, noattr => 1, rootname => "idea", outputfile => "$cfile");
  48:     }
  49: }
  50: 
  51: sub b {
  52:     # Backup all idea files
  53: 
  54:     while (my $cfile = <*.idea>) {
  55: 	# read, output to $cfile.idea~
  56: 	my $simple = XML::Simple->new(); # this is slow. better way?
  57: 	my $cdata = $simple->XMLin($cfile, searchpath => '.');
  58: 	my $xml = $simple->XMLout($cdata, noattr => 1, rootname => "idea", outputfile => "$cfile~");
  59: 	print "Backup of $cfile unsuccesfull : $!\n" if $!;
  60:     }
  61: }
  62: 
  63: sub d {
  64:     # Delete an idea file
  65: 
  66:     while (<*.idea>) {
  67: 	print $_,"\n";
  68:     }
  69:     print "File to delete : ";
  70:     my $cfile = <STDIN>;
  71:     chomp($cfile);
  72:     unlink($cfile);
  73: }
  74: 
  75: sub h {
  76:     print "a - Add info to a project\n";
  77:     print "b - Backup all project files\n";
  78:     print "d - Delete a project file\n";
  79:     print "h - Display this message\n";
  80:     print "l - List all projects\n";
  81:     print "n - New project\n";
  82:     print "q - Quit\n";
  83:     print "r - Restore project files\n";
  84: }
  85: 
  86: sub l {
  87:     # List all idea files and their contents in quasi-nicely formatted way
  88: 
  89:     while (my $cfile = <*.idea>) {
  90: 	my $simple = XML::Simple->new();
  91: 	print "\n$cfile\n";
  92: 	my $idea = $simple->XMLin($cfile, searchpath => '.');
  93: 	print "\t", $idea->{name}, "\n\t", $idea->{desc}, "\n\t";
  94:     }
  95: }
  96: 
  97: sub n {
  98:     # New project file
  99: 
 100:     my $simple = XML::Simple->new();
 101:     print "Project name(s) : ";
 102:     my $cname = <STDIN>;
 103:     chomp($cname);
 104:     print "Description : ";
 105:     my $cdesc = <STDIN>;
 106:     chomp($cdesc);
 107: 
 108:     my $ofile = short($cname);
 109: 
 110:     my %pdata = (name => $cname,
 111: 		 desc => $cdesc,);
 112: 
 113:     my $xml = $simple->XMLout(\%pdata, noattr => 1, rootname => "idea", outputfile => "$ofile.idea");
 114: }
 115: 
 116: sub q {
 117:     print "bye\n";
 118:     exit;
 119: }
 120: 
 121: sub r {
 122:     # Restore backup files
 123: 
 124:     my $simple = XML::Simple->new();
 125:     while (my $cfile = <*.idea~>) {
 126: 	my $ofile = $cfile;
 127: 	chop($ofile); # remove the ~
 128: 
 129: 	my $cdata = $simple->XMLin($cfile, searchpath => ".");
 130: 	my $xml = $simple->XMLout($cdata, noattr => 1, rootname => "idea", outputfile => $ofile);
 131: 
 132: 	print "Restoration of $cfile unsuccesfull : $!\n" if $!;
 133:     }
 134: }
 135: 
 136: sub short {
 137:     (my $word) = @_;
 138:     $word =~ s/\W.*//;
 139:     $word =~ tr/A-Z/a-z/;
 140:     return $word;
 141: }