#!/usr/bin/perl -w
#
# $Revision: 1.5 $ $Date: 2012-07-26 17:11:48-04 $
# $Source: /home/vogelke/bin/RCS/mtime,v $
# $Host: sys7.com $
# $UUID: 648fd305-2527-3348-9c54-ae5114c935b3 $
#
#<mtime: get or set file modification time

use strict;
use Getopt::Long;
use File::Basename;
use Pod::Usage;
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 $flag;
my %options;
my @getopt_args = (
    'h|?',    # print usage
    'm',      # print manpage
    's',      # set modtime
    'u',      # print UUID
    'v',      # print version
    'w',      # print source location
    );

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'};

$flag = $options{'s'} ? 'set' : 'get';

my (
    $atime, $ctime, $dev,   $file, $gid,  $ino,
    $mode,  $mtime, $nlink, $rdev, $size, $uid
    );

# Get or set file access or modtimes.

if ($flag eq 'get') {
    unless (@ARGV) {
        warn "Need some files for arguments, see $myname -m\n";
        exit(1);
    }

    foreach $file (@ARGV) {
        next unless -f $file;

        (
            $dev,  $ino,  $mode,  $nlink, $uid, $gid,
            $rdev, $size, $atime, $mtime, $ctime
        ) = stat(_);

        print "$mtime\t$atime\t$file\n";
    }
}
elsif ($flag eq 'set') {
    while (<>) {
        chomp;
        if (/(\d+)\t(\d+)\t(.*)/) {
            $mtime = $1;
            $atime = $2;
            $file  = $3;
            utime($atime, $mtime, $file) || die "utime $file: $!\n";
        }
        else {
            warn "$_: messed-up line\n";
        }
    }
}

exit(0);

#---------------------------------------------------------------------
# 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: 9ac36f67-e5e2-3a82-927f-5db740fe9713 $ =~ /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: 2012-07-26 17:11:48-04 $ =~ /Date: (.*) /);
    print "$myname $VERSION $DATE\n";
    exit(0);
}

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


__END__

=head1 NAME

mtime - get or set file modification times

=head1 SYNOPSIS

mtime [-hmuvw]

mtime file [file ...]

mtime -s [list]

=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>

Read its own default output and set file modtimes appropriately.

=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.

=back

=head1 DESCRIPTION

With no options, B<mtime> will read files on the command line and print
modtimes to stdout in the form
  "mtime <tab> atime <tab> pathname"

Use "-s" to set the file modification times.

=head1 AUTHOR

 Karl Vogel <vogelke@pobox.com>
 Array Infotech, Inc.

=cut
