#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
use Digest::MD5;
use File::Basename;
use File::Copy;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('content-preservation');

my @files = ("encrypted-with-images.pdf", # encrypted
             "inline-images.pdf",
             "lin-special.pdf",
             "object-stream.pdf",
             "hybrid-xref.pdf");
my @flags = (["-qdf",                                           # 1
              "qdf"],
             ["-qdf --normalize-content=n",                     # 2
              "qdf not normalized"],
             ["-qdf --stream-data=preserve",                    # 3
              "qdf not uncompressed"],
             ["-qdf --stream-data=preserve --normalize-content=n", # 4
              "qdf not normalized or uncompressed"],
             ["--stream-data=uncompress",                       # 5
              "uncompresed"],
             ["--normalize-content=y",                          # 6
              "normalized"],
             ["--stream-data=uncompress --normalize-content=y", # 7
              "uncompressed and normalized"],
             ["-decrypt",                                       # 8
              "decrypted"],
             ["-linearize",                                     # 9
              "linearized"],
             ["-allow-weak-crypto -encrypt \"\" owner 128 --",  # 10
              "encrypted"],
             ["-linearize -allow-weak-crypto -encrypt \"\" o 128 --", # 11
              "linearized and encrypted"],
             ["",                                               # 12
              "no arguments"],
             );

my $n_tests = 1 + (@files * @flags  * 2 * 3);
my $n_compare_pdfs = 1 + (@files * @flags * 2);

foreach my $file (@files)
{
    my $base = basename($file, '.pdf');

    foreach my $o (qw(disable generate))
    {
        my $n = 0;
        my $oflags = "--object-streams=$o";
        my $odescrip = "os:" . substr($o, 0, 1);
        my $osuf = ($o eq 'generate' ? "-ogen" : "");
        foreach my $d (@flags)
        {
            my ($flags, $fdescrip) = @$d;
            ++$n;
            system("rm -f *.pnm");

            $td->runtest("$file ($odescrip $fdescrip)",
                         {$td->COMMAND => "qpdf $flags $oflags $file a.pdf"},
                         {$td->STRING => "",
                          $td->EXIT_STATUS => 0});

            $td->runtest("check status",
                         {$td->COMMAND => "qpdf --check a.pdf"},
                         {$td->FILE => "$base.$n$osuf.check",
                              $td->EXIT_STATUS => 0},
                         $td->NORMALIZE_NEWLINES);

            $td->runtest("check with C API",
                         {$td->COMMAND => [qw(qpdf-ctest 1 a.pdf), "", ""]},
                         {$td->FILE => "$base.$n$osuf.c-check",
                          $td->EXIT_STATUS => 0},
                         $td->NORMALIZE_NEWLINES);

            compare_pdfs($td, $file, "a.pdf");
        }
        flush_tiff_cache();
    }
}

$td->runtest("convert inline-images to qdf",
             {$td->COMMAND => "qpdf --static-id --no-original-object-ids" .
                  " --qdf inline-images.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});

compare_pdfs($td, "inline-images.pdf", "a.pdf");

cleanup();
$td->report(calc_ntests($n_tests, $n_compare_pdfs));
