#!/usr/local/bin/python2.2

#
# (C) Mizi Research
# distributed under GPL
#
# Author: Hwang, ChiDeok (hwang@mizi.co.kr)
# 2000.03.14
#
# ChangeLog:
# -o option  쳪 argument ־ 
# ps  ƴ  a2ps  θ 
# argument  ־  stdin о帲. (2000/03/16)
# stringȯ postscript language ϵ . (2000/03/20)
# stringȯ ϰ     (2000/03/23)

import string,sys,os

hps = '/Gulim-Medium-KSC-EUC-H'
hbps = '/Gulim-Medium-KSC-EUC-H'
hbops = '/Gulim-Medium-KSC-EUC-H'
hops = '/Gulim-Medium-KSC-EUC-H'

def trans_file(filename):
	if type(filename) == type([]):
		lines = filename
	else:
		lines = open(filename).readlines()
	for i in range(len(lines)):
		if lines[i] == '% Check PostScript language level.\n':
			print show_override
			print lines[i],
		elif lines[i] == '% Dictionary for ISO-8859-1 support\n':
			print lines[i],
			print lines[i+1],
			print hangul_font
			lines = lines[i+10:]
			break
		else:
			print lines[i],
	for line in lines:
		print line,
hangul_font = """\
16 dict begin
  /FontName /fCourier def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Courier findfont %(hps)s findfont ] def
  FontName currentdict
  end
definefont /fCourier exch def

16 dict begin
  /FontName /fCourier-Bold def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Courier-Bold findfont %(hbps)s findfont ] def
  FontName currentdict
  end
definefont /fCourier-Bold exch def

16 dict begin
  /FontName /fCourier-BoldOblique def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Courier-BoldOblique findfont %(hbops)s findfont ] def
  FontName currentdict
  end
definefont /fCourier-BoldOblique exch def

16 dict begin
  /FontName /fCourier-Oblique def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Courier-Oblique findfont %(hops)s findfont ] def
  FontName currentdict
  end
definefont /fCourier-Oblique exch def

16 dict begin
  /FontName /fHelvetica def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Helvetica findfont %(hps)s findfont ] def
  FontName currentdict
  end
definefont /fHelvetica exch def

16 dict begin
  /FontName /fHelvetica-Bold def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Helvetica-Bold findfont %(hbps)s findfont ] def
  FontName currentdict
  end
definefont /fHelvetica-Bold exch def

16 dict begin
  /FontName /fTimes-Bold def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Times-Bold findfont %(hbps)s findfont ] def
  FontName currentdict
  end
definefont /fTimes-Bold exch def

16 dict begin
  /FontName /fTimes-Roman def /FontType 0 def
  /WMode 0 def /FMapType 3 def /FontMatrix matrix def
  /Encoding [0 1] def
  /FDepVector [ /Times-Roman findfont %(hps)s findfont ] def
  FontName currentdict
  end
definefont /fTimes-Roman exch def
""" % locals()

show_override = """\
% 
/han-trans {
	dup length 3 mul string /mystr exch def
	/mylen 0 def
	/in-hangul false def
	{ 
		dup 128 gt {
			in-hangul {
			  % c
			   mystr exch mylen exch put
			   /mylen mylen 1 add def
			} {
			  /in-hangul true def
			  % 255 1 c 
			  mystr mylen (\\377\\1) putinterval  
			  mystr exch mylen 2 add exch put
			   /mylen mylen 3 add def
			} ifelse
		} {
			in-hangul not {
			  % c
			  mystr exch mylen exch put
			   /mylen mylen 1 add def
			} {
			  /in-hangul false def
			  % 255 0 c
			  mystr mylen (\\377\\0) putinterval  
			  mystr exch mylen 2 add exch put
			   /mylen mylen 3 add def
			} ifelse
		} ifelse
	} forall
	mystr 0 mylen getinterval
} bind def 

/origshow {show} bind def
/show {han-trans origshow} bind def
/origstringwidth {stringwidth} bind def
/stringwidth { han-trans origstringwidth } bind def

%\
"""


if '-o' in sys.argv:
	i = sys.argv.index('-o')
	out = sys.argv[1+i]
	sys.stdout = open(out, 'w')
	del sys.argv[i:i+2]
else:
	out = None
def run_a2ps():
	os.system('a2ps -o /tmp/a2ps-h.ps ' + string.join(sys.argv[1:]))
	if out:
		sys.stdout = open(out, 'w')
	trans_file('/tmp/a2ps-h.ps')
	os.remove('/tmp/a2ps-h.ps')
if len(sys.argv) == 1:
	trans_file(sys.stdin.readlines())
elif len(sys.argv) > 2:
	run_a2ps()
else:
	if sys.argv[1] == '-h':
		sys.exit(1)
	if open(sys.argv[1]).read(5) == '%!PS-':
		trans_file(sys.argv[1])	
	else:
		run_a2ps()
