#!/bin/sh

### lprx.sh --- Shell script wrapper to imitate lpr's print job duplicating
###             behavior (usually -# or -n option).

## Copyright (C) 2005  Aaron S. Hawley

## Author: Aaron S. Hawley <ashawley@gnu.uvm.edu>
## Keywords: convenience, printing

## This file 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 2, or (at your option)
## any later version.

## This file 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 GNU Emacs; see the file COPYING.  If not, write to
## the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
## Boston, MA 02111-1307, USA.

### Commentary:

usage="\
Usage: lprx NUMBER OPTIONS-FOR-LPR
Runs the \`lpr' command NUMBER times (when you're sure you want to).
Arguments after NUMBER are passed to \`lpr'";

## Requires Bourne-like shell (like Bash), `sed', `bc', and of course
## `lpr'.

### Code:

# Remove non-numbers in $1
n=`echo "$1" | sed -e 's/[^0-9]//'`

# If the argument has changed or is <= 0, then exit with error and
# usage information.
if [ "$n" != "$1" ] || [ $n -le 0 ]; then
  echo "invalid argument for lprx: $1"
  echo "$usage"
  exit 1
fi

# drop $1 from $@
shift

# Rather than using a for-loop, we opt for recursion.
# If $n > 1 print once and recur, else print once.
if [ $n -gt 1 ]; then
  lpr "$@" # Print!
  # $n--
  n=`echo $n - 1 | bc`
  lprx $n "$@"
else
  lpr "$@"
fi

## END ##

