#!/bin/bash # Thu Dec 18 05:56:51 EST 2003 # NAME: ps-envelopes # Copyright 2003, Chris F.A. Johnson # Released under the terms of the GNU General Public License version() { echo " $progname, version $version Copyright $copyright, $author $email This is free software, released under the terms of the GNU General Public License. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " } usage() { echo " $progname - USAGE: $progname [OPTIONS] OPTIONS: -c FILE - read configuration from FILE -h - help: print this message -H - help: print more detailed message (if there is one) -v - verbose: (may not do anything) -V - print version information and exit Copyright 2003, Chris F.A. Johnson " } prolog() { printf "%s\n" "%!PS-Adobe-3.0 /F { findfont exch scalefont setfont } def /pointsize $pointsize def /leading $leading $pointsize mul neg def /showline { gsave show grestore 0 leading rmoveto } def /inches { 72 mul } def %%EndProlog " printf "%s\n" "$pointsize /$font F" } print_return_address() { printf " gsave\n" printf " %s\n" "/leading $rleading $rpointsize mul neg def" printf " %s\n" "$rpointsize /$rfont F" printf " %s\n" "$return_x inches $return_y inches moveto" printf " (%s) showline\n" "${return[@]}" printf " grestore\n" } print_address() { for f in "${fields[@]}" do case $f in *,*) str= fsep=${f##*[0-9]} f=${f%${fsep:= }} for fld in ${f//,/ } do str=${str:+"$str$fsep"}${values[$fld]} done printf "\t(%s) showline\n" "$str" ;; *) [ -n "${values[$f]}" ] && printf "\t(%s) showline\n" "${values[$f]}" ;; esac done } print_envelope() { printf "\n%%%%Page: %s\n" "${page:=1}" printf "save\n" printf "%s\n" "612 0 moveto currentpoint translate 90 rotate" print_return_address printf " gsave\n" printf " %s\n" "/leading $leading $pointsize mul neg def" printf "\t%s\n" "$address_x inches $address_y inches moveto" print_address printf " grestore\n" printf " %s\n" "showpage" printf "restore\n" } csv_split() { local record=${1%"${CR}"} local right local vnum=0 unset values while [ -n "$record" ] do case $record in \"*) right=${record#*\",} value=${record%%\",*} values[$vnum]=${value#\"} ;; *) values[$vnum]=${record%%,*} right=${record#*,} ;; esac case $record in *,*) record=${right} ;; *) record=${record#\"} values[$vnum]=${record%\"} break;; esac values[$vnum]=${values[$vnum]//\(/\\(} values[$vnum]=${values[$vnum]//\)/\\)} vnum=$(( $vnum + 1 )) done } set_values() { local IFS="${separator:-" "}" case $file_type in *CSV*) csv_split "$1" ;; *) set -f set -- ${1%"${CR}"} values=( "$@" ) IFS=${IFS:=" ${NL}"} set +f ;; esac num_values=${#values[@]} } verbose=0 longusage=0 version="1.0" copyright=2003 author="Chris F.A. Johnson" progname=${0##*/} config_file=.ps-envelopesrc while getopts vVhH-:c: var do case $var in c) config_file=$OPTARG ;; -) case $OPTARG in help) usage; exit ;; help_long) longusage=1; usage; exit ;; version) version; exit ;; esac ;; h) usage; exit ;; H) longusage=1; usage; exit ;; v) verbose=$(( $verbose + 1 )) ;; V) version; exit ;; *);; esac done shift $(( $OPTIND - 1 )) return=( "Your Name" "123 Forth Street" "My Town" "Province" "Postal Code" ) pointsize=11 rpointsize=14 ## return address leading=1.2 rleading=1.1 font=Helvetica ##Century-Schoolbook ##Palatino ##Times-Roman # etc... rfont=Century ## return address file_type=CSV separator=, ## required only for non-CSV files ## These positions are guesstimates; adjust to taste ## Units are inches ## position of return address return_x=.333 return_y=5.5 ## position of recipient's address address_x=4 address_y=3.5 ## Fields to print (first field is 0) ## To print more than one field in a line, separate field numbers with a comma ## by default, a space will be used to separate the field; ## to use something else, put it after the last number ## In the example, fields 3 and 2, in that order, will be printed ## on line 3, separated by " -- " #fields=( 0 1 3,2\ --\ 4 5 6 7 ) fields=( 0 1 2 3 4 5 6 7 8 ) [ -f "$config_file" ] && . "$config_file" prolog page=1 cat "$@" | while IFS= read -r line do set_values "$line" print_envelope page=$(( $page + 1 )) done