#! /bin/sh
#
# Brother Print filter
# Copyright (C) 2005-2022 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
#

printer_model="td2135n"
printer_name=`echo $printer_model | tr '[a-z]' '[A-Z]'`
device_name=`echo $printer_name | eval sed -e 's/PJ/PJ-/'`
pcfilename=`echo $printer_name | tr -d '[A-Z]'`
device_model="PTouch"
tmp_filter=/var/tmp/brother_lpdwrapper_${printer_model}

if [ -d "/usr/share/cups/model" ]; then
    mkdir -p /usr/share/cups/model/Brother
else
    mkdir -p /usr/share/ppd/Brother
fi

if [ -d "/usr/share/ppd" ]; then
    mkdir -p /usr/share/ppd/Brother
else
    mkdir -p /usr/share/cups/model/Brother
fi


# -e start
if [ "$1" = '-e' ]; then
if [ -f /etc/debian_version ] ; then
    sudo rm -rf /var/lib/dpkg/info/${printer_model}cupswrapper*
fi
    lpadmin -x ${device_name}
    rm -f /usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
    rm -f /usr/share/ppd/Brother/brother_${printer_model}_printer_en.ppd
    rm -f /usr/lib/cups/filter/brother_lpdwrapper_${printer_model}
    rm -f /usr/lib64/cups/filter/brother_lpdwrapper_${printer_model}
    rm -f /opt/brother/${device_model}/${printer_model}/cupswrapper/brcupsconfpt1
if [  -e /etc/init.d/cups ]; then
    /etc/init.d/cups restart
elif [  -e /etc/init.d/cupsys ]; then
    /etc/init.d/cupsys restart
fi
    exit 0
fi
if [ "$1" = "-r" ]; then
    lpadmin -x ${device_name}
if [  -e /etc/init.d/cups ]; then
    /etc/init.d/cups restart
elif [  -e /etc/init.d/cupsys ]; then
    /etc/init.d/cupsys restart
fi
  exit 0
fi
if [ "$1" = "help" ] || [ "$1" = "-h" ]; then
  echo   'option -h : help'
  echo   '       -i : install'
  echo   '       -e : uninstall'
  echo   '       -r : remove printer'
  exit 0
fi


if [ -e "/opt/brother/${device_model}/${printer_model}/lpd/filter${printer_model}" ]; then
  :
else
  echo "ERROR : Brother LPD filter is not installed."
fi
if [ -f /etc/debian_version ] ; then 
    if [ -d /usr/share/cups/model ]; then
        rm -f /usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
        ppd_file_name=/usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
        ppd_file_name1=/usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
    fi
    if [ -d /usr/share/ppd ]; then
        rm -f /usr/share/ppd/Brother/brother_${printer_model}_printer_en.ppd
        ppd_file_name=/usr/share/ppd/Brother/brother_${printer_model}_printer_en.ppd
        ppd_file_name1=/usr/share/ppd/Brother/brother_${printer_model}_printer_en.ppd
    fi
else 
    rm -f /usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
    ppd_file_name=/usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
    ppd_file_name1=/usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
fi
cat <<ENDOFPPDFILE | tee $ppd_file_name >> $ppd_file_name1
*PPD-Adobe: "4.3"

*%================================================
*%	Copyright(C) 2005-2022 Brother Industries, Ltd.
*%	"Brother TD-2135N CUPS"
*%================================================

*%==== General Information Keywords ========================
*FormatVersion: "4.3"
*FileVersion: "1.00"
*LanguageVersion: English
*LanguageEncoding: ISOLatin1
*PCFileName: "TD-2135N.PPD"
*Manufacturer: "Brother"
*Product: "(TD-2135N)"
*1284DeviceID: "MFG: Brother;MDL: TD-2135N"
*cupsVersion: 1.4
*cupsManualCopies: False
*cupsFilter: "application/vnd.cups-postscript 0 brother_lpdwrapper_td2135n"
*cupsModelNumber: 1
*ModelName: "Brother TD-2135N"
*ShortNickName: "Brother TD-2135N"
*NickName: "Brother TD-2135N CUPS"
*%APDialogExtension: "/Library/Printers/Brother/PDEs/BRPtouchQuality.plugin"
*%APPrinterIconPath: "/Library/Printers/Brother/Icons/BRPTQL.icns"
*PSVersion: "(3010.106) 3"

*%==== Basic Device Capabilities =============
*LanguageLevel: "3"
*ColorDevice: False
*DefaultColorSpace: Gray
*FileSystem: False
*Throughput: "12"
*LandscapeOrientation: Plus90
*VariablePaperSize: False
*TTRasterizer: Type42
*FreeVM: "1700000"

*%DefaultOutputOrder: Reverse

*%==== Media Selection ======================
*OpenGroup: Basic

*OpenUI *PageSize/Media Size: PickOne
*OrderDependency: 11 AnySetup *PageSize
*DefaultPageSize: 50x30
*PageSize 30x30/30mm x 30mm(1.1" x 1.1"):	"          "
*PageSize 40x40/40mm x 40mm(1.5" x 1.5"):	"          "
*PageSize 40x50/40mm x 50mm(1.5" x 1.9"):	"          "
*PageSize 40x60/40mm x 60mm(1.5" x 2.3"):	"          "
*PageSize 50x30/50mm x 30mm(1.9" x 1.1"):	"          "
*PageSize 51x26/51mm x 26mm(2.0" x 1.0"):	"          "
*PageSize 60x60/60mm x 60mm(2.3" x 2.3"):	"          "
*PageSize 57X1/57mm(2.25"):			"          "
*PageSize 58X1/58mm(2.28"):			"          "
*CloseUI: *PageSize

*OpenUI *PageRegion: PickOne
*OrderDependency: 12 AnySetup *PageRegion
*DefaultPageRegion: 50x30
*PageRegion 30x30/30mm x 30mm(1.1" x 1.1"):	"          "
*PageRegion 40x40/40mm x 40mm(1.5" x 1.5"):	"          "
*PageRegion 40x50/40mm x 50mm(1.5" x 1.9"):	"          "
*PageRegion 40x60/40mm x 60mm(1.5" x 2.3"):	"          "
*PageRegion 50x30/50mm x 30mm(1.9" x 1.1"):	"          "
*PageRegion 51x26/51mm x 26mm(2.0" x 1.0"):	"          "
*PageRegion 60x60/60mm x 60mm(2.3" x 2.3"):	"          "
*PageRegion 57X1/57mm(2.25"):			"          "
*PageRegion 58X1/58mm(2.28"):			"          "
*CloseUI: *PageRegion


*DefaultImageableArea: 50x30
*ImageableArea 30x30/30mm x 30mm(1.1" x 1.1"):	"4.32 8.40 80.64 76.56"
*ImageableArea 40x40/40mm x 40mm(1.5" x 1.5"):	"4.32 8.40 108.96 104.88"
*ImageableArea 40x50/40mm x 50mm(1.5" x 1.9"):	"4.32 8.40 108.96 133.44"
*ImageableArea 40x60/40mm x 60mm(1.5" x 2.3"):	"4.32 8.40 108.96 161.76"
*ImageableArea 50x30/50mm x 30mm(1.9" x 1.1"):	"4.32 8.40 137.52 76.56"
*ImageableArea 51x26/51mm x 26mm(2.0" x 1.0"):	"4.32 8.40 139.68 64.08"
*ImageableArea 60x60/60mm x 60mm(2.3" x 2.3"):	"5.76 8.40 164.40 161.76"
*ImageableArea 57X1/57mm(2.25"):			"4.32 8.40 157.68 275.04"
*ImageableArea 58X1/58mm(2.28"):			"4.32 8.40 160.08 275.04"

*%==== Information About Media Sizes ========
*DefaultPaperDimension: 50x30
*PaperDimension 30x30/30mm x 30mm(1.1" x 1.1"):	"84.96 84.96"
*PaperDimension 40x40/40mm x 40mm(1.5" x 1.5"):	"113.28 113.28"
*PaperDimension 40x50/40mm x 50mm(1.5" x 1.9"):	"113.28 141.84"
*PaperDimension 40x60/40mm x 60mm(1.5" x 2.3"):	"113.28 170.16"
*PaperDimension 50x30/50mm x 30mm(1.9" x 1.1"):	"141.84 84.96"
*PaperDimension 51x26/51mm x 26mm(2.0" x 1.0"):	"144.00 72.48"
*PaperDimension 60x60/60mm x 60mm(2.3" x 2.3"):	"170.16 170.16"
*PaperDimension 57X1/57mm(2.25"):			"162.00 283.44"
*PaperDimension 58X1/58mm(2.28"):			"164.40 283.44"


*OpenUI *BrMargin/Feed(invalid for Die-Cut tape): PickOne
*OrderDependency: 23 AnySetup  *BrMargin
*DefaultBrMargin: 3
*BrMargin 3/3 mm: "          "
*BrMargin 4/4 mm: "          "
*BrMargin 5/5 mm: "          "
*BrMargin 6/6 mm: "          "
*BrMargin 7/7 mm: "          "
*BrMargin 8/8 mm: "          "
*BrMargin 9/9 mm: "          "
*BrMargin 10/10 mm: "          "
*BrMargin 11/11 mm: "          "
*BrMargin 12/12 mm: "          "
*BrMargin 13/13 mm: "          "
*BrMargin 14/14 mm: "          "
*BrMargin 15/15 mm: "          "
*BrMargin 16/16 mm: "          "
*BrMargin 17/17 mm: "          "
*BrMargin 18/18 mm: "          "
*BrMargin 19/19 mm: "          "
*BrMargin 20/20 mm: "          "
*BrMargin 21/21 mm: "          "
*BrMargin 22/22 mm: "          "
*BrMargin 23/23 mm: "          "
*BrMargin 24/24 mm: "          "
*BrMargin 25/25 mm: "          "
*BrMargin 26/26 mm: "          "
*BrMargin 27/27 mm: "          "
*BrMargin 28/28 mm: "          "
*BrMargin 29/29 mm: "          "
*BrMargin 30/30 mm: "          "
*CloseUI: *BrMargin

*OpenUI *BrPriority/Quality: PickOne
*OrderDependency: 22 AnySetup  *BrPriority
*DefaultBrPriority: BrSpeed
*BrPriority BrSpeed/Give priority to print speed 300*300dpi: "          "
*BrPriority BrQuality/Give priority to print quality 300*300dpi: "          "
*CloseUI: *BrPriority

*% added start printing/Compress for QL720NW
*OpenUI *BrCompress/Start printing: PickOne
*OrderDependency: 23 AnySetup  *BrCompress
*DefaultBrCompress: OFF
*BrCompress OFF/Immediately after starting to receive data: "          "
*BrCompress ON/After one page of data is received: "          "
*CloseUI: *BrCompress

*CloseGroup: Basic


*OpenGroup: Option
*% added for QL720NW
*%OpenUI *BrCutLabel/Cut every: PickOne
*%OrderDependency: 23 AnySetup  *BrCutLabel
*%DefaultBrCutLabel: 1
*%BrCutLabel 0/OFF: "          "
*%BrCutLabel 1/1 label: "          "
*%BrCutLabel 2/2 labels: "          "
*%BrCutLabel 3/3 labels: "          "
*%BrCutLabel 4/4 labels: "          "
*%BrCutLabel 5/5 labels: "          "
*%BrCutLabel 6/6 labels: "          "
*%BrCutLabel 7/7 labels: "          "
*%BrCutLabel 8/8 labels: "          "
*%BrCutLabel 9/9 labels: "          "
*%BrCutLabel 10/10 labels: "          "
*%BrCutLabel 11/11 labels: "          "
*%BrCutLabel 12/12 labels: "          "
*%BrCutLabel 13/13 labels: "          "
*%BrCutLabel 14/14 labels: "          "
*%BrCutLabel 15/15 labels: "          "
*%BrCutLabel 16/16 labels: "          "
*%BrCutLabel 17/17 labels: "          "
*%BrCutLabel 18/18 labels: "          "
*%BrCutLabel 19/19 labels: "          "
*%BrCutLabel 20/20 labels: "          "
*%BrCutLabel 21/21 labels: "          "
*%BrCutLabel 22/22 labels: "          "
*%BrCutLabel 23/23 labels: "          "
*%BrCutLabel 24/24 labels: "          "
*%BrCutLabel 25/25 labels: "          "
*%BrCutLabel 26/26 labels: "          "
*%BrCutLabel 27/27 labels: "          "
*%BrCutLabel 28/28 labels: "          "
*%BrCutLabel 29/29 labels: "          "
*%BrCutLabel 30/30 labels: "          "
*%CloseUI: *BrCutLabel


*%  added Cut At End for QL720NW
*%OpenUI *BrCutAtEnd/Cut at end: PickOne
*%OrderDependency: 21 AnySetup  *BrCutAtEnd
*%DefaultBrCutAtEnd: ON
*%BrCutAtEnd OFF/OFF: "          "
*%BrCutAtEnd ON/ON: "          "
*%CloseUI: *BrCutAtEnd

*%=====  Added trim tape for QL720NW... =====
*OpenUI *BrTrimtape/Trim tape: PickOne
*OrderDependency: 21 AnySetup  *BrTrimtape
*DefaultBrTrimtape: OFF
*BrTrimtape OFF/OFF: "          "
*BrTrimtape ON/ON: "          "
*CloseUI: *BrTrimtape
*%===== ... Added trim tape for QL720NW =====


*OpenUI *BrMirror/Mirror Printing: PickOne
*OrderDependency: 21 AnySetup  *BrMirror
*DefaultBrMirror: OFF
*BrMirror OFF/OFF: "          "
*BrMirror ON/ON: "          "
*CloseUI: *BrMirror

*OpenUI *BrRotate/Rotate Printing: PickOne
*OrderDependency: 21 AnySetup  *BrRotate
*DefaultBrRotate: OFF
*BrRotate OFF/OFF: "          "
*BrRotate ON/ON: "          "
*CloseUI: *BrRotate

*OpenUI *BrPeeler/Peeler: PickOne
*OrderDependency: 21 AnySetup  *BrPeeler
*DefaultBrPeeler: OFF
*BrPeeler OFF/OFF: "          "
*BrPeeler ON/ON: "          "
*CloseUI: *BrPeeler

*CloseGroup: Option

*%==== Custom Page Sizes ====================
*%NonUIOrderDependency: 40 AnySetup *CustomPageSize
*%VariablePaperSize: True
*%LeadingEdge Short: "          "
*%DefaultLeadingEdge: Short
*%MaxMediaWidth: "612"
*%MaxMediaHeight: "1008"
*%HWMargins: 12 12 12 12
*%CustomPageSize True: "
*%	<</BRCustomPageSize true>> setpagedevice
*%	pop pop pop
*%	<< /PageSize [ 5 -2 roll ] /ImagingBBox null >>
*%	setpagedevice
*%"
*%End
*%ParamCustomPageSize Width: 1 points 284 612
*%ParamCustomPageSize Height: 2 points 595 1008
*%ParamCustomPageSize WidthOffset: 3 points 0 0
*%ParamCustomPageSize HeightOffset: 4 points 0 0
*%ParamCustomPageSize Orientation: 5 int 0 3

*%=== Paper Feed================================
*%OpenUI *InputSlot: PickOne
*%OrderDependency: 13 AnySetup *InputSlot
*%DefaultInputSlot: AutoSelect
*%InputSlot AutoSelect/Auto Select: 	"          "
*%InputSlot Tray1/Tray1: 		"          "
*%InputSlot Manual/Manual: 		"          "
*%CloseUI: *InputSlot

*%==== Resolution Features =================================
*%OpenUI *Resolution/Resolution: PickOne
*%OrderDependency: 14 AnySetup *Resolution
*%DefaultResolution: Normal
*%Resolution	Normal/Normal(300 * 300dpi): 	"          "
*%CloseUI: *Resolution

*%=== Media Type ================================
*%OpenUI *MediaType/Media Type: PickOne
*%OrderDependency: 17 AnySetup *MediaType
*%DefaultMediaType: Plain
*%MediaType Plain/Plain Paper: "          "
*%MediaType Thin/Thin Paper: "          "
*%MediaType Thick/Thick Paper: "          "
*%MediaType Thicker/Thicker Paper: "          "
*%MediaType Transparencies/Transparencies: "          "
*%MediaType Recycled/Recycled Paper: "          "
*%CloseUI: *MediaType

*%#S1

*OpenGroup: Advanced

*OpenUI *BrHalftonePattern/Halftone: PickOne
*OrderDependency: 24 AnySetup  *BrHalftonePattern
*DefaultBrHalftonePattern: BrErrorDiffusion
*BrHalftonePattern BrBinary/Binary: "          "
*BrHalftonePattern BrDither/Dither: "          "
*BrHalftonePattern BrErrorDiffusion/Error Diffusion: "          "
*CloseUI: *BrHalftonePattern

*OpenUI *BrBrightness/Brightness: PickOne
*OrderDependency: 25 AnySetup  *BrBrightness
*DefaultBrBrightness: 0
*BrBrightness 50/50: "          "
*BrBrightness 49/49: "          "
*BrBrightness 48/48: "          "
*BrBrightness 47/47: "          "
*BrBrightness 46/46: "          "
*BrBrightness 45/45: "          "
*BrBrightness 44/44: "          "
*BrBrightness 43/43: "          "
*BrBrightness 42/42: "          "
*BrBrightness 41/41: "          "
*BrBrightness 40/40: "          "
*BrBrightness 39/39: "          "
*BrBrightness 38/38: "          "
*BrBrightness 37/37: "          "
*BrBrightness 36/36: "          "
*BrBrightness 35/35: "          "
*BrBrightness 34/34: "          "
*BrBrightness 33/33: "          "
*BrBrightness 32/32: "          "
*BrBrightness 31/31: "          "
*BrBrightness 30/30: "          "
*BrBrightness 29/29: "          "
*BrBrightness 28/28: "          "
*BrBrightness 27/27: "          "
*BrBrightness 26/26: "          "
*BrBrightness 25/25: "          "
*BrBrightness 24/24: "          "
*BrBrightness 23/23: "          "
*BrBrightness 22/22: "          "
*BrBrightness 21/21: "          "
*BrBrightness 20/20: "          "
*BrBrightness 19/19: "          "
*BrBrightness 18/18: "          "
*BrBrightness 17/17: "          "
*BrBrightness 16/16: "          "
*BrBrightness 15/15: "          "
*BrBrightness 14/14: "          "
*BrBrightness 13/13: "          "
*BrBrightness 12/12: "          "
*BrBrightness 11/11: "          "
*BrBrightness 10/10: "          "
*BrBrightness 9/9: "          "
*BrBrightness 8/8: "          "
*BrBrightness 7/7: "          "
*BrBrightness 6/6: "          "
*BrBrightness 5/5: "          "
*BrBrightness 4/4: "          "
*BrBrightness 3/3: "          "
*BrBrightness 2/2: "          "
*BrBrightness 1/1: "          "
*BrBrightness 0/0: "          "
*BrBrightness -1/-1: "          "
*BrBrightness -2/-2: "          "
*BrBrightness -3/-3: "          "
*BrBrightness -4/-4: "          "
*BrBrightness -5/-5: "          "
*BrBrightness -6/-6: "          "
*BrBrightness -7/-7: "          "
*BrBrightness -8/-8: "          "
*BrBrightness -9/-9: "          "
*BrBrightness -10/-10: "          "
*BrBrightness -11/-11: "          "
*BrBrightness -12/-12: "          "
*BrBrightness -13/-13: "          "
*BrBrightness -14/-14: "          "
*BrBrightness -15/-15: "          "
*BrBrightness -16/-16: "          "
*BrBrightness -17/-17: "          "
*BrBrightness -18/-18: "          "
*BrBrightness -19/-19: "          "
*BrBrightness -20/-20: "          "
*BrBrightness -21/-21: "          "
*BrBrightness -22/-22: "          "
*BrBrightness -23/-23: "          "
*BrBrightness -24/-24: "          "
*BrBrightness -25/-25: "          "
*BrBrightness -26/-26: "          "
*BrBrightness -27/-27: "          "
*BrBrightness -28/-28: "          "
*BrBrightness -29/-29: "          "
*BrBrightness -30/-30: "          "
*BrBrightness -31/-31: "          "
*BrBrightness -32/-32: "          "
*BrBrightness -33/-33: "          "
*BrBrightness -34/-34: "          "
*BrBrightness -35/-35: "          "
*BrBrightness -36/-36: "          "
*BrBrightness -37/-37: "          "
*BrBrightness -38/-38: "          "
*BrBrightness -39/-39: "          "
*BrBrightness -40/-40: "          "
*BrBrightness -41/-41: "          "
*BrBrightness -42/-42: "          "
*BrBrightness -43/-43: "          "
*BrBrightness -44/-44: "          "
*BrBrightness -45/-45: "          "
*BrBrightness -46/-46: "          "
*BrBrightness -47/-47: "          "
*BrBrightness -48/-48: "          "
*BrBrightness -49/-49: "          "
*BrBrightness -50/-50: "          "
*CloseUI: *BrBrightness

*OpenUI *BrContrast/Contrast: PickOne
*OrderDependency: 26 AnySetup  *BrContrast
*DefaultBrContrast: 0
*BrContrast 50/50: "          "
*BrContrast 49/49: "          "
*BrContrast 48/48: "          "
*BrContrast 47/47: "          "
*BrContrast 46/46: "          "
*BrContrast 45/45: "          "
*BrContrast 44/44: "          "
*BrContrast 43/43: "          "
*BrContrast 42/42: "          "
*BrContrast 41/41: "          "
*BrContrast 40/40: "          "
*BrContrast 39/39: "          "
*BrContrast 38/38: "          "
*BrContrast 37/37: "          "
*BrContrast 36/36: "          "
*BrContrast 35/35: "          "
*BrContrast 34/34: "          "
*BrContrast 33/33: "          "
*BrContrast 32/32: "          "
*BrContrast 31/31: "          "
*BrContrast 30/30: "          "
*BrContrast 29/29: "          "
*BrContrast 28/28: "          "
*BrContrast 27/27: "          "
*BrContrast 26/26: "          "
*BrContrast 25/25: "          "
*BrContrast 24/24: "          "
*BrContrast 23/23: "          "
*BrContrast 22/22: "          "
*BrContrast 21/21: "          "
*BrContrast 20/20: "          "
*BrContrast 19/19: "          "
*BrContrast 18/18: "          "
*BrContrast 17/17: "          "
*BrContrast 16/16: "          "
*BrContrast 15/15: "          "
*BrContrast 14/14: "          "
*BrContrast 13/13: "          "
*BrContrast 12/12: "          "
*BrContrast 11/11: "          "
*BrContrast 10/10: "          "
*BrContrast 9/9: "          "
*BrContrast 8/8: "          "
*BrContrast 7/7: "          "
*BrContrast 6/6: "          "
*BrContrast 5/5: "          "
*BrContrast 4/4: "          "
*BrContrast 3/3: "          "
*BrContrast 2/2: "          "
*BrContrast 1/1: "          "
*BrContrast 0/0: "          "
*BrContrast -1/-1: "          "
*BrContrast -2/-2: "          "
*BrContrast -3/-3: "          "
*BrContrast -4/-4: "          "
*BrContrast -5/-5: "          "
*BrContrast -6/-6: "          "
*BrContrast -7/-7: "          "
*BrContrast -8/-8: "          "
*BrContrast -9/-9: "          "
*BrContrast -10/-10: "          "
*BrContrast -11/-11: "          "
*BrContrast -12/-12: "          "
*BrContrast -13/-13: "          "
*BrContrast -14/-14: "          "
*BrContrast -15/-15: "          "
*BrContrast -16/-16: "          "
*BrContrast -17/-17: "          "
*BrContrast -18/-18: "          "
*BrContrast -19/-19: "          "
*BrContrast -20/-20: "          "
*BrContrast -21/-21: "          "
*BrContrast -22/-22: "          "
*BrContrast -23/-23: "          "
*BrContrast -24/-24: "          "
*BrContrast -25/-25: "          "
*BrContrast -26/-26: "          "
*BrContrast -27/-27: "          "
*BrContrast -28/-28: "          "
*BrContrast -29/-29: "          "
*BrContrast -30/-30: "          "
*BrContrast -31/-31: "          "
*BrContrast -32/-32: "          "
*BrContrast -33/-33: "          "
*BrContrast -34/-34: "          "
*BrContrast -35/-35: "          "
*BrContrast -36/-36: "          "
*BrContrast -37/-37: "          "
*BrContrast -38/-38: "          "
*BrContrast -39/-39: "          "
*BrContrast -40/-40: "          "
*BrContrast -41/-41: "          "
*BrContrast -42/-42: "          "
*BrContrast -43/-43: "          "
*BrContrast -44/-44: "          "
*BrContrast -45/-45: "          "
*BrContrast -46/-46: "          "
*BrContrast -47/-47: "          "
*BrContrast -48/-48: "          "
*BrContrast -49/-49: "          "
*BrContrast -50/-50: "          "
*CloseUI: *BrContrast

*CloseGroup: Advanced

*DefaultFont: Courier
*Font AvantGarde-Book: Standard "(001.006S)" Standard ROM
*Font AvantGarde-BookOblique: Standard "(001.006S)" Standard ROM
*Font AvantGarde-Demi: Standard "(001.007S)" Standard ROM
*Font AvantGarde-DemiOblique: Standard "(001.007S)" Standard ROM
*Font Bookman-Demi: Standard "(001.004S)" Standard ROM
*Font Bookman-DemiItalic: Standard "(001.004S)" Standard ROM
*Font Bookman-Light: Standard "(001.004S)" Standard ROM
*Font Bookman-LightItalic: Standard "(001.004S)" Standard ROM
*Font Courier: Standard "(002.004S)" Standard ROM
*Font Courier-Bold: Standard "(002.004S)" Standard ROM
*Font Courier-BoldOblique: Standard "(002.004S)" Standard ROM
*Font Courier-Oblique: Standard "(002.004S)" Standard ROM
*Font Helvetica: Standard "(001.006S)" Standard ROM
*Font Helvetica-Bold: Standard "(001.007S)" Standard ROM
*Font Helvetica-BoldOblique: Standard "(001.007S)" Standard ROM
*Font Helvetica-Narrow: Standard "(001.006S)" Standard ROM
*Font Helvetica-Narrow-Bold: Standard "(001.007S)" Standard ROM
*Font Helvetica-Narrow-BoldOblique: Standard "(001.007S)" Standard ROM
*Font Helvetica-Narrow-Oblique: Standard "(001.006S)" Standard ROM
*Font Helvetica-Oblique: Standard "(001.006S)" Standard ROM
*Font NewCenturySchlbk-Bold: Standard "(001.009S)" Standard ROM
*Font NewCenturySchlbk-BoldItalic: Standard "(001.007S)" Standard ROM
*Font NewCenturySchlbk-Italic: Standard "(001.006S)" Standard ROM
*Font NewCenturySchlbk-Roman: Standard "(001.007S)" Standard ROM
*Font Palatino-Bold: Standard "(001.005S)" Standard ROM
*Font Palatino-BoldItalic: Standard "(001.005S)" Standard ROM
*Font Palatino-Italic: Standard "(001.005S)" Standard ROM
*Font Palatino-Roman: Standard "(001.005S)" Standard ROM
*Font Times-Bold: Standard "(001.007S)" Standard ROM
*Font Times-BoldItalic: Standard "(001.009S)" Standard ROM
*Font Times-Italic: Standard "(001.007S)" Standard ROM
*Font Times-Roman: Standard "(001.007S)" Standard ROM
*Font ZapfChancery-MediumItalic: Standard "(001.007S)" Standard ROM
*Font ZapfDingbats: Special "(001.004S)" Special ROM
*Font Symbol: Special "(001.007S)" Special ROM
*Font Alaska: Standard "(001.005)" Standard ROM
*Font AlaskaExtrabold: Standard "(001.005)" Standard ROM
*Font AntiqueOakland: Standard "(001.005)" Standard ROM
*Font AntiqueOakland-Bold: Standard "(001.005)" Standard ROM
*Font AntiqueOakland-Oblique: Standard "(001.005)" Standard ROM
*Font ClevelandCondensed: Standard "(001.005)" Standard ROM
*Font Connecticut: Standard "(001.005)" Standard ROM
*Font Guatemala-Antique: Standard "(001.005)" Standard ROM
*Font Guatemala-Bold: Standard "(001.005)" Standard ROM
*Font Guatemala-Italic: Standard "(001.005)" Standard ROM
*Font Guatemala-BoldItalic: Standard "(001.005)" Standard ROM

*Font LetterGothic: Standard "(001.005)" Standard ROM
*Font LetterGothic-Bold: Standard "(001.005)" Standard ROM
*Font LetterGothic-Oblique: Standard "(001.005)" Standard ROM
*Font Maryland: Standard "(001.005)" Standard ROM
*Font Oklahoma: Standard "(001.005)" Standard ROM
*Font Oklahoma-Bold: Standard "(001.005)" Standard ROM
*Font Oklahoma-Oblique: Standard "(001.005)" Standard ROM
*Font Oklahoma-BoldOblique: Standard "(001.005)" Standard ROM
*Font Utah: Standard "(001.005)" Standard ROM
*Font Utah-Bold: Standard "(001.005)" Standard ROM
*Font Utah-Oblique: Standard "(001.005)" Standard ROM
*Font Utah-BoldOblique: Standard "(001.005)" Standard ROM
*Font UtahCondensed: Standard "(001.005)" Standard ROM
*Font UtahCondensed-Bold: Standard "(001.005)" Standard ROM
*Font UtahCondensed-Oblique: Standard "(001.004)" Standard ROM
*Font UtahCondensed-BoldOblique: Standard "(001.005)" Standard ROM
*Font BermudaScript: Standard "(001.005)" Standard ROM
*Font Germany: Standard "(001.005)" Standard ROM
*Font SanDiego: Standard "(001.005)" Standard ROM
*Font US-Roman: Standard "(001.005)" Standard ROM


ENDOFPPDFILE

brotherlpdwrapper=/usr/lib/cups/filter/brother_lpdwrapper_${printer_model}
brotherlpdwrapper64=/usr/lib64/cups/filter/brother_lpdwrapper_${printer_model}
#brotherlpdwrapper=/usr/lib/cups/backend/brusb_${printer_model}
#brotherlpdwrapper64=/usr/lib64/cups/backend/brusb_${printer_model}

rm -f  $brotherlpdwrapper
#echo 'rm -f /usr/lib/cups/filter/brlpdwrapper${printer_model}'


cat <<!ENDOFWFILTER! >$brotherlpdwrapper
#! /bin/sh
#
# Brother Print filter  >>  /usr/lib/cups/backend/brusb_${printer_model}
# Copyright (C) 2006-2022 Brother. Industries, Ltd.
#                                    Ver1.10

# 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
#

LOGFILE="/dev/null"
LOGLEVEL="1"
LOGCLEVEL="7"
DEBUG=0
NUPENABLE=1
LOG_LATESTONLY=1

touch /tmp/${printer_model}_latest_print_info
chmod 600 -R /tmp/${printer_model}_latest_print_info

errorcode=0

if [ \$DEBUG != 0 ]; then
    LOGFILE=/tmp/br_cupsfilter_debug_log
fi

PPDC=\`printenv | grep "PPD="\`
PPDC=\`echo \$PPDC | sed -e 's/PPD=//'\`

if [ "\$PPDC" = "" ]; then
    PPDC="/usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd"
fi


if [ \$LOGFILE != "/dev/null" ]; then
  if [ \$LOG_LATESTONLY == "1" ]; then
    rm -f \$LOGFILE
    date                                                           >\$LOGFILE
  else
    if [ -e \$LOGFILE ]; then
	    date                                                        >>\$LOGFILE
    else
	    date                                                        >\$LOGFILE
    fi
  fi
    echo "arg0 = \$0"                                           >>\$LOGFILE
    echo "arg1 = \$1"                                           >>\$LOGFILE
    echo "arg2 = \$2"                                           >>\$LOGFILE
    echo "arg3 = \$3"                                           >>\$LOGFILE
    echo "arg4 = \$4"                                           >>\$LOGFILE
    echo "arg5 = \$5"                                           >>\$LOGFILE
    echo "arg6 = \$6"                                           >>\$LOGFILE
    echo "PPD  = \$PPD"                                         >>\$LOGFILE
fi

cp  /opt/brother/${device_model}/${printer_model}/inf/br${printer_model}rc  /tmp/br${printer_model}rc_\$$
chmod 777 -R /tmp/br${printer_model}rc_\$$
export BRPRINTERRCFILE=/tmp/br${printer_model}rc_\$$

INPUT_TEMP_PS=\`mktemp /tmp/br_input_ps.XXXXXX\`

nup="cat"
if [ "\`echo \$5 | grep 'Nup='\`" != '' ] && [ \$NUPENABLE != 0 ]; then

	if   [ "\`echo \$5 | grep 'Nup=64'\`" != '' ]; then
		nup="psnup -64"
	elif [ "\`echo \$5 | grep 'Nup=32'\`" != '' ]; then
		nup="psnup -32"
	elif [ "\`echo \$5 | grep 'Nup=25'\`" != '' ]; then
		nup="psnup -25"
	elif [ "\`echo \$5 | grep 'Nup=16'\`" != '' ]; then
		nup="psnup -16"
	elif [ "\`echo \$5 | grep 'Nup=8'\`" != '' ]; then
		nup="psnup -8"
	elif [ "\`echo \$5 | grep 'Nup=6'\`" != '' ]; then
		nup="psnup -6"
	elif [ "\`echo \$5 | grep 'Nup=4'\`" != '' ]; then
		nup="psnup -4"
	elif [ "\`echo \$5 | grep 'Nup=2'\`" != '' ]; then
		nup="psnup -2"
	elif [ "\`echo \$5 | grep 'Nup=1'\`" != '' ]; then
		nup="cat"
	fi
	echo   "NUP=\$nup"                                      >>\$LOGFILE
	if [ -e /usr/bin/psnup ]; then
		if [ \$# -ge 7 ]; then
			cat \$6  | \$nup > \$INPUT_TEMP_PS
		else
			cat       | \$nup > \$INPUT_TEMP_PS
		fi
	else
		if [ \$# -ge 7 ]; then
			cp \$6  \$INPUT_TEMP_PS
		else
			cat    > \$INPUT_TEMP_PS
		fi
	fi
else
	if [ \$# -ge 7 ]; then
		cp \$6  \$INPUT_TEMP_PS
	else
		cat    > \$INPUT_TEMP_PS
	fi
fi
if [ -e "/opt/brother/${device_model}/${printer_model}/lpd/filter${printer_model}" ]; then
       :
else
    echo "ERROR: /opt/brother/${device_model}/${printer_model}/lpd/filter${printer_model} does not exist"   >>\$LOGFILE
    echo "ERROR: /opt/brother/${device_model}/${printer_model}/lpd/filter${printer_model} does not exist"   >>/tmp/${printer_model}_latest_print_info
    errorcode=30
    exit
fi

CUPSOPTION=\`echo "\$5 Copies=1" | sed -e 's/PageSize/media/' -e 's/BrMargin/Feed/' -e 's/BrCompress/Compress/' -e 's/BrCutAtEnd/CutAtEnd/' -e  's/BrTrimtape/Trimtape/' -e 's/BrRotate/RotatePrint/' -e 's/BrPeeler/Peeler/' -e 's/BrCutLabel/CutLabel/' -e 's/BrMirror/MirrorPrint/' -e 's/BrPriority/Priority/' -e 's/BrQuality/QUALITY/' -e 's/BrSpeed/SPEED/' -e 's/BrHalftonePattern/Halftone/' -e 's/BrErrorDiffusion/ERROR/' -e 's/BrBinary/BINARY/' -e 's/BrDither/DITHER/' -e 's/BrBrightness/Brightness/' -e 's/BrContrast/Contrast/' -e 's/multiple-document-handling/Collate/' -e 's/separate-documents-collated-copies/ON/' -e 's/separate-documents-uncollated-copies/OFF/'\`




BRURI=\`printenv | grep DEVICE_URI=\`
BRURI=\`echo \$BRURI | grep "brserial"\`
PORT="USB"
if [ "\$BRURI" != "" ]; then
	PORT="SERIAL"
fi

if [ -e "/opt/brother/${device_model}/${printer_model}/cupswrapper/brcupsconfpt1" ]; then
  if [ \$DEBUG = 0 ]; then
     /opt/brother/${device_model}/${printer_model}/cupswrapper/brcupsconfpt1  ${printer_model}  \$PPDC 0 "\$CUPSOPTION" \$BRPRINTERRCFILE>> /dev/null
  else
     /opt/brother/${device_model}/${printer_model}/cupswrapper/brcupsconfpt1  ${printer_model}  \$PPDC \$LOGCLEVEL "\$CUPSOPTION" \$BRPRINTERRCFILE>>\$LOGFILE
  fi
fi

if [ \$DEBUG -lt 10 ]; then
    cat    \$INPUT_TEMP_PS | /opt/brother/${device_model}/${printer_model}/lpd/filter${printer_model} "\$\$" "CUPS" "\$PORT"
    echo br${printer_model}rc_\$$   > /tmp/${printer_model}_latest_print_info
    cat  /tmp/br${printer_model}rc_\$$  >> /tmp/${printer_model}_latest_print_info
    rm -f /tmp/br${printer_model}rc_\$$
    if [ \$LOGLEVEL -gt 2 ];  then
	   if [ \$LOGFILE != "/dev/null" ]; then
	     echo ""                                                >>\$LOGFILE
	     echo "    ------PostScript Data-------"                >>\$LOGFILE
	     cat    \$INPUT_TEMP_PS                                  >>\$LOGFILE
	   fi
    fi
fi
rm -f  \$INPUT_TEMP_PS

exit $errorcode

!ENDOFWFILTER!

chmod a+x $brotherlpdwrapper
if [ -e /usr/lib64/cups/backend ]; then
	if [ ! -e $brotherlpdwrapper64 ]; then
		cp -f $brotherlpdwrapper  $brotherlpdwrapper64
	fi
fi

if [ -e /etc/init.d/lpd ]; then
   /etc/init.d/lpd stop
fi
if [  -e /etc/init.d/lprng ]; then
   /etc/init.d/lprng stop
fi

if [  -e /etc/init.d/cups ]; then
   /etc/init.d/cups restart
elif [  -e /etc/init.d/cupsys ]; then
   /etc/init.d/cupsys restart
fi

sleep 2s

uris=$(lpinfo -v)

for uri in $uris
do
	URI=$(echo $uri | grep ${device_name} | grep usb)
	if [ "$URI" != '' ];then
		break;
	fi
done

if [ "$URI" = '' ];then
	for uri in $uris
	do
		URI=$(echo $uri | grep ${device_name} )
		if [ "$URI" != '' ];then
			break;
		fi
	done
fi

if [ "$URI" = '' ];then
	for uri in $uris
	do
		URI=$(echo $uri | grep -i Brother | grep usb )
		if [ "$URI" != '' ];then
			break;
		fi
	done
fi	

if [ "$URI" = '' ];then
	for uri in $uris
	do
		URI=$(echo $uri | grep usb )
		if [ "$URI" != '' ];then
			break;
		fi
	done
fi


if [ "$URI" = '' ];then
	URI="usb:/dev/usb/lp0"
fi

echo lpadmin -p ${device_name} -E -v $URI -P $ppd_file_name
lpadmin -p ${device_name} -E -v $URI -P $ppd_file_name

exit 0

