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