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