# ex:ts=4
#	Modified from CXterm.sh included with the cxterm Chinese lang xterm pkg

# ENCODE_FONTS:		automaticially search for a font from the list

#Files:  "vn-r14 vn10x20 vn9x15 vnlutRS18"
VN_FONTS="vn-r14 vn10x20 viet   vn-lucidasanstypewriter-18"

if [ "$BINDIR" != "" ]; then
	VNTERM="$BINDIR/vnterm"
else
	VNTERM=vnterm
fi

check_use_font ()
{
	for f in "$@" ; do
	if [ "`xlsfonts $f 2>/dev/null`" = "$f" ]; then
		echo $f
		return 0
	fi
	done
	xset fp+ "$XFONTDIR"
	for f in "$@" ; do
	if [ "`xlsfonts $f 2>/dev/null`" = "$f" ]; then
		echo $f
		return 0
	fi
	done
	return 1
}


VF=""
FN=0
for i in "$@" ; do 
	if [ "$i" = "-fn" ]; then
		FN=1;  continue;
	fi
	if [ "$FN" -eq 1 ]; then
		VF=`check_use_font $i`
		if [ "$VF" = "" ]; then
			echo "Specified font doesn't exist in the X Window system."
			exit 1
		fi
		break
    fi
	FN=0
done


if [ "$VF" = "" ]; then
	VF=`check_use_font $VN_FONTS`
	if [ "$VF" = "" ]; then
		echo "Cannot identify a Vietnamese font in the X Window system."
		exit 1
	fi
	FN_OPT="-fn $VF"
fi

XENVIRONMENT=$VNTERM_AD
export XENVIRONMENT
exec $VNTERM $FN_OPT $*
