#!/bin/bash
#
# /usr/lib/lpdfilter/bin/guess
#
# helper guess of /usr/lib/lpdfilter/bin/if
#
# Copyright 2000 SuSE GmbH, Nuernberg Germany
#
# Author: Werner Fink <feedback@suse.de>
#

#
# Activate debugging if requested.
#
if test -n "$DEBUG" -a "$DEBUG" = "medium" -o "$DEBUG" = "high" ; then
    # bash should be verbose
    set -x
    # Redirect stderr to a file
    exec 2> $(mktemp /tmp/lpdfilter.guess-$$.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

#
# Default filter, type, and unpacker
#
 unpack=""
   type=""
creator=""
    cat="cat"
options=""

#
# The only option `-r'
# We escape then after testing fo a unpacker.
#
if test -n "$1" -a "$1" = "-r" ; then
    shift
    type=raw
fi

#
# Input directly given not standard in.
#
test -n "$1" -a -r "$1" && exec 0< "$1"

#
# First try
#
magic="$(file -b - | tolower 2> /dev/null)"
test $? -ne 0 && fatal_error "Error: can not guess."
rewindstdin

case "$magic" in
    *gzip*)	unpack='gzip -dc' ;;
    *bzip2*)	unpack='bzip2 -d' ;;
    *compress*)	unpack='zcat'	  ;;
    *frozen*)	unpack='fcat'	  ;;
    *packed*)	unpack='gzip -dc' ;;
    *)		unpack=''	  ;;
esac
if test -n "$unpack" ; then
     type -p ${unpack%% *} &> /dev/null || fault_unpacker
fi
if test -n "$type" ; then
    echo unpack=\"$unpack\"
    echo type=\"$type\"
    echo cat=\"$cat\"
    exit 0
fi

#
# Second try: have look into the packed data.
#
if test -n "$unpack" ; then
    magic="$($unpack 2> /dev/null | file -b - | tolower)"
    test $? -ne 0 && fatal_error "Error: can not guess."
    rewindstdin
fi

#
# Foreign PostScript data may contains some ASCII EOT
# (socalled job separators) at the beginning.
#
case "$magic" in
    postscript*)
	if test -n "$unpack" ; then
	    first=$($unpack 2> /dev/null | readn 2)
	else
	    first=$(readn 2)
	fi
        rewindstdin
	case "$first" in
	    %\!)		;;
	    *)  cat=lstrip	;;
	esac
esac

#
# Sometimes there are empty lines or job separators at the
# beginning. Skipt them an have a look onto the real data.
#
case "$magic" in
    postscript*)		;; # Ignore real PostScript text
    *english*|*text*)
	if test -n "$unpack" ; then
	    magic="$($unpack 2> /dev/null | lstrip | file -b - | tolower)"
	else
	    magic="$(lstrip | file -b - | tolower)"
	fi
        rewindstdin
	case "$magic" in
	    *english*|*text*)	;;
	    *)  cat=lstrip	;;
	esac
esac

#
# What data are embbeded within this PJL stream?
#
case "$magic" in
    hp*printer*job*language*data|*pjl*)
	if test -n "$unpack" ; then
	    magic="$($unpack 2> /dev/null | unpjl  | file -b - | tolower)"
	else
	    magic="$(unpjl | file -b - | tolower)"
	fi
        rewindstdin
	cat=unpjl
esac

#
# What data are embbeded within this PS stream?
#
case "$magic" in
    postscript*)
	if test -n "$unpack" ; then
	    creator="$($unpack 2> /dev/null | $cat | readn 1024 | grep -aE '^%%[ ]*Creator:' 2>/dev/null | tolower)"
	else
	    creator="$($cat | readn 1024 | grep -aE '^%%[ ]*Creator:' 2>/dev/null | tolower)"
	fi
	rewindstdin
esac

#
# Handle the found type. 
#
while read type tag ; do
    case "$type" in
	\#*|"") continue ;;
    esac
    case "$magic" in
	$tag) break ;;
    esac
    type=
done < /etc/lpdfilter/types
test -n "$type" || fault_method

echo unpack=\"$unpack\"
echo type=\"$type\"
echo creator=\"$creator\"
echo options=\"$options\"
echo cat=\"$cat\"
exit 0
