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

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

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

require TestDriver;

cleanup();

my $td = new TestDriver('arg-parsing');

my $n_tests = 17;

$td->runtest("required argument",
             {$td->COMMAND => "qpdf --password minimal.pdf"},
             {$td->REGEXP => "must be given as --password=pass",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("required argument with choices",
             {$td->COMMAND => "qpdf --decode-level minimal.pdf"},
             {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("required argument with choices",
             {$td->COMMAND => "qpdf --decode-level minimal.pdf"},
             {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
copy("minimal.pdf", '@file.pdf');
$td->runtest("\@file exists and file doesn't",
             {$td->COMMAND => "qpdf --check \@file.pdf"},
             {$td->FILE => "check-at-file.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("missing underlay filename",
             {$td->COMMAND => "qpdf --underlay --"},
             {$td->REGEXP => ".*underlay file not specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("extra overlay filename",
             {$td->COMMAND => "qpdf --overlay x x --"},
             {$td->REGEXP => ".*overlay file already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("multiple pages options",
             {$td->COMMAND => "qpdf --pages . --password=x -- --pages . --"},
             {$td->REGEXP => ".*--pages may only be specified one time.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("bad numeric range detects unclosed --pages",
             {$td->COMMAND => "qpdf --pages . --pages . --"},
             {$td->REGEXP => ".*pages options must be terminated with --.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("bad file detected as unclosed --pages",
             {$td->COMMAND => "qpdf --pages . 1 --xyz out"},
             {$td->REGEXP => ".*pages options must be terminated with --.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("misplaced pages password 1",
             {$td->COMMAND => "qpdf --pages . 1 --password=z --"},
             {$td->REGEXP => ".*password must immediately follow a file name.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("misplaced pages password 2",
             {$td->COMMAND => "qpdf --pages --password=z . 1 --"},
             {$td->REGEXP => ".*password must immediately follow a file name.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("duplicated pages password",
             {$td->COMMAND => "qpdf --pages . --password=z --password=z --"},
             {$td->REGEXP => ".*password already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v1-only objects json-key",
             {$td->COMMAND => "qpdf --json=2 --json-key=objects minimal.pdf"},
             {$td->REGEXP => ".*\"objects\" and \"objectinfo\" are " .
                  "only valid for json version 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v1-only objectinfo json-key",
             {$td->COMMAND => "qpdf --json=2 --json-key=objectinfo minimal.pdf"},
             {$td->REGEXP => ".*\"objects\" and \"objectinfo\" are " .
                  "only valid for json version 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v2-only qpdf json-key",
             {$td->COMMAND => "qpdf --json=1 --json-key=qpdf minimal.pdf"},
             {$td->REGEXP => ".*\"qpdf\" is only valid for json version > 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
# Ignoring -- at the top level was never intended but turned out to
# have been there for a long time so that people relied on it. It is
# intentionally not documented.
$td->runtest("ignore -- at top level",
             {$td->COMMAND => "qpdf -- --check -- minimal.pdf --"},
             {$td->REGEXP => ".*No syntax or stream encoding errors found.*",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("empty and replace-input",
             {$td->COMMAND => "qpdf --empty --replace-input"},
             {$td->REGEXP => ".*--replace-input may not be used with --empty.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

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