#!/bin/sh
#

# Default DPI to use
DPI=600

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

#
# 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: xdvic [xdvi options] file[.dvi]'
    exit
fi

#
# scan options to retrieve DPI from "-p ###" option.
set -- $opt ""
opt=""
while test $# -ne 0
do
    case $1 in
	-p)	shift 1; DPI=$1;;
	*)	opt="$opt $1";;
    esac
    shift 1;
done

opt="$opt -p $DPI"

#
# 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 -b $f ${tmp}; then
    xdvi $opt ${tmp}
else
    echo Abort!
fi

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