#!/bin/bash
# Copyright 2011-2019 Chad Lemmen http://www.lemmen.com
# Print script

file=$1
dest=$2
copies=$3
tray=$4
opts=$5
lpopt="-o raw"

if [ "$opts" == "12cpi" ]; then
  sub=Y
fi
  
# From the PCL 5 reference manual, common VMI settings 
# \e&l7.27C 7.27=(10/66)*48 66 lines per page
# \e&k2G CR=CR LF=CR+LF FF=CR+FF

# Letter paper \e&l2A, not setting, if using A4 it caused prompt: load the tray
# Roman-8 Symbol Set
# Courier Font 10 cpi
# 7.27 lpi
# Auto CR
# Text Length 66
pcl="\e(8U\e(s0p10h0s0b4099T\e&l7.27C\e&k2G\e&l66F"

case $tray in
    1) pcl="${pcl}\e&l4H";;  # Tray1 
    2) pcl="${pcl}\e&l1H";;  # Tray2
    3) pcl="${pcl}\e&l5H";;  # Tray3
    4) pcl="${pcl}\e&l8H";;  # Tray4
    5) pcl="${pcl}\e&l9H";;  # Tray5
    *) pcl="${pcl}\e&l7H";;  # Auto
esac


case "$opts" in
  xpnd)    pcl="\e@\eM\ex1\ek0\eR0\e2" # Epson 12 cpi, Roman NLQ, 6 lpi
           # 66 lines on 11 inch paper.
           # 60 printable with 3 lines .5 inch each top and bottom margins.
           # Margin value is in points, each point is 1/72 inch.
           #lpopt="-o cpi=10 -o lpi=6 -o page-top=36 -o page-bottom=36"
           ;;
  epson)   pcl="";;                                # Epson set null pcl 
  10.5cpi) pcl="${pcl}\e(s0p10.5H";;               # Pitch 10.5cpi
  63l)     pcl="\e&k2G\e&l7.62C\e(s0p10.5H";;      # Pitch 10.5cpi 63 lines
  10cpi)   pcl="${pcl}\e&k0S";;                    # Pitch 10cpi
  12cpi)   pcl="${pcl}\e&k4S";;                    # Pitch 12cpi
  12c8lpi) pcl="${pcl}\e&k4S\e&l8D\e&l3E\e&l83F";; # 12cpi, 8 lpi
                                                   #   Top Margin 3 lines
                                                   #   Text Length 83
  comp)    pcl="${pcl}\e&k2S";;                    # Pitch Compressed
  *)       pcl="${pcl}\e&k2S";;                    # Pitch Compressed
esac


if [ "$sub" == "Y" ]; then
  # Add our print codes then prepend pcl codes using process substitution
  prtcodes "$file"|cat <(echo -ne "$pcl") -|lp -s "$lpopt" -n"$copies" -d"$dest"
else
  echo -ne "$pcl" | cat - "$file" | lp -s "$lpopt" -n"$copies" -d"$dest"
fi

