#!/bin/sh

# lp2 --- print a document duplex on a simplex printer

# Copyright 1999-2023 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, 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
# 02110-1301, USA.

# Author: Reuben Thomas <rrt@sc3d.org>
# Shell version based on fixps by Akim Demaille

set -e

# Get the name of the program
program=`echo $0 | sed 's#.*/##g'`

# Local vars
back=:		# Print the back side pages.
front=:		# Print the front side pages.

# The version/usage strings
version="lp2 1.0 (GNU a2ps 4.15.6)
Written by Reuben Thomas.

Copyright (c) 2023 Reuben Thomas
Based on fixps, copyright (c) 1998-2000 Akim Demaille
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."

usage="\
Usage: $program [OPTIONS] FILE...
Print a file duplex on a simplex printer.n First print the odd pages, then
prompt the user to put the printed pages back in, then print the even pages
in reverse order.


Options:
 -h, --help              display this help and exit
 -V, --version           display version information and exit
 -f, --front             print only the front pages (recto)
 -b, --back              print only the back pages (verso)

Report bugs to <bug-a2ps@gnu.org>"

help="Try \`$program --help' for more information."

# Parse command line arguments.
option_without_arguments='vhqfb'

# Push a token among the arguments that will be used to notice when
# we ended options/arguments parsing.
arg_sep="$$--$$"
set dummy ${1+"$@"} "$arg_sep"
shift
while test "x$1" != "x$arg_sep"; do

  # Handle --option=value by splitting apart and putting back on argv.
  case "$1" in
    --*=*)
      opt=`echo "$1" | sed -e 's/=.*//'`
      val=`echo "$1" | sed -e 's/[^=]*=//'`
      shift
      set dummy "$opt" "$val" ${1+"$@"}
      shift
      ;;

    -[$option_without_arguments]?*)
      # Prefix $1 with x to avoid running `echo -na' for instance.
      opt=`echo "x$1" | sed -e 's/x-\(.\).*/-\1/'`
      rest=`echo "x$1" | sed -e 's/x-.\(.*\)/-\1/'`
      shift
      set dummy "$opt" "$rest" ${1+"$@"}
      shift
      ;;

    # This case needs to be protected so that the case `-??*' does
    # not split long options without arguments
    --*)
      ;;

    # This is an option with argument.  Split apart and put back on argv.
    -??*)
      opt=`echo "x$1" | sed -e 's/x-\(.\).*/-\1/'`
      arg=`echo "x$1" | sed -e 's/x-.\(.*\)/\1/'`
      shift
      set dummy "$opt" "$arg" ${1+"$@"}
      shift
      ;;
  esac

  # Now, handle the options.  $1 is the option *only*.  If it has an
  # argument, it is now necessarily in $2 etc.  Remember to shift
  # when fetching an argument.
  case "$1" in
    -V | --v*) echo "$version"; exit 0;;
    -h | --h*) echo "$usage"; exit 0;;
    -b | --bac*) front= ; back=: ;;
    -f | --fro*) front=: ; back= ;;

    --) # What remains are not options.
      shift
      while test "x$1" != "x$arg_sep"; do
        set dummy ${1+"$@"} "$1"
        shift
        shift
      done
      break;;

    -*)
      echo "$program: Unknown or ambiguous option \`$1'." >&2
      echo "$program: Try \`--help' for more information." >&2
      exit 1;;
    *) set dummy ${1+"$@"} "$1"
       shift
       ;;
  esac
  shift
done
# Pop the token
shift


# Check the number of arguments.
if test $# = 0; then
   echo "$program: not enough arguments" 1>&2
   echo "$help"  1>&2
   exit 1
fi

for file in "$@"; do
  if test -f "$file"; then
     if test -n "$front"; then
        lp -o page-set=odd "$file"
     fi
     if test -n "$front" && test -n "$back"; then
        echo "Turn the paper over and press <Return>" 1>&2
        read
     fi
     if test -n "$back"; then
        lp -o page-set=even -o outputorder=reverse "$file"
     fi
  else
    printf "$program: \`$file' could not be read\n" 1>&2
  fi
done
