#! /bin/sh
# -*- sh -*-
#
# $ApsCVS: src/apsfilter/bin/apspreview.in,v 1.1.2.6 2002/03/03 20:21:14 andreas Exp $
#
# apspreview  by Michael Loßin
#
# create a temporary PostScript file using apsfilter and preview it
#

error()
{
    echo >&2 "error: $1"
    rm -rf "$APS_TMPDIR"
    exit 1
}

usage()
{
    cat >&2 <<EOF
apspreview: preview a file as it would be printed by apsfilter

Usage:
	apspreview [-h] [-P queue] [-Z options] [input...]

Options:
	-h		show this help
	-P queue	name of printer queue to be used for apsfilter
			(default: value of '\$PRINTER'; otherwise 'lp')
	-Z options	comma separated option list
			(default: empty)
	input		input file(s)
			(default: stdin)
EOF
    exit 0
}


# look for a PostScript viewer
if type gv > /dev/null 2>&1; then
    viewer=gv
elif type kghostview > /dev/null 2>&1; then
    viewer=kghostview
elif type ghostview > /dev/null 2>&1; then
    viewer=ghostview
else
    error "can't find a suitable PostScript viewer"
fi


QUEUE=${PRINTER:-lp}
unset Z_OPTS INPUT_FILES


# parse commandline
while [ "$1" ]; do
    case "$1" in
	-P)		shift; QUEUE="$1" ;;
	-P*)		QUEUE="${1#-P}" ;;

	-Z)		shift; Z_OPTS="-Z$1" ;;
	-Z*)		Z_OPTS="$1" ;;

	-h|--help)	usage ;;

	-*)		error "unknown option '$1'" ;;

	*)		# silly construction to allow spaces in filenames
			INPUT_FILES="$INPUT_FILES
$1" ;;
    esac
    shift
done

# if the queue name is an alias, parse printcap for the original name
if [ ! -d "$APS_CONFDIR/$QUEUE" ]; then
    # quote forward slashes
    q=$(echo "$QUEUE" | sed 's%/%\\/%')
    q=$("/usr/bin/awk" -F":" "/^${q}[|:]|^[^#].*\|${q}[|:]/,/^#/ \
	{ if (\$0 ~ /:sd=/) print \$2 }" < "/etc/printcap")
    if [ "$q" ]; then
	QUEUE="${q##*/}"
    else
	error "couldn't resolve printer name '$QUEUE' in /etc/printcap"
    fi
fi
if [ ! -f "/etc/apsfilter/$QUEUE/apsfilterrc" ]; then
    error "no configuration file found for queue '$QUEUE'"
fi


# create temporary configuration directory
: ${TMPDIR:=/tmp}
APS_TMPDIR="$TMPDIR/apspreview$$"
rm -rf "$APS_TMPDIR"
mkdir -m 700 "$APS_TMPDIR"
if [ $? -ne 0 ]; then
    APS_TMPDIR=`mktemp -q -d "$TMPDIR/apspreview.XXXXXX"` \
	|| error "can't create temporary directory"
    chmod 700 "$APS_TMPDIR"
fi
trap 'rm -rf "$APS_TMPDIR"' 0

ln -s "/usr/local/share/apsfilter" "$APS_TMPDIR/basedir"
mkdir "$APS_TMPDIR/$QUEUE" || error "can't create temporary directory"

# use global configuration files (if available)
if [ -f "/etc/apsfilter/apsfilterrc" ]; then
    ln -s "/etc/apsfilter/apsfilterrc" "$APS_TMPDIR/apsfilterrc"
fi
if [ -f "/etc/apsfilter/restrictions" ]; then
    ln -s "/etc/apsfilter/restrictions" "$APS_TMPDIR/restrictions"
fi
if [ -f "/etc/apsfilter/$QUEUE/restrictions" ]; then
    ln -s "/etc/apsfilter/$QUEUE/restrictions" \
	"$APS_TMPDIR/$QUEUE/restrictions"
fi

# create local configuration file from the original one
{
    cat "/etc/apsfilter/$QUEUE/apsfilterrc"
    # overwrite the PRINTER setting for previewing
    echo "PRINTER=PS"
    # let's say we have a good PostScript printer :^)
    echo "HARDWARE_DUPLEX=set"
    echo "HARDWARE_COPIES=set"
} > "$APS_TMPDIR/$QUEUE/apsfilterrc"

export APS_CONFDIR="$APS_TMPDIR"


# convert file to PostScript (one after another), then preview it
: ${INPUT_FILES:=/dev/stdin}

# regain file list (potentially spaces) with strange IFS setting
IFS="
"
for file in $INPUT_FILES; do
    echo "converting '$file'..."
    /usr/local/bin/aps2file "$file" -P"$QUEUE" $Z_OPTS -o "$APS_TMPDIR/apspreview.ps"

    filetype=$(file "$APS_TMPDIR/apspreview.ps")
    case $(echo ${filetype#$APS_TMPDIR/apspreview.ps: } | tr A-Z a-z) in
	postscript*)
	    echo "previewing PostScript data with $viewer..."
	    $viewer "$APS_TMPDIR/apspreview.ps" ;;
	*)
	    echo "aps2file didn't yield PostScript, skipping '$file'..." ;;
    esac
done
