#!/bin/sh
#
# psconertij [Paper_inf] [rc_file]
# Copyright (C) 2003 Brother. Industries, Ltd.

# 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., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA
#

PAPER_INF=$1
RC_FILE=$2

# get Paper Type
PAPER_TYPE=`sed -n '/PaperType/p' $RC_FILE`
PAPER_TYPE=`echo $PAPER_TYPE | sed -e 's/ //g' -e 's/PaperType=//'`

##########
hasBorder=`echo $PAPER_TYPE | grep _B`
if [ -z "$hasBorder" ]
then
	hasBorder="true"
else
	hasBorder="false"
fi
###############

# get Resolution
RESOLUTION=`sed -n '/\<Resolution/p' $RC_FILE`
RESOLUTION=`echo $RESOLUTION | sed -e 's/ //g' -e 's/Resolution=//'`

# get paper size
LINE=`eval sed -n '/^"$PAPER_TYPE"/p' $PAPER_INF`
WIDTH=$LINE
HEIGHT=$LINE


WIDTH=`echo $WIDTH | sed -e 's/^.*:[ ]//' -e 's/[ ].*//'`
HEIGHT=`echo $HEIGHT | sed -e 's/^.*[ ]//'`
#############
Width_72dpi=`expr $WIDTH \* 72 / 600 + 14`
Height_72dpi=`expr $HEIGHT \* 72 / 600 + 14`
################
if [ "$RESOLUTION" = "300" ] ; then
#Adjust the paper size according to the Fast Normal printing , 300DPI
  WIDTH=`expr $WIDTH / 2`
  HEIGHT=`expr $HEIGHT / 2`
elif [ "$RESOLUTION" = "150" ] ; then
#Adjust both the Real Resolution and paper size for the Enhanced Normal Printing , 750DPI
#  WIDTH=`expr $WIDTH \* 5`
  WIDTH=`expr $WIDTH / 4`
#  HEIGHT=`expr $HEIGHT \* 5`
  HEIGHT=`expr $HEIGHT / 4`
fi
GHOST_SCRIPT=`which gs`
OUTPUT_TYPE=ppmraw
OUT="-"
GHOST_OPT="-q -dNOPROMPT -dNOPAUSE -dSAFER -sDEVICE=$OUTPUT_TYPE -sOutputFile=$OUT - -c quit"
################
if [ "$hasBorder" = "true" ]
then
	TEMP=`mktemp /tmp/br_input.XXXXXX`
	echo "<</ImagingBBox [14 14 $Width_72dpi $Height_72dpi] /PageOffset [-14 14]>> setpagedevice" > $TEMP
	cat >> $TEMP
	cat $TEMP | exec $GHOST_SCRIPT -r$RESOLUTION -g${WIDTH}x${HEIGHT} $GHOST_OPT
	rm $TEMP
else
	exec $GHOST_SCRIPT -r$RESOLUTION -g${WIDTH}x${HEIGHT} $GHOST_OPT
fi

