#! /bin/sh
#
# Photo Image Print System
# Copyright (C) 2001-2004 EPSON KOWA Corporation.
# Copyright (C) SEIKO EPSON CORPORATION 2001-2004.
#
#  This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

model_full="Stylus C80 Series"
model=$(echo $model_full \
    | sed 's/ //g' \
    | sed 's/[[:lower:]_\-]//g' \
    | tr [:upper:] [:lower:] | tr / _)
model_l=$(echo $model | tr [:lower:] [:upper:])

PRINTER="$model_full"
PIPS=pips-sc80s
EKPSTM=/usr/local/bin/ekpstm
E_PRT_PATH=/usr/local/libexec/pips/printer
PAPER_LIST=$E_PRT_PATH/paper_list.csv
GSCONF=$E_PRT_PATH/gsconfig
FRESET=$E_PRT_PATH/freset
DTRFILTER=$E_PRT_PATH/dtrfilter

PATH=$PATH:/usr/bin:/usr/local/bin
RCFILE=/etc/pipsrc

touch /var/ekpd/lpr_lock

# locale set
LANGPATH=/usr/local/libexec/pips/$model_l/lang
if [ -s $LANGPATH ]; then
    . $LANGPATH
fi

while [ -n "$1" ]; do
    TMPSTR="`echo x$1 | sed -e 's/^x//;s/^-h.*$//'`";
    if [ -z "$TMPSTR" ]; then
	break;
    fi
  shift
done

USER_HOST=
if [ -n "$1" ]; then
    USER_HOST=`echo $1 | sed -e "s/^.*-h//"`
    if [ \( -z "$USER_HOST" \) -a \( -n "$2" \) ]; then
	USER_HOST=$2
    fi
fi

if [ -z $USER_HOST ]; then
    USER_HOST="localhost"
fi

DISPLAY=$USER_HOST:0.0

PSOUT=`ps ewwwwwwwwww | grep ekpstm | grep -v grep | grep "DISPLAY=$DISPLAY"`

if [ -z "$PSOUT" ]; then
    export DISPLAY
    $EKPSTM &
    STMPID=$!
fi


# filter
number=`grep -n "$PRINTER" $RCFILE | sed -e "s@:.*@@"`

if [ x$number = x ] ; then
    echo "$0 : $RCFILE : does not include entry of \"$model_full\"." >&2

else
    i=0

    exec 3< $RCFILE
    while read line <&3 ; do
	if [ "$i" -le "$number" ] ; then
	    i=`expr $i + 1`
	    continue;
	fi
	if [ x"`echo \"$line\" | grep \"^.*\[.*\].*$\"`" != x ] ; then
	    break
	fi

	resource=`echo "$line" | sed -e 's@=.*$@@' -e 's@[[:space:]]@@g' | tr A-Z a-z`
	value=`echo "$line" | sed -e 's@[[:space:]]@@g' -e 's@^.*=@@' -e 's@#.*$@@'`

	case "$resource" in
	    "colorcorrection") KEY_CC=$value;;
	    "ink") KEY_INK=$value;;
	    "mediasize") KEY_MEDIA=$value ;;
	    "resolution") KEY_RESOL=$value ;;
	    "fit2page") KEY_FITP=$value ;;
	    "multipage") KEY_NPAGE=$value ;;
	    "papersource") KEY_SOURCE=$value ;;
	    \?) ;;
	esac
    done
fi

# default set
KEY_CC=${KEY_CC:=PHOTO}
KEY_INK=${KEY_INK:=COLOR}
KEY_MEDIA=${KEY_MEDIA:=A4}
KEY_RESOL=${KEY_RESOL:=360}
KEY_FITP=${KEY_FITP:=OFF}
KEY_NPAGE=${KEY_NPAGE:=1}
KEY_SOURCE=${KEY_SOURCE:=AUTO}

#Check file type
FILE_TYPE=`file - | sed -e 's@^.*:[[:space:]]*@@' -e 's@[[:space:]].*$@@'`
$FRESET

# set gsconfig & pips options
PIPS_OPTS="-ui C"

if [ "$FILE_TYPE" = "PostScript" ] ; then
    GSCONF_OPTS="-P\"$PRINTER\" -p\"$PAPER_LIST\" -r\"$FRESET\" -P\"$PRINTER\" -p\"$PAPER_LIST\" -r\"$FRESET\" -l\"$KEY_NPAGE\" -m\"$KEY_MEDIA\" -s\"$KEY_SOURCE\""

    if [ "$KEY_FITP" = "ON" ] ; then
	GSCONF_OPTS="$GSCONF_OPTS -f"
	PIPS_OPTS="$PIPS_OPTS -ft OFF -mg 0 0"
    fi
fi

if [ "$KEY_CC" = "AUTO" -a "$KEY_INK" = "COLOR" ] ; then
    TMPFILE=`mktemp /tmp/pips.XXXXXX`
    cat > $TMPFILE
    exec <&-

    ATBMP_FIFO=`mktemp /tmp/atbmp_pipe.XXXXXX`
    rm $ATBMP_FIFO
    mkfifo $ATBMP_FIFO

    trap '' 2
    case "$FILE_TYPE" in
	"PostScript")
	    eval $GSCONF -R120 $GSCONF_OPTS < $TMPFILE | $DTRFILTER > $ATBMP_FIFO &
	    eval $GSCONF -R"$KEY_RESOL" $GSCONF_OPTS < $TMPFILE | $PIPS $PIPS_OPTS -at $ATBMP_FIFO
	    PRTSTATUS=$?
	    ;;

	"PNG")
	    eval $DTRFILTER < $TMPFILE > $ATBMP_FIFO &
	    eval $PIPS $PIPS_OPTS -at $ATBMP_FIFO < $TMPFILE
	    PRTSTATUS=$?
	    ;;

	* )
	    cat $TMPFILE
	    ;;
    esac
    trap 2

    rm $TMPFILE $ATBMP_FIFO

else
    trap '' 2
    case "$FILE_TYPE" in
	"PostScript")
	    eval $GSCONF $GSCONF_OPTS | $PIPS $PIPS_OPTS
	    PRTSTATUS=$?
	    ;;

	"PNG")
	    eval $PIPS $PIPS_OPTS
	    PRTSTATUS=$?
	    ;;

	* )
	    cat
	    ;;
    esac
    trap 2
fi


if [ -n "$STMPID" ]; then
    kill -s 9 $STMPID
fi

rm -f /var/ekpd/lpr_lock

exit $PRTSTATUS
