#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

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

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

require TestDriver;

cleanup();

my $td = new TestDriver('replace-input');

my $n_tests = 8;

# Use Unicode file names to test replace input so we can be sure it
# works for that case.
$td->runtest("create unicode filenames",
             {$td->COMMAND => "test_unicode_filenames"},
             {$td->STRING => "created Unicode filenames\n",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

foreach my $d (['auto-ü', 1], ['auto-öπ', 2])
{
    my ($u, $n) = @$d;
    $td->runtest("replace input $u",
                 {$td->COMMAND => "qpdf --deterministic-id" .
                      " --object-streams=generate --replace-input ./$u.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("check output ($u)",
                 {$td->FILE => "$u.pdf"},
                 {$td->FILE => "replace-input.pdf"},
                 $td->NORMALIZE_NEWLINES);
}

system("cp xref-with-short-size.pdf auto-warn.pdf") == 0 or die;
$td->runtest("replace input with warnings",
             {$td->COMMAND =>
                  "qpdf --deterministic-id --replace-input ./auto-warn.pdf"},
             {$td->FILE => "replace-warn.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);

$td->runtest("check output",
             {$td->FILE => "auto-warn.pdf"},
             {$td->FILE => "warn-replace.pdf"});
$td->runtest("check orig output",
             {$td->FILE => "auto-warn.pdf.~qpdf-orig"},
             {$td->FILE => "xref-with-short-size.pdf"});

cleanup();
$td->report($n_tests);
