#!/bin/bash
#
# tops.sh - text filter into PostScript, shell script version
#
# Copyright (C) 2001-2003 Taiji Yamada <taiji@aihara.co.jp>
#
tops_dir='/usr/local/share/tops'
#concat='yes'
concat='no'
#hexmode='yes'
hexmode='no'
odflags='-v -t x1'
dateformat="%Y/%m/%d %T"
if [ "$OSTYPE" != 'solaris' ]; then
  [ "`sed -V | grep 'multi-byte'`" != "" ] && sedflags='--ctype=ascii'
fi

case "$LANG" in
zh_TW.eucTW)	Encoding='euc-tw';;
zh_TW*)		Encoding='big5';;
zh*)		Encoding='euc-cn';;
#zh*)		Encoding='euccn';;		# for OCF & gs support only
#zh*)		Encoding='euc-china';;		# for both OCF and CID?
#zh*)		Encoding='iso-2022-cn';;
ja_JP.eucJP2)	Encoding='euc-jp2';;
ja_JP.euc*)	Encoding='euc-jp';;
ja_JP.SJIS)	Encoding='shift_jis';;
ja_JP.JIS*)	Encoding='iso-2022-jp';;
#ja*)		Encoding='iso-2022-jp2';;
ja*)		Encoding='eucjp';;		# for OCF & gs support only
#ja*)		Encoding='euc-japan';;		# for both OCF and CID
ko*)		Encoding='euc-kr';;
#ko*)		Encoding='euckr';;		# for OCF & gs support only
#ko*)		Encoding='euc-korea';;		# for both OCF and CID?
#ko*)		Encoding='iso-2022-kr';;
#)		Encoding='iso-2022-cjk';;	# for Emacs/Mule cjk text
#)		Encoding='iso-2022-m17n';;	# for Emacs/Mule m17n text
#)		Encoding='utf8-cn';;
#)		Encoding='utf8-jp';;
#)		Encoding='utf8-kr';;
#)		Encoding='utf8-tw';;
#)		Encoding='utf16-cn';;
#)		Encoding='utf16-jp';;
#)		Encoding='utf16-kr';;
#)		Encoding='utf16-tw';;
*)		Encoding='iso-6429';;		# depend on defined Latin fonts
esac

setlocalcat(){		# Please set local filter in place of ``cat'' and
case "$Encoding" in	# let me know suitable one for your locale!
b5-eten)	localcat="cat";;
big5)		localcat="cat";;
euc-china)	localcat="cat";;
euc-cn)		localcat="cat";;
euc-japan)	localcat="nkf -e";;
euc-jp)		localcat="cat";;
euc-jp2)	localcat="cat";;
euc-korea)	localcat="cat";;
euc-kr)		localcat="cat";;
euc-tw)		localcat="cat";;
euccn)		localcat="cat";;
eucjp)		localcat="nkf -e";;
euckr)		localcat="cat";;
gbk2k)		localcat="cat";;
hkscs)		localcat="cat";;
iso-2022-cjk)	localcat="cat";;
iso-2022-cn)	localcat="cat";;
iso-2022-jp)	localcat="nkf -j";;
iso-2022-jp2)	localcat="cat";;
iso-2022-kr)	localcat="cat";;
iso-2022-m17n)	localcat="cat";;
iso-6429)	localcat="cat";;
shift_jis)	localcat="cat";;
sjis)		localcat="nkf -s";;
uhc)		localcat="cat";;
utf8-cn)	localcat="cat";;
utf8-jp)	localcat="cat";;
utf8-kr)	localcat="cat";;
utf8-tw)	localcat="cat";;
utf16-cn)	localcat="cat";;
utf16-jp)	localcat="cat";;
utf16-kr)	localcat="cat";;
utf16-tw)	localcat="cat";;
*)		localcat="cat";;
esac
}

setlocalcat

setinfo(){
  timestamp=`date +"$dateformat"`
  if [ "$usertitle" != '' ]; then
    basename="$usertitle"
    basenames="$usertitle"
  else
    if [ $# != 0 ]; then
      basename="`basename $1`"
      basenames="$basename"; shift
      while [ "$1" != "" ]; do
        basenames="$basenames, `basename $1`"
        shift
      done
      [ "$concat" = 'yes' ] && basename="$basenames"
    else
      basename=''
      basenames='stdin'
    fi
  fi
}
# Please set variables for local systems here, and let me know your setting!
Media='A4'
MediaSize='595 842'
MediaMargin='25 29 25 29'
Margin='25 29 25 29'
Orient='Portrait'
Landscape='false'
Landscale='false'
EvenScale='true'
PageScale='true'
VertText='false'
FoldLine='true'
Division='2 2'
Header='true'
Footer='true'
Border='true'
FontSize='11'
FontWidth='11'
# fonts for single-byte encodings
Latin='Courier'
Arabic='CourierNewArabic'		# pseudo fonts
Cyrillic='CourierNewCyrillic'		# pseudo fonts
Greek='CourierNewGreek'			# pseudo fonts
Hebrew='CourierNewHebrew'		# pseudo fonts
Icelandic='CourierNewIcelandic'		# pseudo fonts
Latin3='CourierNewLatin3'		# pseudo fonts
Latin4='CourierNewLatin4'		# pseudo fonts
Latin6='CourierNewLatin6'		# pseudo fonts
Latin8='CourierNewLatin8'		# pseudo fonts
Roman='CourierNewRoman'			# pseudo fonts
Romanian='CourierNewRomanian'		# pseudo fonts
Thai='CourierNewThai'			# pseudo fonts
Turkish='CourierNewTurkish'		# pseudo fonts
Vietnamese='CourierNewVietnamese'	# pseudo fonts
# fonts for multi-byte encodings
CNS1='MSung-Light'
GB1='STSong-Light'
Japan1='HeiseiMin-W3'
Japan2='HeiseiMin-W3H'
Korea1='HYSMyeongJo-Medium'
# bold fonts for single-byte encodings
Latin_Bold='Courier-Bold'
Arabic_Bold='CourierNewArabic-Bold'		# pseudo fonts
Cyrillic_Bold='CourierNewCyrillic-Bold'		# pseudo fonts
Greek_Bold='CourierNewGreek-Bold'		# pseudo fonts
Hebrew_Bold='CourierNewHebrew-Bold'		# pseudo fonts
Icelandic_Bold='CourierNewIcelandic-Bold'	# pseudo fonts
Latin3_Bold='CourierNewLatin3-Bold'		# pseudo fonts
Latin4_Bold='CourierNewLatin4-Bold'		# pseudo fonts
Latin6_Bold='CourierNewLatin6-Bold'		# pseudo fonts
Latin8_Bold='CourierNewLatin8-Bold'		# pseudo fonts
Roman_Bold='CourierNewRoman-Bold'		# pseudo fonts
Romanian_Bold='CourierNewRomanian-Bold'		# pseudo fonts
Thai_Bold='CourierNewThai-Bold'			# pseudo fonts
Turkish_Bold='CourierNewTurkish-Bold'		# pseudo fonts
Vietnamese_Bold='CourierNewVietnamese-Bold'	# pseudo fonts
# bold fonts for multi-byte encodings
CNS1_Bold='MHei-Medium'
GB1_Bold='STHeiti-Regular'
Japan1_Bold='HeiseiKakuGo-W5'
Japan2_Bold='HeiseiKakuGo-W5H'
Korea1_Bold='HYGoThic-Medium'

GsSafe='false'
#GsSafe='true'

#
# Here, please write automatically-detect definitions for your locale!
#
detectaffairs(){
  case "$Encoding" in
  eucjp|euc-japan|sjis|iso-2022-jp)
    # for traditional Japanese PostScript printer
    Japan1='Ryumin-Light'
    Japan1_Bold='GothicBBB-Medium'
    ;;
  euc-jp|euc-jp2|shift_jis|iso-2022-jp2)
    # for recent Japanese PostScript printer
    Japan1='HeiseiMin-W3'
    Japan1_Bold='HeiseiKakuGo-W5'
    ;;
  esac
}

detectaffairs

me=`basename $0`
usage(){
  cat <<EOF
$me - text filter into PostScript, revision 20030610
Usage:	$me [-I Encoding]
	[-P MediaType] [-S MediaSize] [-M MediaMargin] [-m Margin]
	[-l] [-L|c|C] [-d Division] [-1|2|4|8|9|-T PageType] [-t] [-f] [-b]
	[-s FontSize|-n lines] [-w FontWidth] [-F Font=Name] [-V] [-B]
	[-D dateformat] [-H title] [-p pipe] [-g] [-h]
Options:
	-I Encoding	b5-eten, big5, euc-tw, euc-cn, euc-jp, euc-jp2, euc-kr,
			euc-china, euc-japan, euc-korea, euccn, eucjp, euckr,
			gbk2k, hkscs,
			iso-2022-cn, iso-2022-jp, iso-2022-jp2, iso-2022-kr,
			iso-2022-cjk, iso-2022-m17n,
			shift_jis, sjis, uhc,
			utf8-cn, utf8-jp, utf8-kr, utf8-tw,
			utf16-cn, utf16-jp, utf16-kr, utf16-tw,
			iso-6429, iso-8859-1, iso-8859-2, iso-8859-3,
			iso-8859-4, iso-8859-5, (iso-8859-6), iso-8859-7,
			(iso-8859-8), iso-8859-9, iso-8859-10, iso-8859-13,
			iso-8859-14, iso-8859-15, koi8-r,
			(mac-arabic), mac-ce, mac-croatian, mac-cyrillic,
			mac-greek, (mac-hebrew), mac-icelandic, mac-roman,
			(mac-romanian), mac-turkish,
			(ms-cp874), ms-cp1250, ms-cp1251, ms-cp1252, ms-cp1253,
			ms-cp1254, (ms-cp1255), (ms-cp1256), ms-cp1257,
			ms-cp1258
	-P MediaType	A4,A5,A6,A7,A8,A9,A10,B5,Letter,11x17,Ledger,Legal
	-S MediaSize	width,height
 	-M MediaMargin	left,top,right,bottom of media margin
 	-m Margin	left,top,right,bottom of page margin
	-l		Landscape mode
	-L		Landscape and scaling to media coordinate
	-c		media coordinate mode
	-C		device coordinate mode
	-d Division	horizontal and vertical division
	-1|2|4|8|9	print multi-page
	-T PageType	print multi-page with following style:
			micro	- landscape, 4 sides, 132 lines
			tiny	- landscape, 3 sides, 132 lines
			small	- portrait,  2 sides, 132 lines
			normal	- landscape, 2 sides,  66 lines
			large	- portrait,  1 sides,  66 lines
			huge	- landscape, 1 sides,  33 lines
			1L	- landscape, 1 pages,  33 lines
			1	- portrait,  1 pages,  66 lines
			1l	- landscape, 1 pages,  66 lines
			2	- landscape, 2 pages,  66 lines
			2p	- portrait,  2 pages, 132 lines
			3	- landscape, 3 pages, 132 lines
			4L	- landscape, 4 pages,  33 lines
			4	- portrait,  4 pages,  66 lines
			4l	- landscape, 4 pages,  66 lines
			4p	- landscape, 4 pages, 132 lines
			5	- landscape, 5 pages, 198 lines
			6	- portrait,  6 pages,  66 lines
			6l	- landscape, 6 pages,  66 lines
			7	- landscape, 7 pages, 264 lines
			8	- portrait,  8 pages,  66 lines
			8l	- landscape, 8 pages,  66 lines
			8p	- portrait,  8 pages, 132 lines
			9	- portrait,  9 pages,  66 lines
			9l	- landscape, 9 pages,  66 lines
			10	- landscape,10 pages,  66 lines
	-t		toggle title of header, default $Header
	-f		toggle page number of footer, default $Footer
	-b		toggle frame line of border, default $Border
	-s FontSize	size of font
	-n lines	lines par page
	-w FontWidth	width of font
	-F Font=Name	Latin=Times-Roman	Latin_Bold=Times-Bold
			Arabic=Times-Roman	Arabic_Bold=Times-Bold
			Cyrillic=Times-Roman	Cyrillic_Bold=Times-Bold
			Greek=Times-Roman	Greek_Bold=Times-Bold
			Hebrew=Times-Roman	Hebrew_Bold=Times-Bold
			Icelandic=Times-Roman	Icelandic_Bold=Times-Bold
			Latin3=Times-Roman	Latin3_Bold=Times-Bold
			Latin4=Times-Roman	Latin4_Bold=Times-Bold
			Latin6=Times-Roman	Latin6_Bold=Times-Bold
			Latin8=Times-Roman	Latin8_Bold=Times-Bold
			Roman=Times-Roman	Roman_Bold=Times-Bold
			Romanian=Times-Roman	Romanian_Bold=Times-Bold
			Thai=Times-Roman	Thai_Bold=Times-Bold
			Turkish=Times-Roman	Turkish_Bold=Times-Bold
			Vietnamese=Times-Roman	Vietnamese_Bold=Times-Bold
			CNS1=Sung		CNS1_Bold=Hei
			GB1=Song		GB1_Bold=Hei
			Japan1=HeiseiMin-W3	Japan1_Bold=HeiseiKakuGo-W5
			Japan2=HeiseiMin-W3H	Japan2_Bold=HeiseiKakuGo-W5H
			Korea1=SMMyungjo-Medium	Korea1_Bold=Gothic
	-V		use vertically-oriented font
	-B		toggle permission to break lines, default $FoldLine
	-D dateformat	default $dateformat
	-H title	header title, default filename(s)
	-p pipe		pipe through to convert encoding and so on
	-g		gather files and print, default $concat
	-h		show this message
Files:		$me requires $tops_dir/ishow-*.ps resources.
Keywords:	m2ps, coco, Emacs/Mule, iconv, lv, a2ps, mpage, gs-cjk
Copyright:	(C) 2001-2003 Taiji Yamada <taiji@aihara.co.jp>
License:	This program is provided under BSD license.
Acknowledgment:	It is a pleasure to thank gs-cjk project members,
		Masatake YAMATO <masata-y@is.aist-nara.ac.jp>,
		suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>,
		Hideyuki Suzuki <hideyuki@sat.t.u-tokyo.ac.jp> and
		CHOI Junho <cjh@FreeBSD.org>.
EOF
}
pagewidth(){
  if [ "$PageScale" = 'true' ]; then
    PageWidth="\
`echo $MediaSize | sed -n -e 's/\(.*\) \(.*\)/\1/p'`\
`echo $MediaMargin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\1-\3/p'`\
`echo $Margin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\1-\3/p'`"
    PageWidth=`echo "$PageWidth" | bc`
  else
    PageWidth="\
`echo $MediaSize | sed -n -e 's/\(.*\) \(.*\)/(\1/p'`\
`echo $MediaMargin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\1-\3)/p'`\
`echo $Division | sed -n -e 's/\(.*\) \(.*\)/\/\1/p'`\
`echo $Margin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\1-\3/p'`"
    PageWidth=`echo "$PageWidth" | bc -l`
  fi
  echo $PageWidth
}
pageheight(){
  if [ "$PageScale" = 'true' ]; then
    PageHeight="\
`echo $MediaSize | sed -n -e 's/\(.*\) \(.*\)/\2/p'`\
`echo $MediaMargin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\2-\4/p'`\
`echo $Margin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\2-\4/p'`"
    PageHeight=`echo "$PageHeight" | bc`
  else
    PageHeight="\
`echo $MediaSize | sed -n -e 's/\(.*\) \(.*\)/(\2/p'`\
`echo $MediaMargin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\2-\4)/p'`\
`echo $Division | sed -n -e 's/\(.*\) \(.*\)/\/\2/p'`\
`echo $Margin | sed -n -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/-\2-\4/p'`"
    PageHeight=`echo "$PageHeight" | bc -l`
  fi
  echo $PageHeight
}
fontsize2nlines(){
  [ "$Landscape" = 'false' ] && Height=`pageheight` || Height=`pagewidth`
  echo "($Height)/$FontSize" | bc
}
nlines2fontsize(){
  [ "$Landscape" = 'false' ] && Height=`pageheight` || Height=`pagewidth`
  printf "%g" `echo "($Height)/$NLines" | bc -l`
}
while getopts 12489BbcCD:d:F:fgH:hI:LlM:m:n:P:p:S:s:T:tVw: c; do
  case $c in
  I) Encoding="$OPTARG"
    setlocalcat
    detectaffairs
    ;;
  P) Media="$OPTARG"
    case "$Media" in
    A4)     MediaSize='595 842' ;;
    A5)     MediaSize='421 595' ;;
    A6)     MediaSize='297 421' ;;
    A7)     MediaSize='210 297' ;;
    A8)     MediaSize='148 210' ;;
    A9)     MediaSize='105 148' ;;
    A10)    MediaSize='74 105'  ;;
    B5)     MediaSize='501 709' ;;
    Letter) MediaSize='612 792' ;;
    11x17)  MediaSize='792 1224';;
    Ledger) MediaSize='1224 792';;
    Legal)  MediaSize='612 1008';;
    esac
    ;;
  S) MediaSize=`echo "$OPTARG" | sed -e 's/,/ /g'`;;
  M) MediaMargin=`echo "$OPTARG" | sed -e 's/,/ /g'`;;
  m) Margin=`echo "$OPTARG" | sed -e 's/,/ /g'`;;
  l) Landscape='true'
    [ "$Stretch" != '' ] && Stretch=" `echo $Stretch | sed 's/mul/div/'`";;
  L) Landscale='true';;
  c) EvenScale='false';;
  C) PageScale='false';;
  V) VertText='true';;
  B) [ "$FoldLine" = 'true' ] && FoldLine='false' || FoldLine='true';;
  d) Division=`echo "$OPTARG" | sed -e 's/,/ /g'`;;
  1) Landscale='false'; Division='1 1';;
  2) Landscale='true' ; Division='1 2';;
  4) Landscale='false'; Division='2 2';;
  8) Landscale='true' ; Division='2 4';;
  9) Landscale='false'; Division='3 3';;
  T)
    case "$OPTARG" in
    micro)  EvenScale='false'; Landscale='true' ; Division='1 4'; NLines='132'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize"
      [ "$Landscape" != 'true' ] && Stretch=" 2 mul" || Stretch=" 2 div";;
    tiny)   EvenScale='false'; Landscale='true' ; Division='1 3'; NLines='132'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize"
      [ "$Landscape" != 'true' ] && Stretch=" 1.5 mul" || Stretch=" 1.5 div";;
    small)  EvenScale='false'; Landscale='false'; Division='2 1'; NLines='132'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize"
      [ "$Landscape" != 'true' ] && Stretch=" 2 mul" || Stretch=" 2 div";;
    normal) EvenScale='false'; Landscale='true' ; Division='1 2'; NLines='66'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize"
      Stretch="";;
    large)  EvenScale='false'; Landscale='false'; Division='1 1'; NLines='66'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize"
      Stretch="";;
    huge)   EvenScale='false'; Landscale='true' ; Division='1 1'; NLines='33'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize"
      [ "$Landscape" != 'true' ] && Stretch=" .5 mul" || Stretch=" .5 div";;
    1L) PageScale='false'; Division='1 1'; NLines='33';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='20 20 20 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    1)  PageScale='false'; Division='1 1'; NLines='66';  Landscape='false';
      MediaMargin='30 30 30 30'; Margin='20 28 20 28'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    1l) PageScale='false'; Division='1 1'; NLines='66';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='20 20 20 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    2)  PageScale='false'; Division='1 2'; NLines='66';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='20 20 20 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    2p) PageScale='false'; Division='2 1'; NLines='132'; Landscape='false';
      MediaMargin='30 30 30 30'; Margin='20 28 20 28'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    3)  PageScale='false'; Division='1 3'; NLines='132'; Landscape='true';
      MediaMargin='30 30 30 30'; Margin='20 20 20 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    4L) PageScale='false'; Division='2 2'; NLines='33';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='10 20 10 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    4)  PageScale='false'; Division='2 2'; NLines='66';  Landscape='false';
      MediaMargin='30 30 30 30'; Margin='20 31 20 30'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    4l) PageScale='false'; Division='2 2'; NLines='66';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='18.5 20 18 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    4p) PageScale='false'; Division='1 4'; NLines='132'; Landscape='true';
      MediaMargin='30 30 30 30'; Margin='20  5 20  5'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    5)  PageScale='false'; Division='1 5'; NLines='198'; Landscape='true';
      MediaMargin='30 30 30 30'; Margin='20  5 20  5'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    6)  PageScale='false'; Division='2 3'; NLines='66';  Landscape='false';
      MediaMargin='30 30 30 30'; Margin='20 15 20 15'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    6l) PageScale='false'; Division='2 3'; NLines='66';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='18.5 20 18 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    7)  PageScale='false'; Division='1 7'; NLines='264'; Landscape='true';
      MediaMargin='30 30 30 30'; Margin='20  5 20  5'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    8)  PageScale='false'; Division='2 4'; NLines='66';  Landscape='false';
      MediaMargin='30 30 30 30'; Margin='20 15.5 20 15'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    8l) PageScale='false'; Division='2 4'; NLines='66';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='18.5 20 18 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    8p) PageScale='false'; Division='4 2'; NLines='132'; Landscape='false';
      MediaMargin='15 15 15 15'; Margin=' 5  5  5  5'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    9)  PageScale='false'; Division='3 3'; NLines='66';  Landscape='false';
      MediaMargin='15 15 15 15'; Margin='20 20 20 20'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    9l) PageScale='false'; Division='3 3'; NLines='66';  Landscape='true';
      MediaMargin='15 15 15 15'; Margin='12 12 12 12'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    10) PageScale='false'; Division='2 5'; NLines='66';  Landscape='true';
      MediaMargin='30 30 30 30'; Margin='18.5  5 18  5'
      FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
    esac
    ;;
  t) [ "$Header" = 'true' ] && Header='false' || Header='true';;
  f) [ "$Footer" = 'true' ] && Footer='false' || Footer='true';;
  b) [ "$Border" = 'true' ] && Border='false' || Border='true';;
  s) FontSize="$OPTARG"; FontWidth="$FontSize";;
  n) NLines="$OPTARG"; FontSize=`nlines2fontsize`; FontWidth="$FontSize";;
  w) FontWidth="$OPTARG";;
  F)
    case "$OPTARG" in
    Latin*=*|Arabic=*|Cyrillic=*|Greek=*|Hebrew=*|Icelandic=*|Roman=*|Romanian=*|Thai=*|Turkish=*|Vietnamese=*|CNS*=*|GB*=*|Japan*=*|Korea*=*) eval "$OPTARG";;
    esac
    ;;
  D) dateformat="$OPTARG";;
  H) usertitle="$OPTARG";;
  p) localcat="$OPTARG";;
  g) [ "$concat" = 'yes' ] && concat='no' || concat='yes';;
  h) usage; exit 0;;
  esac
done
shift `expr $OPTIND - 1`
ishow="$tops_dir/ishow-${Encoding}.ps"
if [ ! -f "$ishow" ]; then
  echo "$me: invalid Encoding: $Encoding" 1>&2
  exit 1
fi

[ "$Landscape" = "$Landscale" ] && Orient='Portrait' || Orient='Landscape'
if [ "$VertText" = 'true' ]; then
  [ "$Orient" = 'Landscape' ] && Orient='Portrait' || Orient='Landscape'
fi
Nlines=`fontsize2nlines`
setinfo "$@"

title_orig="%%Title: .*"
title="%%Title: $basenames"
head_orig="/head (stdin) def"
head="/head ($timestamp [1m$basename[22m) def"
media_orig="A4"
media="$Media"
mediasize_orig="595 842"
mediasize="$MediaSize"
mediamargin_orig="/mediamargin \[ 25 29 25 29 \] def"
mediamargin="/mediamargin [ $MediaMargin ] def"
margin_orig="/margin \[ 25 29 25 29 \] def"
margin="/margin [ $Margin ] def"
orient_orig="%%Orientation: Portrait"
orient="%%Orientation: $Orient"
landscape_orig="/landscape false def"
landscape="/landscape $Landscape def"
landscale_orig="/landscale false def"
landscale="/landscale $Landscale def"
evenscale_orig="/evenscale true def"
evenscale="/evenscale $EvenScale def"
pagescale_orig="/pagescale true def"
pagescale="/pagescale $PageScale def"
verttext_orig="/verttext false def"
verttext="/verttext $VertText def"
foldline_orig="/foldline false def"
foldline="/foldline $FoldLine def"
division_orig="/division \[ 2 2 \] def"
division="/division [ $Division ] def"
header_orig="/header true def"
header="/header $Header def"
footer_orig="/footer true def"
footer="/footer $Footer def"
border_orig="/border true def"
border="/border $Border def"
fontsize_orig="/fontsize 11 def"
fontsize="/fontsize $FontSize def"
fontwidth_orig="/fontwidth 11 def"
fontwidth="/fontwidth $FontWidth$Stretch def"
# fonts for single-byte encodings
latin_orig="/Courier"
latin="/$Latin"
arabic_orig="/CourierNewArabic"
arabic="/$Arabic"
cyrillic_orig="/CourierNewCyrillic"
cyrillic="/$Cyrillic"
greek_orig="/CourierNewGreek"
greek="/$Greek"
hebrew_orig="/CourierNewHebrew"
hebrew="/$Hebrew"
icelandic_orig="/CourierNewIcelandic"
icelandic="/$Icelandic"
latin3_orig="/CourierNewLatin3"
latin3="/$Latin3"
latin4_orig="/CourierNewLatin4"
latin4="/$Latin4"
latin6_orig="/CourierNewLatin6"
latin6="/$Latin6"
latin8_orig="/CourierNewLatin8"
latin8="/$Latin8"
roman_orig="/CourierNewRoman"
roman="/$Roman"
romanian_orig="/CourierNewRomanian"
romanian="/$Romanian"
thai_orig="/CourierNewThai"
thai="/$Thai"
turkish_orig="/CourierNewTurkish"
turkish="/$Turkish"
vietnamese_orig="/CourierNewVietnamese"
vietnamese="/$Vietnamese"
# fonts for multi-byte encodings
cns1_orig="/LiSungLight"
cns1="/$CNS1"
gb1_orig="/SIL-FangSong-"
gb1="/$GB1"
japan1_orig="/KozMin-Regular"
japan1="/$Japan1"
japan2_orig="/HeiseiMin-W3H"
japan2="/$Japan2"
korea1_orig="/SMMyungjo-Medium"
korea1="/$Korea1"
# bold fonts for single-byte encodings
latin_bold_orig="/Courier-Bold"
latin_bold="/$Latin_Bold"
arabic_bold_orig="/CourierNewArabic-Bold"
arabic_bold="/$Arabic_Bold"
cyrillic_bold_orig="/CourierNewCyrillic-Bold"
cyrillic_bold="/$Cyrillic_Bold"
greek_bold_orig="/CourierNewGreek-Bold"
greek_bold="/$Greek_Bold"
hebrew_bold_orig="/CourierNewHebrew-Bold"
hebrew_bold="/$Hebrew_Bold"
icelandic_bold_orig="/CourierNewIcelandic-Bold"
icelandic_bold="/$Icelandic_Bold"
latin3_bold_orig="/CourierNewLatin3-Bold"
latin3_bold="/$Latin3_Bold"
latin4_bold_orig="/CourierNewLatin4-Bold"
latin4_bold="/$Latin4_Bold"
latin6_bold_orig="/CourierNewLatin6-Bold"
latin6_bold="/$Latin6_Bold"
latin8_bold_orig="/CourierNewLatin8-Bold"
latin8_bold="/$Latin8_Bold"
roman_bold_orig="/CourierNewRoman-Bold"
roman_bold="/$Roman_Bold"
romanian_bold_orig="/CourierNewRomanian-Bold"
romanian_bold="/$Romanian_Bold"
thai_bold_orig="/CourierNewThai-Bold"
thai_bold="/$Thai_Bold"
turkish_bold_orig="/CourierNewTurkish-Bold"
turkish_bold="/$Turkish_Bold"
vietnamese_bold_orig="/CourierNewVietnamese-Bold"
vietnamese_bold="/$Vietnamese_Bold"
# bold fonts for multi-byte encodings
cns1_bold_orig="/LiGothicMed"
cns1_bold="/$CNS1_Bold"
gb1_bold_orig="/SIL-Hei-Med-Jian"
gb1_bold="/$GB1_Bold"
japan1_bold_orig="/KozMin-Bold"
japan1_bold="/$Japan1_Bold"
japan2_bold_orig="/HeiseiKakuGo-W5H"
japan2_bold="/$Japan2_Bold"
korea1_bold_orig="/SMGothic-Bold"
korea1_bold="/$Korea1_Bold"
gssafe_orig="/gssafe true def"
gssafe="/gssafe $GsSafe def"
maxbufsize_orig="65535"
maxbufsize="65535"

prolog="
s|$orient_orig|$orient|;
s|$title_orig|$title|;
s|$head_orig|$head|;
s|$media_orig|$media|;
s|$mediasize_orig|$mediasize|;
s|$mediamargin_orig|$mediamargin|;
s|$margin_orig|$margin|;
s|$landscape_orig|$landscape|;
s|$landscale_orig|$landscale|;
s|$evenscale_orig|$evenscale|;
s|$pagescale_orig|$pagescale|;
s|$verttext_orig|$verttext|;
s|$foldline_orig|$foldline|;
s|$division_orig|$division|;
s|$header_orig|$header|;
s|$footer_orig|$footer|;
s|$border_orig|$border|;
s|$fontsize_orig|$fontsize|;
s|$fontwidth_orig|$fontwidth|;
s|$latin_bold_orig|$latin_bold|g;
s|$arabic_bold_orig|$arabic_bold|g;
s|$cyrillic_bold_orig|$cyrillic_bold|g;
s|$greek_bold_orig|$greek_bold|g;
s|$hebrew_bold_orig|$hebrew_bold|g;
s|$icelandic_bold_orig|$icelandic_bold|g;
s|$latin3_bold_orig|$latin3_bold|g;
s|$latin4_bold_orig|$latin4_bold|g;
s|$latin6_bold_orig|$latin6_bold|g;
s|$latin8_bold_orig|$latin8_bold|g;
s|$romanian_bold_orig|$romanian_bold|g;
s|$roman_bold_orig|$roman_bold|g;
s|$thai_bold_orig|$thai_bold|g;
s|$turkish_bold_orig|$turkish_bold|g;
s|$vietnamese_bold_orig|$vietnamese_bold|g;
s|$cns1_bold_orig|$cns1_bold|g;
s|$gb1_bold_orig|$gb1_bold|g;
s|$japan1_bold_orig|$japan1_bold|g;
s|$japan2_bold_orig|$japan2_bold|g;
s|$korea1_bold_orig|$korea1_bold|g;
s|$latin_orig|$latin|g;
s|$arabic_orig|$arabic|g;
s|$cyrillic_orig|$cyrillic|g;
s|$greek_orig|$greek|g;
s|$hebrew_orig|$hebrew|g;
s|$icelandic_orig|$icelandic|g;
s|$latin3_orig|$latin3|g;
s|$latin4_orig|$latin4|g;
s|$latin6_orig|$latin6|g;
s|$latin8_orig|$latin8|g;
s|$romanian_orig|$romanian|g;
s|$roman_orig|$roman|g;
s|$thai_orig|$thai|g;
s|$turkish_orig|$turkish|g;
s|$vietnamese_orig|$vietnamese|g;
s|$cns1_orig|$cns1|g;
s|$gb1_orig|$gb1|g;
s|$japan1_orig|$japan1|g;
s|$japan2_orig|$japan2|g;
s|$korea1_orig|$korea1|g;
s|$gssafe_orig|$gssafe|;
s|$maxbufsize_orig|$maxbufsize|"

ESC=""
del_bs="s/..//g;s/.//g"
iso6429="
s/\(\(__..\)\(__..\)*\)/$ESC[4m\1$ESC[24m/g;
s/__//g;
s/\(\(....\)\(....\)*\)/$ESC[1m\1$ESC[22m/g;
s/..//g;
s/\(\(_.\)\(_.\)*\)/$ESC[4m\1$ESC[24m/g;
s/_//g;
s/\(\(..\)\(..\)*\)/$ESC[1m\1$ESC[22m/g;
s/.//g"

SO=""; SI=""
[ "$Encoding" = 'iso-2022-cn' ] && ctext="s/$SO/$ESC\$A/g;s/$SI/$ESC(B/g;s/$ESC\$)A//g"
[ "$Encoding" = 'iso-2022-kr' ] && ctext="s/$SO/$ESC\$(C/g;s/$SI/$ESC(B/g;s/$ESC\$)C//g"

escps='s/\\/\\\\/g;s/(/\\(/g;s/)/\\)/g'
macdos2='BEGIN{RS="\\r|\\n|(\\r\\n)"}{print $0}'

cattops(){
  if [ "$hexmode" != 'yes' ]; then
    sed -e "$prolog" $ishow
    while [ "$1" != "" ]; do
      echo "$ESC[4m$ESC[1m`basename $1`$ESC[22m$ESC[24m"
      $localcat $1 | sed $sedflags -e "$iso6429;$ctext"
      shift
    done
  else
    sed -e "$prolog" -e "s/\({.*} shown\)-readlines/\1-readhexstrings/" $ishow
    while [ "$1" != "" ]; do
      echo "$ESC[4m$ESC[1m`basename $1`$ESC[22m$ESC[24m" | od $odflags | sed -e 's/^[0-9]* *//;s/ *//g'
      $localcat $1 | sed $sedflags -e "$iso6429;$ctext" | od $odflags | sed -e 's/^[0-9]* *//;s/ *//g'
      shift
    done
  fi
}

moretops(){
  sed -e "$prolog" -e "s/\({.*} shown\)-readlines/\1-init/" $ishow
  while [ "$1" != "" ]; do
    if [ "$hexmode" != 'yes' ]; then
      $localcat $1 | sed $sedflags -e "$iso6429;$ctext;$escps" | gawk "$macdos2" | sed -e 's/^\(.*\)$/(\1) shown/'
    else
      $localcat $1 | sed $sedflags -e "$iso6429;$ctext" | gawk "$macdos2" | perl -e 'while(<>){chomp;$_=~s/(.)/sprintf("%02x",unpack("C",$1))/ge;print"<$_> shown\n"}'
    fi
    shift
    [ "$1" != "" ] && basename=`basename $1` && $localcat <<EOF
() shown
/head ($timestamp $ESC[1m$basename$ESC[22m) def
EOF
  done
  $localcat <<EOF
shown-close
%%Trailer
%%EOF
EOF
}

#case "$Encoding" in utf16-*) hexmode='yes';; esac

case $# in
0)
  if [ "$hexmode" != 'yes' ]; then
    sed -e "$prolog" $ishow
    $localcat | sed $sedflags -e "$iso6429;$ctext"
  else
    sed -e "$prolog" -e "s/\({.*} shown\)-readlines/\1-readhexstrings/" $ishow
    $localcat | sed $sedflags -e "$iso6429;$ctext" | od $odflags | sed -e 's/^[0-9]* *//;s/ *//g'
  fi
  ;;
1)
  if [ "$hexmode" != 'yes' ]; then
    sed -e "$prolog" $ishow
    $localcat "$@" | sed $sedflags -e "$iso6429;$ctext"
  else
    sed -e "$prolog" -e "s/\({.*} shown\)-readlines/\1-readhexstrings/" $ishow
    $localcat "$@" | sed $sedflags -e "$iso6429;$ctext" | od $odflags | sed -e 's/^[0-9]* *//;s/ *//g'
  fi
  ;;
*)
  #case "$Encoding" in iso-2022-*) concat='yes';; esac
  case "$Encoding" in iso-2022-*) hexmode='yes';; esac
  if [ "$concat" = 'yes' ]; then
    cattops "$@"
  else
    moretops "$@"
  fi
;;
esac
