| 1 |
#!/usr/bin/perl |
| 2 |
# $Id: add-memo,v 3.1 2002/02/08 13:55:59 arensb Exp $ |
| 3 |
use strict; |
| 4 |
use Palm::Memo; |
| 5 |
|
| 6 |
use vars qw( $category $fname $pdb $cat_id $memo_text ); |
| 7 |
|
| 8 |
# Set default values |
| 9 |
$category = undef; # Default category to add to: "Unfiled" |
| 10 |
$cat_id = 0; # Default category index is 0 |
| 11 |
$fname = "$ENV{HOME}/.palm/backup/MemoDB.pdb"; |
| 12 |
# Default file to modify |
| 13 |
|
| 14 |
# Parse command-line arguments |
| 15 |
# XXX - Ought to read ~/.add-memo.args or some such: this should |
| 16 |
# contain the default command-line arguments. read it, split into |
| 17 |
# arguments, and prepend to @ARGV. Then continue as below. |
| 18 |
while ($ARGV[0] =~ /^-./) |
| 19 |
{ |
| 20 |
my $arg = shift; |
| 21 |
|
| 22 |
if ($arg eq "-h" or $arg eq "-help" or $arg eq "--help") |
| 23 |
{ |
| 24 |
&usage; |
| 25 |
exit 0; |
| 26 |
} |
| 27 |
|
| 28 |
if ($arg eq "-c") # Set category |
| 29 |
{ |
| 30 |
$category = shift; |
| 31 |
if (!defined($category)) |
| 32 |
{ |
| 33 |
print STDERR |
| 34 |
"Error: -c argument requires an argument.\n"; |
| 35 |
&usage; |
| 36 |
exit 1; |
| 37 |
} |
| 38 |
next; |
| 39 |
} |
| 40 |
|
| 41 |
if ($arg eq "-f") # Specify PDB file |
| 42 |
{ |
| 43 |
$fname = shift; |
| 44 |
if (!defined($fname)) |
| 45 |
{ |
| 46 |
print STDERR |
| 47 |
"Error: -f argument requires an argument.\n"; |
| 48 |
&usage; |
| 49 |
exit 1; |
| 50 |
} |
| 51 |
next; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
$pdb = new Palm::PDB; |
| 56 |
$pdb->Load($fname); |
| 57 |
|
| 58 |
# Given a category name, set $cat_id to its index. |
| 59 |
if ($category eq "") |
| 60 |
{ |
| 61 |
# No category specified. Default to "Unfiled" |
| 62 |
$cat_id = 0; |
| 63 |
} else { |
| 64 |
# Find the named category |
| 65 |
my $i; |
| 66 |
for ($i = 0; $i <= 16; $i++) |
| 67 |
{ |
| 68 |
if ($pdb->{appinfo}{categories}[$i]{name} eq $category) |
| 69 |
{ |
| 70 |
$cat_id = $i; |
| 71 |
last; |
| 72 |
} |
| 73 |
} |
| 74 |
if ($i >= 16) |
| 75 |
{ |
| 76 |
# No such category |
| 77 |
print STDERR "Warning: can't find category \"$category\".\n", |
| 78 |
"Defaulting to \"Unfiled\".\n"; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$memo_text = ""; |
| 83 |
|
| 84 |
# This loop might iterate over several files |
| 85 |
while (<>) |
| 86 |
{ |
| 87 |
# XXX - If input file is STDIN, perhaps ought to print a |
| 88 |
# message saying to hit <Ctrl-D> at EOF. Then again, that's |
| 89 |
# not the Unix way. |
| 90 |
$memo_text .= $_; |
| 91 |
if (eof(ARGV)) |
| 92 |
{ |
| 93 |
# End of current file. Append the current record to |
| 94 |
# the PDB, and reset $memo_text for the next file. |
| 95 |
|
| 96 |
my $record; |
| 97 |
|
| 98 |
$record = $pdb->append_Record; |
| 99 |
$record->{data} = $memo_text; |
| 100 |
$record->{category} = $cat_id; |
| 101 |
$memo_text = ""; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$pdb->Write($fname); |
| 106 |
|
| 107 |
# usage |
| 108 |
# Print a usage message |
| 109 |
sub usage |
| 110 |
{ |
| 111 |
print <<EOT; |
| 112 |
Usage: $0 [options] [files] |
| 113 |
Options: |
| 114 |
-h Help |
| 115 |
-c category Category to add to |
| 116 |
-f fname PDB file to modify |
| 117 |
EOT |
| 118 |
} |