#! /bin/sh
#
# psconertij [Paper_inf] [rc_file]
# Copyright (C) 2003-2023 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
PRINTER=$3
IMAGINGAREA_FILE=$4

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


# 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/^.*[ ]//'`

#
# get paper imagingarea
#
LINE=`eval sed -n '/^"$PAPER_TYPE"/p' $IMAGINGAREA_FILE`
LINE=`echo $LINE | sed -e 's/^.*:[ ]//' -e 's/ /;/g'`

LEFT=`echo $LINE | awk -F';' '{print $1}'`
TOP=`echo $LINE | awk -F';' '{print $2}'`
RIGHT=`echo $LINE | awk -F';' '{print $3}'`
BOTTOM=`echo $LINE | awk -F';' '{print $4}'`


#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" = "750" ] ; 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`

# get ps bandingbox
INPUT_TEMP=`mktemp /tmp/br_input.XXXXXX`
INPUT_PS=`mktemp /tmp/br_input.XXXXXX`
TEMP_PS=`mktemp /tmp/br_input.XXXXXX`

touch $INPUT_PS

echo "<</ImagingBBox [$LEFT $TOP $RIGHT $BOTTOM] /PageOffset [-$LEFT $TOP]>> setpagedevice" > $INPUT_PS
cat >> $INPUT_PS

cp $INPUT_PS $TEMP_PS

OUTPUT_TYPE=ppmraw
OUT="-"

GHOST_OPT="-q -dNOPROMPT -dNOPAUSE -dSAFER -sDEVICE=$OUTPUT_TYPE -sstdout=%stderr -sOutputFile=$OUT - -c quit"
eval cat $TEMP_PS | $GHOST_SCRIPT -r$RESOLUTION -g${WIDTH}x${HEIGHT} $GHOST_OPT

rm -f $INPUT_TEMP
rm -f $INPUT_PS
rm -f $TEMP_PS



