#!/usr/bin/perl
# -*- perl -*-
my $version_banner = <<END;
psresize 2.06
Copyright (c) Reuben Thomas 2016-2021.
Released under the GPL version 3, or (at your option) any later version.
END

use warnings;
use strict;

my $bindir;
BEGIN
{
# Relocatable header

if ("no" eq "yes") {
  my $exec_prefix = "/usr/local";
  my $orig_installdir = "/usr/local/bin"; # see Makefile.am's *_SCRIPTS variables
  my ($orig_installprefix, $curr_installprefix) = find_prefixes($orig_installdir, find_curr_installdir());
  sub relocate { # the subroutine is defined whether or not the enclosing block is executed
    my ($dir) = @_;
    if ("no" eq "yes") {
      $dir =~ s%^$orig_installprefix/%$curr_installprefix/%;
      $dir =~ s,/$,,;
    }
    return $dir;
  }
}

# Relocate the directory variables that we use.
my $pkgdatadir = &relocate("/usr/local/share/psutils");
$bindir = &relocate("/usr/local/bin");

# End of relocatable header; "real" Perl starts here.

unshift (@INC, '/usr/local/share/psutils') unless $ENV{PSUTILS_UNINSTALLED};
$bindir = '/local/pobj/psutils-2.06/psutils-2.06' if $ENV{PSUTILS_UNINSTALLED};
}

use File::Spec::Functions 'catfile';
use File::Basename;
use Getopt::Long;

our $program_name = basename($0);
my ($help_flag, $version_flag);

sub usage {
  my ($exit_code) = @_;
  print STDERR <<END;
Usage: $program_name [OPTION...] [INFILE [OUTFILE]]
Change the page size of a PostScript document.

  -p, --paper=PAPER    output paper size
  -P, --inpaper=PAPER  input paper size
  -q, --quiet          don't show page numbers being output
      --help           display this help and exit
      --version        display version information and exit
END
  exit $exit_code;
}

# Get arguments
Getopt::Long::Configure("bundling");
# Having configured bundling, must give short option names explicitly
my @psnup_args = ();
GetOptions(
  "paper|p=s" => sub { push @psnup_args, "-p", $_[1]; },
  "inpaper|P=s" => sub { push @psnup_args, "-P", $_[1]; },
  "width|w=s" => sub { push @psnup_args, "-w", $_[1]; },
  "height|h=s" => sub { push @psnup_args, "-h", $_[1]; },
  "inwidth|W=s" => sub { push @psnup_args, "-W", $_[1]; },
  "inheight|H=s" => sub { push @psnup_args, "-H", $_[1]; },
  "quiet|q" => sub { push @psnup_args, "-q"; },
  "help" => \$help_flag,
  "version" => \$version_flag,
 ) or usage(1);
if ($version_flag) {
  print STDERR $version_banner;
  exit 0;
}
usage(0) if $help_flag;

# Resize pages
exec(catfile($bindir, "psnup"), "-1", @psnup_args, @ARGV) or Die("error running pstops");
