#!/bin/bash
# Copyright 2008-2019 Chad Lemmen http://www.lemmen.com
#
# Insert printer PCL escape codes using space placeholders in a 4gl report
#
# 2 spaces = <esc>(s0B               Normal Print
# 3 spaces = <esc>(s3B               Bold Print
# 4 spaces = <esc>(s1p20v1s3b16602T  Arial Bold Italic, 20 point size
# 5 spaces = <esc>(s0p10.5h0s0b4099T Courier, 10.5 pitch
# 6 spaces = <esc>*c+2400a+10b100g0P Draw line right Len=2400 Wid=10 100% black
# 6 '='    = ''
# 7 spaces = <esc>*c+2400a+5b100g0P  Draw line right Len=2400 Wid=5 100% black
# 7 '-'    = '' 

# The [^ ] matches any character NOT enclosed in [] in this case 1 space 
# the () around the [^ ] makes it a group, but the () need to be escaped with \
# the \1 repeats the group once.
# So we are matching any line ending in x number of spaces, but no more than x.
# http://www.panix.com/~elflord/unix/sed.html
# http://sed.sourceforge.net/sedfaq3.html


cat $1 | sed -e 's/\([^ ]\)  $/\1(s0B/' \
-e 's/^   $/(s3B/' \
-e 's/^    $/(s1p20v1s3b16602T/' \
-e 's/^      $/*c+2400a+10b100g0P/' \
-e 's/^=\{6,\}$/*c+2400a+10b100g0P/' \
-e 's/^       $/*c+2400a+5b100g0P/' \
-e 's/^-\{7,\}$/*c+2400a+5b100g0P/'
