3b8dbcc
#!/bin/bash
3b8dbcc
## Copyright (C) 2003-2006 Red Hat, Inc.
3b8dbcc
## Copyright (C) 2003-2006 Tim Waugh <twaugh@redhat.com>
5f9fdee
## Changed on 2007/05/17, Opher Shachar, LADPC Ltd.
5f9fdee
##     Added support for page-ranges option.
5f9fdee
##     Added page accounting.
3b8dbcc
3b8dbcc
## This program is free software; you can redistribute it and/or modify
3b8dbcc
## it under the terms of the GNU General Public License as published by
3b8dbcc
## the Free Software Foundation; either version 2 of the License, or
3b8dbcc
## (at your option) any later version.
3b8dbcc
3b8dbcc
## This program is distributed in the hope that it will be useful,
3b8dbcc
## but WITHOUT ANY WARRANTY; without even the implied warranty of
3b8dbcc
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3b8dbcc
## GNU General Public License for more details.
3b8dbcc
3b8dbcc
## You should have received a copy of the GNU General Public License
3b8dbcc
## along with this program; if not, write to the Free Software
3b8dbcc
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
3b8dbcc
3b8dbcc
if [ $# == 0 ]; then
3b8dbcc
  echo >&2 "ERROR: $0 job-id user title copies options [file]"
3b8dbcc
  exit 1
3b8dbcc
fi
3b8dbcc
3b8dbcc
# Extract the papersize
3b8dbcc
SENDFF=`grep '^\*DefaultSendFF' "$PPD" | cut -d\  -f2`
3b8dbcc
COPIES=1
3b8dbcc
if [ $# -ge 4 ]; then
3b8dbcc
  COPIES="$4"
3b8dbcc
fi
3b8dbcc
7870e7a
if [ $# -lt 6 ]; then
3b8dbcc
  unset TMPFILE
3b8dbcc
  trap -- 'rm -f "$TMPFILE"' EXIT
3b8dbcc
  TMPFILE=$(mktemp ${TMPDIR:-/tmp}/textonly.XXXXXX)
3b8dbcc
  cat > "$TMPFILE"
3b8dbcc
else
3b8dbcc
  TMPFILE="$6"
3b8dbcc
fi
3b8dbcc
5f9fdee
PR=${5#*page-ranges=}
5f9fdee
# Do options specify page-ranges?
5f9fdee
if [[ "$PR" != "$5" ]]; then
5f9fdee
  PR=${PR%% *}
5f9fdee
else
5f9fdee
  #unset PR
5f9fdee
  PR=1-999999
5f9fdee
fi
5f9fdee
5f9fdee
if [[ "$PR" ]]; then
5f9fdee
  TMPFILE2=$(mktemp ${TMPDIR:-/tmp}/textonly2.XXXXXX)
5f9fdee
  pagenum=0
5f9fdee
  EOF=
5f9fdee
  { 
5f9fdee
  while [[ "$PR" ]]; do
5f9fdee
    pl=${PR%%,*}		;# take first subrange
5f9fdee
    PR=${PR#$pl};PR=${PR#,}	;# remove from range list
5f9fdee
    pu=${pl#*-}			;# extract upper and lower
5f9fdee
    pl=${pl%-*}			;# pages of subrange
5f9fdee
    # Allows interpreting 0-5,3-10 as 1-5,6-10 rejects 5-1 or 1-
5f9fdee
    (( pagenum >= pl )) && pl=$(( pagenum + 1 ))
5f9fdee
    (( pl > pu )) && continue
5f9fdee
    
5f9fdee
    # Loop reading pages until at or over lower page of subrange.
5f9fdee
    while read -d `echo -ne '\f'` -r; do
5f9fdee
      (( pagenum++ ))
5f9fdee
      (( pagenum == pl )) && break
5f9fdee
    done
5f9fdee
    # Did we reach lower page of subrange or EOF?
5f9fdee
    if (( pagenum < pl )); then
5f9fdee
      [[ ! "$REPLY" ]] && break		;# empty last page - we're done.
5f9fdee
      (( pagenum++ ))
5f9fdee
      EOF=y
5f9fdee
    fi
5f9fdee
    # Output page and report to page log
5f9fdee
    if (( pagenum == pl )); then
5f9fdee
      echo -n "${REPLY}" >>"$TMPFILE2"
5f9fdee
      # If EOF then page has no final FF
5f9fdee
      [[ ! "$EOF" ]] && echo -ne '\f' >>"$TMPFILE2"
5f9fdee
      echo "PAGE: $pagenum $COPIES" >&2
5f9fdee
    fi
5f9fdee
    [[ "$EOF" ]] && break
5f9fdee
    # Is the current subrange a single page?
5f9fdee
    (( pagenum == pu )) && continue
5f9fdee
    while read -d `echo -ne '\f'` -r; do
5f9fdee
      (( pagenum++ ))
5f9fdee
      echo -ne "${REPLY}\f" >>"$TMPFILE2"
5f9fdee
      echo "PAGE: $pagenum $COPIES" >&2
5f9fdee
      (( pagenum == pu )) && break
5f9fdee
    done
5f9fdee
    # Could be that we reached EOF before page boundry
5f9fdee
    if (( pagenum < pu )); then
5f9fdee
      if [[ "$REPLY" ]]; then
5f9fdee
        (( pagenum++ ))
5f9fdee
        echo -n "${REPLY}" >>"$TMPFILE2"
5f9fdee
        echo "PAGE: $pagenum $COPIES" >&2
5f9fdee
      fi
5f9fdee
      break
5f9fdee
    fi
5f9fdee
  done
5f9fdee
  } <"$TMPFILE"
5f9fdee
else
5f9fdee
  TMPFILE2="$TMPFILE"
5f9fdee
  pc=$(grep -co `echo -ne '\f'` "$TMPFILE2")
5f9fdee
  pc=$(( pc * $COPIES ))
5f9fdee
  echo "PAGE: $pc" >&2
5f9fdee
fi
5f9fdee
3b8dbcc
while [ "$COPIES" -gt 0 ]; do
3b8dbcc
  # Just translate LF->CRLF at the moment, until the PPD has options added.
5f9fdee
  sed -e 's/$/'`echo -ne '\r'`'/g' "$TMPFILE2"
3b8dbcc
3b8dbcc
  if [ "$SENDFF" == "True" ]
3b8dbcc
    then
6eeadd4
    echo -ne '\f'
3b8dbcc
  fi
3b8dbcc
3b8dbcc
  COPIES=$(($COPIES - 1))
3b8dbcc
done
5f9fdee
# Cleanup
5f9fdee
[[ "$TMPFILE" != "$TMPFILE2" ]] && rm -f "$TMPFILE2"
5f9fdee
exit 0