#! /bin/csh -f
#
#   lpf-escp -- ESC/PץѤΥե륿
#
#   lpf-escp.text: ƥȥե (ܸ쥳ɤѴʤɤԤ)
#   lpf-escp.bin: Ϥ򤽤Τޤޥץ󥿤˻Ȥ
#   lpf-escp.ps, lpf-escp.dvi: 줾 ps, dviե
#   lpf-escp.small: ̾
#
#   revision history:
#	0.0: Jan. 5, 94 by Dai Ishijima
#	1.0: Feb. 6, 96
#

set path=(/usr/bin /bin /usr/local/bin)

set prog=$0
set type=$prog:e
set basename=$prog:r

if (x$type == x) then
	foreach t (text bin ps dvi small)
		rm -f $basename.$t
		ln -s $prog $basename.$t
	end
	exit 0
endif

# ץ󥿽
#	ESC @		ꥻå
#	ESC P ESC Q 80	ѻ極8
#		ESC P     10ʸ/
#		ESC Q 80  ѻ80ʸ
#	ESC l 5		ޡ1/2 (5ʸʬ) 
set init='\033@\033P\033Q\120\033l\005'

#	ESC x 0		® (ɥե) 
#	FS x 1		®
#	ESC M		꡼ȥ (12ʸ/) 
#	FS S 3 3	κ;
set init="$init"'\033x0\034x1\033M\034S\003\003'

# ֥ȥåפ (ESC D): 8ʸ
set init="$init"'\033D\010\020\030\040\050\060\070\100\110\120\010'

# 塢ڡ (^L) ƥꥻå (ESC @)
set fin="\014\033@"

set  kin=`printf '\033$B'`
set kout=`printf '\033(B'`
set  escp_kin=`printf '\034\\&'`
set escp_kout=`printf '\034\\.'`

printf "$init"
if ($type == text) then
	nkf -j |\
	sed -e 's/'"$kin"'/'"$escp_kin"'/g' -e 's/'"$kout"'/'"$escp_kout"'/g'
else if ($type == ps) then
	gs -sDEVICE=epson -r180x180 -q -sOutputFile=- -
else if ($type == dvi) then
	jdvi2kps | gs -sDEVICE=epson -r180x180 -q -sOutputFile=- -
else if ($type == small) then
	nkf -s | slpr
else if ($type == bin) then
	cat
else
	cat
endif
printf "$fin"
