#!/bin/sh

generate_header() {
    cat << endline
%!PS
%
% ------------------  header from hps filter -----------------
%
%   This is the header from 'hps' filter to print hangul
%   From end of this header, real postscript output begins.
%  
%   (c) Copyright by Oh SungKyu  1993.11.23
%   

/option {
    exch
    dup (-fn) eq {
	1 index /HangulFontName exch def
    } if
    dup (-sw) eq {
	1 index /HangulScaleWidth exch def
    } if
    dup (-sh) eq {
	1 index /HangulScaleHeight exch def
    } if
    dup (-sz) eq {
	1 index /HangulSizeToAscii exch def
    } if
    pop pop
} def

% include the file which has routines for hangul font
(hps.ps) run

endline
}


parseoptions () {
    while [ -n "$1" ]
    do
	case $1 in
	    -fn)
		echo "(-fn) /"$2" option" ;;
	    -sw)
		echo "(-sw) "$2" option" ;;
	    -sh)
		echo "(-sh) "$2" option" ;;
	    -sz)
		echo "(-sz) "$2" option" ;;
	esac
	shift
    done
}

help () {
cat << endofhelp 1>&2
______________________________________________________________________________
     hps :  Hangul Postscript Filter 				1993.11.23
------------------------------------------------------------------------------

   usage : hps [options] file    : print file
           hps [options] [-]     : print standar input
           hps -help             : show this help message

 options : -fn fontname           : use fontname as hangul font
           -sz value              : Size of a hangul letter in ASCII letter
           -sw percentage         : Scaling factor for Width of a hangul letter
           -sh percentage         : Scaling factor for Height of a hangul letter

 default : -fn Kaist-Myeongjo -sz 2 -sw 100 -sh 100

avaiable fonts
         : Kaist-Myeongjo, Kaist-Gothic, Kaist-Geulssi, Kaist-Dinaru, 
	   Kaist-Myeongjo-Bold
	
  Author : Oh SungKyu (member of SPARCS)
           Computer Architecture Lab. of KAIST
  E-Mail : hanmaum@camars.kaist.ac.kr or hanmaum@baram.kaist.ac.kr
           hanmaum in ARA BBS (bbs of ara.kaist.ac.kr)
endofhelp
exit
}

parsecmd () {
    while [ -n "$1" ]
    do
	case $1 in
	    -fn|-sw|-sh|-sz)
		if [ -n "$2" ]; then
		    validoptions="$validoptions $1 $2 "
		    shift 2
		else 
		    help
		fi
		;;
	     -*)
		help
		;;
	     *)
		filename=$1
		shift
		;;
	esac
    done
}

validoptions=""
filename=""

/usr/bin/tty > /dev/null
if [ $? -eq 0 ]; then
    help
fi


# parse given command line arguments
parsecmd -fn Kaist-Myeongjo -sz 2 -sw 100 -sh 100 $*

# generate header for following options
generate_header

echo "% -------------------  start of options --------------------"

# parse options
parseoptions $validoptions

echo "% -------------------  start of output ---------------------"

# append filename
cat $filename

echo "% --------------------  end of output ----------------------"
