#!/bin/sh
#! -*- perl -*-
eval 'exec perl -x -S $0 ${1+"$@"} ;'
if 0;
# Above lines are for portable Perl startup honoring PATH and emacs. Don't
# change lightly. Options may be inserted before "-x". For background see
# "perldoc perlrun" and http://cr.yp.to/slashpackage/studies/findingperl/7 .
# Developers may need to run as "perl -x -Mblib ..." or "perl -x -Ilib ...".
use 5.006;
use strict;
use warnings;
my $VERSION = $File::Namaste::VERSION;
# nam - get and set Namaste tags
#
# Do "perl nam --man" for description.
use Getopt::Long qw(:config bundling_override);
use Pod::Usage;
use File::Namaste;
use File::ANVL;
use File::ERC;
my %opt = (
help => 0,
man => 0,
version => 0,
directory => 0,
format => 0,
verbose => 0,
);
# main
{
GetOptions(\%opt,
'help|?',
'man',
'version',
'format|m=s', # one of plain, anvl, xml
'directory|d=s',
'verbose|v',
) or pod2usage(1);
pod2usage(1)
if $opt{help};
pod2usage(-exitstatus => 0, -verbose => 2)
if $opt{man};
print "$VERSION\n" and exit(0)
if $opt{version};
@ARGV > 0 or
pod2usage("$0: not enough arguments");
my $om = "om_" . # initialize output multiplexor
($opt{format} || "plain");
defined(&$om) or pod2usage("$0: unknown format: $opt{format}");
my $dir = $opt{directory} || "";
my $cmd = lc(shift @ARGV);
my ($num, $fname, $fvalue, $msg, @nnv);
my $delete = 0;
if ($cmd eq "add") { # easiest, since don't need to check existence
@ARGV > 1 or
pod2usage("$cmd: needs at least two arguments");
($msg = set_namaste($dir, @ARGV)) and
die $msg;
exit 0;
}
if (($delete = $cmd eq "rm") || $cmd eq "set") {
pod2usage("$cmd what? (more arguments needed)")
if (@ARGV == 0);
# same args for command as for get_namaste()
@nnv = ($delete
? get_namaste($dir, @ARGV) # delete all args or
: get_namaste($dir, $ARGV[0])); # just 1st for "set"
# @nnv may contain multiple tags (even for "set") to delete
while (defined($num = shift @nnv)) {
$fname = shift @nnv;
$fvalue = shift @nnv;
unlink($fname) or
print STDERR "$fname: $!";
}
$delete and exit 0; # since we're done
($msg = set_namaste($dir, @ARGV)) and # "set" case
die $msg;
exit 0;
}
if (($delete = $cmd eq "rmall") || $cmd eq "get") {
# same args for command as for get_namaste()
@nnv = get_namaste($dir, @ARGV);
while (defined($num = shift @nnv)) {
$fname = shift @nnv;
$fvalue = shift @nnv;
if ($delete) { # we're doing a delete
unlink($fname) or
print STDERR "$fname: $!";
next;
}
# XXXXX map nums to kernel tags (1->who, etc)
# different kinds of output, eg, XML
no strict 'refs'; # permits next call
print &$om(DATA,
($num == 0 ? "dir_type" : num2dk($num)),
$fvalue);
$opt{verbose} and
print &$om(NOTE, "file", $fname);
}
exit 0;
}
pod2usage("$cmd: unrecognized command");
}
__END__
=pod
=for roff
.nr PS 12p
.nr VS 14.4p
=head1 NAME
nam - set, get, and remove Namaste tag files
=head1 SYNOPSIS
=over
=item B<nam> [B<-vh>] [B<-m> I<fmt>] [B<-d> I<dir>] B<set|add> I<integer> I<string> [[I<maxlen>] I<ellipsis>]
=item B<nam> [B<-vh>] [B<-m> I<fmt>] [B<-d> I<dir>] B<get|rm> [I<integer> ...]
=item B<nam> [B<-vh>] [B<-m> I<fmt>] [B<-d> I<dir>] B<rmall>
=back
=head1 DESCRIPTION
The B<nam> command manages Namaste tag files. A Namaste (Name-as-text)
tag file holds a single metadata value and its filename is derived from
the metadata value.
=head1 OPTIONS
=over
=item B<-d>, B<--directory>
Use I<directory> instead of the current directory to look for tag files.
=item B<-m>, B<--format>
Output in format I<fmt>, one of C<plain> (unlabeled),
C<anvl>, or C<xml>. Default format is C<plain>.
=item B<-v>, B<--verbose>
Output ancillary information (the tag filename itself) as a comment.
=item B<-h>, B<--help>
Print extended help documentation.
=item B<--man>
Print full documentation.
=item B<--version>
Print the current version number and exit.
=back
=head1 EXAMPLES
nam set 0 bagit_0.98
nam set 1 'Mark Twain'
nam set 2 'Adventures of Huckleberry Finn' 13m
nam get
nam rmall
=head1 SEE ALSO
rm(1)
=head1 AUTHOR
John Kunze I<jak at ucop dot edu>
=head1 COPYRIGHT
Copyright 2009 UC Regents. Open source Apache License, Version 2.
=begin CPAN
=head1 README
Manage Namaste tag files.
=head1 SCRIPT CATEGORIES
=end CPAN
=cut
# LocalWords: LocalWords Getopt GetOptions ARGV