#!/bin/sh
#
# by Constantine Gavrilov <const-g@xpert.com>
#

usage()
{
 echo 'Use as $0 [-st style] [file]' >&2
 exit 1
}

if [ -e /etc/profile.d/cyrprint.sh ]; then
 source /etc/profile.d/cyrprint.sh
fi

if [ "$PRDIR" = "" ]; then
 PRDIR=/usr/lib/X11/fonts/cyr/Type1
fi

if [ "$MOZPRINTHELP" = "" ]; then
 MOZPRINTHELP=mozprinthelp
fi

FILE=""
STYLE="st0n"
[ `basename $0` = "cyrmozprint" ] && STYLE="st0m"

##do some saniry checks####
[ $# -gt 3 ] && usage

( [ $# -gt 1 ] && [ "$1" != "-st" ] && [ "$2" != "-st" ] ) && usage
##end of sanity checks

##parse args
if [ "$1" = "-st" ]; then
  STYLE=$2
  [ $# -eq 3 ] && FILE=$3
elif [ "$2" = "-st" ]; then
   STYLE=$3
   FILE=$1
else
  [ $# -eq 1 ] && FILE=$1
fi
##end parse args

if [ $FILE != "" ] && [ ! -e $FILE ]; then
 echo Cannot open $FILE >&2
 exit 2
fi

STYLE="$PRDIR/$STYLE"
if [ ! -e $STYLE ]; then
 echo Cannot open $STYLE >&2
 exit 2
fi

##parse through print style
font1=`grep "font1=" $STYLE | cut -f2 -d "="`
font2=`grep "font2=" $STYLE | cut -f2 -d "="`
font3=`grep "font3=" $STYLE | cut -f2 -d "="`
font4=`grep "font4=" $STYLE | cut -f2 -d "="`
font5=`grep "font5=" $STYLE | cut -f2 -d "="`
font6=`grep "font6=" $STYLE | cut -f2 -d "="`
font7=`grep "font7=" $STYLE | cut -f2 -d "="`
font8=`grep "font8=" $STYLE | cut -f2 -d "="`
font9=`grep "font9=" $STYLE | cut -f2 -d "="`
font10=`grep "font10=" $STYLE | cut -f2 -d "="`
font11=`grep "font11=" $STYLE | cut -f2 -d "="`
font12=`grep "font12=" $STYLE | cut -f2 -d "="`
FONTLIST=`grep "fontlist=" $STYLE | cut -f2 -d "="`
###end parse through print style

##check for fonts
for i in $FONTLIST
do
 if [ ! -f $PRDIR/$i ]; then
  echo "Can't find font $PRDIR/$i" >&2
  exit 3
 fi
done
##end check for fonts

#choose tmp file names
if [ -x /bin/mktemp ] || [ -x /usr/bin/mktemp ]; then
 SEDFILE=`mktemp -q /tmp/.cyrprint-sed.XXXXXX`
 TMPFILE=`mktemp -q /tmp/.cyrprint-tmp.XXXXXX`
fi
[ "$SEDFILE" = "" ] &&  SEDFILE=/tmp/.cyrprint-sed.$$
[ "$TMPFILE" = "" ] && TMPFILE=/tmp/.cyrprint-tmp.$$
#end choose tmp file names

##generate sed script
{
[ "$font1" != "" ]  && echo           '1,$s/\(Times-Roman\)\(\)\( findfont\)/'"$font1"'\3/'
[ "$font2" != "" ]  && echo            '1,$s/\(Times-Bold\)\(\)\( findfont\)/'"$font2"'\3/'
[ "$font3" != "" ]  && echo          '1,$s/\(Times-Italic\)\(\)\( findfont\)/'"$font3"'\3/'
[ "$font4" != "" ]  && echo      '1,$s/\(Times-BoldItalic\)\(\)\( findfont\)/'"$font4"'\3/'
[ "$font5" != "" ]  && echo               '1,$s/\(Courier\)\(\)\( findfont\)/'"$font5"'\3/'
[ "$font6" != "" ]  && echo          '1,$s/\(Courier-Bold\)\(\)\( findfont\)/'"$font6"'\3/'
[ "$font7" != "" ]  && echo       '1,$s/\(Courier-Oblique\)\(\)\( findfont\)/'"$font7"'\3/'
[ "$font8" != "" ]  && echo   '1,$s/\(Courier-BoldOblique\)\(\)\( findfont\)/'"$font8"'\3/'
[ "$font9" != "" ]  && echo             '1,$s/\(Helvetica\)\(\)\( findfont\)/'"$font9"'\3/'
[ "$font10" != "" ] && echo        '1,$s/\(Helvetica-Bold\)\(\)\( findfont\)/'"$font10"'\3/'
[ "$font11" != "" ] && echo     '1,$s/\(Helvetica_Oblique\)\(\)\( findfont\)/'"$font11"'\3/'
[ "$font12" != "" ] && echo '1,$s/\(Helvetica-BoldOblique\)\(\)\( findfont\)/'"$font12"'\3/' } > $SEDFILE
##end generate sed script

##cat font files
for i in $FONTLIST
do
 echo '%!PS-Adobe-3.0' >> $TMPFILE
 echo '%%BeginResource: fonts' >> $TMPFILE
 cat $PRDIR/$i >> $TMPFILE
 echo '%%EndResource: fonts' >> $TMPFILE
done

if [ `basename $0` = "cyrmozprint" ]; then
  cat $FILE | $MOZPRINTHELP KOI8-R | sed -e 's/ unicodeshow/ show/' | sed -f $SEDFILE >> $TMPFILE
else
  cat $FILE | sed -f $SEDFILE >> $TMPFILE
fi

cat $TMPFILE
rm -f $TMPFILE
rm -f $SEDFILE
