#!/bin/sh
#

# Default DPI and MAG to use
DPI=600
MAG=0

# Paths of Chinese fonts and tmp PK fonts
###CCPKPATH=/usr/local/lib/texmf/fonts/cct/pk
###CCHZPATH=/usr/local/lib/texmf/fonts/cct/hzfonts
TEXPKS=/afs/sipb.mit.edu/project/zhongwen/lib/fonts/cct/pk:/afs/athena.mit.edu/astaff/project/tex/lib/tex/fonts/pk:/tmp/.$USER.ccdvi.$$
CCPKPATH=/afs/sipb.mit.edu/project/zhongwen/lib/fonts/cct/pk
CCHZPATH=/afs/sipb.mit.edu/project/zhongwen/lib/fonts/cct/hzfonts
# Export them for "patchdvi"
export CCPKPATH CCHZPATH TEXPKS

#
# retrieve options for xdvi and the dvi filename (last argument)
f=`echo $* | awk '{ print $NF }'`
opt=`echo $* | awk '{ for (i = 1; i <NF; ++i)  print $i," " }'`

if [ "$f" = "" ]; then
    echo "Usage: dvipsc [dvips options] file[.dvi]"
    exit
fi

#
# scan options to retrieve DPI from "-D ###" option, MAG from "-x" option
set -- $opt ""
opt=""
while test $# -ne 0
do
    case $1 in
	-D)	shift 1; DPI=$1;;
	-x)	shift 1; MAG=$1;;
	-y)	shift 1; echo The "\"-y\" option is not allowed!"; exit;;
	*)	opt="$opt $1";;
    esac
    shift 1;
done

opt="$opt -D $DPI"
if [ $MAG != "0" ]; then
  opt="$opt -x $MAG"
fi

#
# strip possible .dvi extension from dvi file name
####f=`echo $f | awk '{ a=$1; gsub("\.dvi$", "", a); print a }'`
f=`echo $f | sed -e 's/\.dvi$//g'`

#
# strip path from the dvi file name to construct tmp dvi file.
tmp=`echo $f | sed -e 's/\// /g' | awk ' { print $NF} '`_tmp

if patchdvi -r$DPI -m$MAG -b $f ${tmp}; then
    if dvips $opt ${tmp}; then
	echo mv ${tmp}.ps $f.ps
	mv ${tmp}.ps $f.ps
    fi
else
    echo Abort!
fi

rm -f ${tmp}.dvi ${CCPKPATH}/`whoami`-${tmp}*.888pk >/dev/null 2>&1
