#!/bin/bash
#
# /usr/lib/lpdfilter/filter/fax2ps
#
# filter used by /usr/lib/lpdfilter/bin/if
#
# Copyright 2000 SuSE GmbH, Nuernberg Germany
#
# Author: Werner Fink <feedback@suse.de>
#

#
# Activate debugging if requested.
#
if test "$DEBUG" = "medium" -o "$DEBUG" = "high" ; then
    # bash should be verbose
    set -x
    # Redirect stderr to a file
    exec 2> $(mktemp /tmp/lpdfilter.${0##*/}-$$.XXXXXX) || exit 2
fi

#
# Get our environment
#
. /usr/lib/lpdfilter/global/signals
. /usr/lib/lpdfilter/global/functions
type -p readpc   &> /dev/null || export PATH=/usr/lib/lpdfilter/bin:${PATH}
type -p xpmtoppm &> /dev/null || export PATH=${PATH}:/usr/X11R6/bin

#
# Check for our filter program
#
type -p g3cat    &> /dev/null || fault_filetype g3cat
type -p g3topbm  &> /dev/null || fault_filetype g3topbm
type -p pbmtolps &> /dev/null || fault_filetype pbmtolps

#
# We need a temporary file (will be deleted at exit,
# therefore we do not use exec to run pbmtolps)
#
tmp="$(mktemp ${TMPDIR:-/tmp}/lpdfilter-${0##*/}.XXXXXX)" || exit 2

#
# Checking needs a real file
#
cat > $tmp || exit 2
magic="$(file -b $tmp| tolower 2> /dev/null)"
exec 0< $tmp

#
# run the command on stdin
#
exec 0< <(exec -a g3cat g3cat -)
stretch=""
case "$magic" in
    *group*3*fax*,*normal*resolution*) stretch="-s" ;;
esac
exec 0< <(exec -a g3topbm g3topbm $stretch)
pbmtolps -dpi 204x196
