#!/usr/bin/perl -w
#<path2sig: writes MD5 signature for each line read

use Getopt::Long;
use File::Basename;
use Carp;
use strict;
use Digest::MD5 qw/md5_base64 md5_hex/;

use subs qw(manpage myuuid usage version where);
$ENV{'PATH'} = join ":", qw(/bin /usr/bin /usr/local/bin /opt/sfw/bin);

my $myname = basename($0);
$myname =~ s/\.\w*$//;    # strip any extension

my %options;
my @getopt_args = (
    'h|?',    # print usage
    'm',      # print manpage
    's',      # print just the signature
    'u',      # print UUID
    'v',      # print version
    'w',      # print source location
    'x',      # print signature in hex
    );

Getopt::Long::config("noignorecase", "bundling");
usage unless GetOptions(\%options, @getopt_args);

manpage if $options{'m'};
myuuid  if $options{'u'};
version if $options{'v'};
where   if $options{'w'};
usage   if $options{'h'};

if (@ARGV) {
    foreach (@ARGV) {
        process($_);
    }
}
else {
    while (<>) {
        chomp;
        process($_);
    }
}

exit(0);

#---------------------------------------------------------------------
# Process a given string.

sub process {
    my $str = shift;
    my $sig;

    s,//*$,,g;
    $sig = $options{'x'} ? md5_hex($_) : md5_base64($_);
    $options{'s'} ? print "$sig\n" : print "$sig $_\n";
}

#---------------------------------------------------------------------
# Print a usage message from the comments and exit.

sub usage {
    my ($emsg) = @_;
    use Pod::Usage qw(pod2usage);
    warn "$emsg\n" if defined $emsg;
    pod2usage(-verbose => 99, -sections => "NAME|SYNOPSIS|OPTIONS");
}

sub manpage {
    my @args = ("perldoc", "$0");
    exec { $args[0] } @args;          # safe even with one-arg list
    die("should not get here\n");
}

#---------------------------------------------------------------------
# Print the UUID, current version, or source location.

sub myuuid {
    my $UUID = sprintf("%s",
        q$UUID: a882ed05-2ec6-3fbf-bce0-3bac3af1b159 $ =~ /UUID: (.*) /);
    print "$UUID\n";
    exit(0);
}

sub version {
    my $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/);
    my $DATE =
      sprintf("%s", q$Date: 2010-11-02 17:50:13-04 $ =~ /Date: (.*) /);
    print "$myname $VERSION $DATE\n";
    exit(0);
}

sub where {
    my $SOURCE = sprintf("%s",
        q$Source: /home/vogelke/bin/RCS/path2sig,v $ =~ /Source: (.*) /);
    print "$SOURCE\n";
    exit(0);
}

#---------------------------------------------------------------------
__END__

=head1 NAME

path2sig - writes MD5 signature for each line read

=head1 SYNOPSIS

path2sig [-hmsuvwx] [file ...]

=head1 OPTIONS

=over 4

=item B<-h>

Print a brief help message and exit.

=item B<-m>

Print the manual page and exit.

=item B<-s>

Print only the signatures.

=item B<-u>

Print the script UUID and exit.

=item B<-v>

Print the version and exit.

=item B<-w>

Print the source location and exit.

=item B<-x>

Print the signature in hex instead of base64.

=back

=head1 DESCRIPTION

B<path2sig> reads pathnames and writes base64 MD5 hash of each one.
Trailing slashes are trimmed from filenames for consistency.

If filenames are present on the command line, the filenames are
hashed and printed, NOT their contents.

=head1 AUTHOR

 Karl Vogel <vogelke@pobox.com>
 Oasis Systems, Inc.

=cut
