#!/usr/bin/env perl
use 5.014;
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use JSON::PP ();
use PDF::ISO32000::Audit;

our $VERSION = '1.00';

my ($opt_json, $opt_cdx, $opt_strip, $opt_help, $opt_ver);
GetOptions(
    'json'                => \$opt_json,
    'cyclonedx'           => \$opt_cdx,
    'strip-incremental=s' => \$opt_strip,
    'help|h'              => \$opt_help,
    'version|v'           => \$opt_ver,
) or die "Usage: pdf-iso32000-audit [--json] [--cyclonedx] [--strip-incremental OUT.pdf] FILE.pdf\n";

if ($opt_ver) {
    print "pdf-iso32000-audit v$VERSION (PDF::ISO32000::Audit)\n";
    exit 0;
}

if ($opt_help || !@ARGV) {
    print <<"USAGE";
Usage: pdf-iso32000-audit [OPTIONS] <file.pdf>

Options:
  --json                     Output full ISO 32000 & PDF/UA-1 audit report as JSON
  --cyclonedx                Output ECMA-424 CycloneDX v1.6 Document SBOM/CBOM JSON
  --strip-incremental <out>  Write sanitized baseline revision (without trailing %%EOF updates)
  --version, -v              Print version
  --help, -h                 Show this help message
USAGE
    exit($opt_help ? 0 : 1);
}

my $input = shift @ARGV;
open my $fh, '<:raw', $input or die "Cannot read '$input': $!\n";
local $/;
my $bytes = <$fh>;
close $fh;

my $auditor = PDF::ISO32000::Audit->new();

if (defined $opt_strip) {
    my $clean = $auditor->sanitize_incremental($bytes);
    open my $out, '>:raw', $opt_strip or die "Cannot write '$opt_strip': $!\n";
    print {$out} $clean;
    close $out;
    printf "Wrote sanitized baseline revision (%d -> %d bytes) to %s\n",
        length($bytes), length($clean), $opt_strip;
    exit 0;
}

if ($opt_cdx) {
    print $auditor->generate_cyclonedx_sbom($bytes, filename => $input);
    exit 0;
}

my $report = $auditor->inspect_bytes($bytes, filename => $input);
if ($opt_json) {
    print JSON::PP->new->utf8->canonical->pretty->encode($report);
    exit 0;
}

printf "File:                %s (%d bytes)\n", $report->{filename}, $report->{size_bytes};
printf "SHA-256:             %s\n", $report->{sha256};
printf "Valid PDF Header:    %s (Version: %s)\n",
    ($report->{valid_pdf} ? 'YES' : 'NO'), ($report->{pdf_version} // 'N/A');
printf "Estimated Pages:     %d\n", $report->{page_count_estimate};
printf "Incremental Saves:   %d revision(s)%s\n",
    $report->{incremental}{revision_count},
    ($report->{incremental}{has_incremental_updates} ? ' [INCREMENTAL UPDATES PRESENT]' : '');
printf "PDF/UA-1 Score:      %d/100 (Candidate: %s, Lang: %s)\n",
    $report->{pdfua}{score},
    ($report->{pdfua}{is_pdfua_candidate} ? 'YES' : 'NO'),
    ($report->{pdfua}{lang} // 'undeclared');
printf "Encryption:          %s\n", $report->{security}{encryption}{algorithm};
if (@{ $report->{security}{active_content} }) {
    printf "Active Risk Flags:   %s\n", join(', ', @{ $report->{security}{active_content} });
}

exit 0;

__END__

=head1 NAME

pdf-iso32000-audit - Command-line ISO 32000 PDF inspector, PDF/UA-1 accessibility checker, and CycloneDX SBOM generator

=head1 SYNOPSIS

    pdf-iso32000-audit document.pdf
    pdf-iso32000-audit --json document.pdf
    pdf-iso32000-audit --cyclonedx document.pdf > document.cdx.json
    pdf-iso32000-audit --strip-incremental clean.pdf redacted.pdf

=head1 AUTHOR

Islam Ayoub (UTILVO) E<lt>info@utilvo.comE<gt>

=cut
