Current File : //bin/psbook
#!/usr/bin/perl
# -*- perl -*-
my $version_banner = <<END;
psbook 2.07
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

# The functions in this file provide support for relocatability of
# Perl scripts.  They should be included near the beginning of each
# Perl script in a relocatable program, by adding @relocatable_pl@
# and causing the script to be expanded with AC_CONFIG_FILES.  A
# small amount of additional code must be added and adapted to the
# package by hand; see doc/relocatable-maint.texi (in Gnulib) for
# details.
#
# This code is based on relocatable.sh.in, and design changes (and
# bugs) should probably be cross-checked with it.
#
# Copyright (C) 2013, 2015-2022 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

use Config;
use File::Basename;
use File::Spec::Functions;
use Cwd 'realpath';

# Support for relocatability.
sub find_curr_installdir {
  # Determine curr_installdir, even taking into account symlinks.
  my $curr_executable = $0;
  my $basename = basename($0);
  if ($curr_executable eq $basename) {
    LOOP: for my $dir (split /$Config{path_sep}/, $ENV{PATH}) {
      $dir = "." unless $dir;
      for my $ext ('') {
        my $filename = catfile($dir, "$curr_executable$ext");
        if (-f $filename) {
          $curr_executable = $filename;
          last LOOP;
        }
      }
    }
  }
  # Resolve symlinks and canonicalize.
  return realpath(dirname($curr_executable));
}
sub find_prefixes {
  # Compute the original/current installation prefixes by stripping the
  # trailing directories off the original/current installation directories.
  my ($orig_installprefix, $curr_installprefix) = @_;
  my $orig_last = basename($orig_installprefix);
  my $curr_last = basename($curr_installprefix);
  if ($orig_last && $curr_last && $orig_last eq $curr_last) {
    $orig_installprefix = dirname($orig_installprefix);
    $curr_installprefix = dirname($curr_installprefix);
  }
  return $orig_installprefix, $curr_installprefix;
}
if ("no" eq "yes") {
  my $exec_prefix = "/usr";
  my $orig_installdir = "/usr/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/share/psutils");
$bindir = &relocate("/usr/bin");

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

unshift (@INC, "/usr/share/psutils") unless $ENV{PSUTILS_UNINSTALLED};
$bindir = '/builddir/build/BUILD/psutils-2.07' if $ENV{PSUTILS_UNINSTALLED};
}

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

use PSUtils;

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

sub usage {
  my ($exit_code) = @_;
  print STDERR <<END;
Usage: $program_name [OPTION...] [INFILE [OUTFILE]]
Rearrange pages in a PostScript document into signatures.

  -s, --signature=N    number of pages per signature;
                       0 = all pages in one signature [default];
                       1 = one page per signature;
                       otherwise, a multiple of 4
  -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 options explicitly
my @pstops_args = ();
GetOptions(
  "signature|s=i" => sub { $signature = $_[1];
                           usage(1) if $signature > 1 && $signature % 4 != 0; },
  "quiet|q" => sub { push @pstops_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;
Die("signature must be a multiple of 4") if $signature > 1 && $signature % 4 != 0;

my ($infile, $outfile) = setup_input_and_output(1);
usage(1) if $#ARGV != -1; # Check no more arguments were given

# Get number of pages
my $psinfo = parse_file($infile);
my $input_pages = $psinfo->{pages};

sub page_index_to_real_page {
  my ($maxpage, $signature, $page_number) = @_;
  my $real_page = $page_number - $page_number % $signature;
  my $page_on_sheet = $page_number % 4;
  my $recto_verso = int(($page_number % $signature) / 2);
  if ($page_on_sheet == 0 || $page_on_sheet == 3) {
    $real_page += $signature - 1 - $recto_verso;
  } else {
    $real_page += $recto_verso;
  }
  return $real_page + 1;
}

# Adjust for signature size
my $maxpage;
if ($signature == 0) {
  $signature = $maxpage = $input_pages + (4 - $input_pages % 4) % 4;
} else {
  $maxpage = $input_pages + ($signature - $input_pages % $signature) % $signature;
}

# Compute page list
my @page_list = ();
for (my $page = 0; $page < $maxpage; $page++) {
  my $real_page = page_index_to_real_page($maxpage, $signature, $page);
  push @page_list, $real_page <= $input_pages ? $real_page : "_";
}

# Rearrange pages
exec(catfile($bindir, "pstops"), "-R" . join(',', @page_list), @pstops_args, @ARGV) or Die("error running pstops");