#!/usr/bin/env perl
# PODNAME: timestamp.pl
# ABSTRACT: Utility to request RFC3161 timestamps for files

#
# Originally based on timestamp 
# (c) 2010–2020 Mantas Mikulėnas <grawity@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# MIT License (also available at <https://spdx.org/licenses/MIT>)
#

use warnings;
use strict;
use 5.020;
use Net::RFC3161::Timestamp;
use Getopt::Long qw(:config bundling no_ignore_case);

sub usage {
    my $tsas=list_tsas();
    
    print "$_\n" for (
    "Usage: timestamp [-h ALGO] INPUT...",
    "",
    "  -h, --hash ALGO",
    "  -p, --policy OID",
    "  -t, --tsa TSA",
    "  -v, --verbose",
    "",
    "Available TSAs: ".join(", ", sort grep {!/\./} keys %{$tsas}),
    );
}

my $tsa = "dfn.de";
my $hash_algo;
my $policy;
my $out_file;
my $verbose;

GetOptions(
    "help" => sub { usage(); exit; },
    "h|hash=s" => \$hash_algo,
    "p|policy=s" => \$policy,
    "t|tsa=s" => \$tsa,
    "v|verbose!" => \$verbose,
) or exit(2);

if (!@ARGV) {
    die("no files provided");
}

for my $in_file (@ARGV) {
    $out_file = "$in_file.ts";
    attest_file($in_file, $out_file, $tsa, $hash_algo, $policy, $verbose);
}

__END__

=pod

=encoding UTF-8

=head1 NAME

timestamp.pl - Utility to request RFC3161 timestamps for files

=head1 VERSION

version 0.020

=head1 AUTHOR

Andreas K. Huettel <dilfridge@gentoo.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Andreas K. Huettel.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
