#!/usr/bin/env bash
# LFS comes with bash, which allows a simple 'which'
# Configure my TeX tests, according to what is available.
# FreeBSD-10.1's shell feels more like csh than POSIX, but
# after installing a desktop and texlive, I found that
# bash had been installed in /usr/local, so try that.
# Copyright © 2014-2016 Ken Moffat <ken@linuxfromscratch.org>
# licensed under the GNU GPL v2 License:
# https://gnu.org/licenses/old-licenses/gpl-2.0.html
# Best viewed in a term 100+ columns wide

VERSION=$(cat ./VERSION)
MAXEXTRA=2 # tests NOT run automatically
MAXTESTS=23 # for reporting how many can be run, FIXME increase if adding more tests
# above do not include any w-i-p items which have not been wired in.
# for testing on a fully-provisioned system, --without-something
# will be useful to check the logic.
TESTALLPDFLATEX=yes
TESTASY=yes
TESTBIBER=yes
TESTBIBTEX=yes
TESTBIBXEL=yes
TESTBIDIPOEM=yes
TESTCYL=yes
TESTDVIPDFMX=yes
TESTDVISVGM=yes
TESTJALUA=yes
# TESTLATEX would make no sense, it is only a dep for
# the svg tests and is checked for if dvisvgm is tested.
TESTLUALATEX=yes
TESTLIG=yes
TESTMAKEINDEX=yes
TESTOLDJAP=yes
TESTPDFLATEX=yes
TESTPERIODICSVG=yes
TESTPERIODICTAB=yes
TESTRUBY=yes
TESTTIKZ=yes
TESTTRI=yes
TESTUPLATEX=yes
TESTXELATEX=yes
TESTXINDY=yes
TESTFONTS=yes
TESTCHINESE=yes
TESTJAPANESE=yes
TESTKOREAN=yes

BADPARM=

MAINFONT=
POEMFONT=
CFONT=
JFONT=
KFONT=
PREFER=japanese # it has to default to something.

check_omit_parm () {
	PARM=$(echo $1 | sed 's/--without-//')
	if [ "$PARM" = "allpdflatex" ]; then
		TESTALLPDFLATEX=
	elif [ "$PARM" = "asy" ]; then
		TESTASY=
	elif [ "$PARM" = "biber" ]; then
		TESTBIBER=
	elif [ "$PARM" = "bibtex" ]; then
		TESTBIBTEX=
	elif [ "$PARM" = "bibxelatex" ]; then
		TESTBIBXEL=
	elif [ "$PARM" = "bidipoem" ]; then
		TESTBIDIPOEM=
	elif [ "$PARM" = "cylinder" ]; then
		TESTCYL=
	elif [ "$PARM" = "dvipdfmx" ]; then
		TESTDVPDFMX=
	elif [ "$PARM" = "dvisvgm" ]; then
		TESTDVISVGM=
	elif [ "$PARM" = "jalua" ]; then
		TESTJALUA=
	elif [ "$PARM" = "lualatex" ]; then
		TESTLUALATEX=
	elif [ "$PARM" = "ligatures" ]; then
		TESTLIG=
	elif [ "$PARM" = "makeindex" ]; then
		TESTMAKEINDEX=
	elif [ "$PARM" = "oldjap" ]; then
		TESTOLDJAP=
	elif [ "$PARM" = "pdflatex" ]; then
		TESTPDFLATEX=
	elif [ "$PARM" = "periodicsvg" ]; then
		TESTPERIODICSVG=
	elif [ "$PARM" = "periodictab" ]; then
		TESTPERIODICTAB=
	elif [ "$PARM" = "ruby" ]; then
		TESTRUBY=
	elif [ "$PARM" = "tikz" ]; then
		TESTTIKZ=
	elif [ "$PARM" = "triangles" ]; then
		TESTTRI=
	elif [ "$PARM" = "uplatex" ]; then
		TESTUPLATEX=
	elif [ "$PARM" = "xelatex" ]; then
		TESTXELATEX=
	elif [ "$PARM" = "xindy" ]; then
		TESTXINDY=
	elif [ "$PARM" = "fonts" ]; then
		TESTFONTS=
	elif [ "$PARM" = "chinese" ]; then
		TESTCHINESE=
	elif [ "$PARM" = "japanese" ]; then
		TESTJAPANESE=
	elif [ "$PARM" = "korean" ]; then
		TESTKOREAN=
	else
		BADPARM="$BADPARM $PARM"
	fi
}

check_font_parm () {
	# allow user to force a font, at their own risk
	FONTVAL=$(echo "$1" | cut -d '=' -f 2)
	FONTTYPE=$(echo $1 | sed -e 's%^--with-%%' -e 's%-font=.*$%%')
	if [ "$FONTTYPE" = "main" ]; then
		MAINFONT="$FONTVAL"
	elif [ "$FONTTYPE" = "chinese" ]; then
		CFONT="$FONTVAL"
	elif [ "$FONTTYPE" = "japanese" ]; then
		JFONT="$FONTVAL"
	elif [ "$FONTTYPE" = "korean" ]; then
		KFONT="$FONTVAL"
	elif [ "$FONTTYPE" = "poem" ]; then
		POEMFONT=$FONTVAL
	else
		BADPARM="$BADPARM $1"
	fi
}

check_prefer () {
	LANG=$(echo "$1" | cut -d '=' -f 2)
	case $LANG in chinese|japanese)
		PREFER=$LANG
		;;
	*)
		echo "ERROR: --prefer= only accepts chinese or japanese"
		exit 1
		;;
	esac
}

sysfont () {
# $1 is the font file name (it saves trying to work out if the 'regular'
# variant is installed), Mostly, the Regular variant has Style=Regular,
#but for some of the Chinese fonts the Style is Book or Light, and
# freefont gives a multilingual string, fortunately beginning Regular
# Although WenQuanYi Zen Hei is Chinese, it also provides Korean.
#
# If file is found, return the name else return empty string
# the 2>/dev/null is because on a system using fontconfig-2.11.0 I was
# getting a message on the output for every invocation of fc-list:
# Fontconfig warning: ignoring chinese: not a valid language tag
# this used to be called with the name to use as the second parm,
# but now return that,
if [ $# -ne 1 ]; then
	echo "BAD CALL to sysfont : $*"
	exit 2
fi
if [ -n "$FCLIST" ]; then
	fc-list 2>/dev/null | grep -q $1
	if [ $? -eq 0 ]; then
		# now work out the best match (prefer Regular,
		# but Book for UKai, Light for UMing)
		FNAME=$1
		# add enough to get the simplified, or the non-monospace
		echo $1 | grep -qi ukai  && FNAME="$1.*CN"
		echo $1 | grep -qi uming && FNAME="$1.*CN"
		echo $1 | grep -qi odosung && FNAME="AR PL New Sung,"
		echo $1 | grep -qi wqy-zenhei && FNAME="WenQuanYi Zen Hei,"
		FONTWORK=$(fc-list 2>/dev/null | grep "${FNAME}.*style=Regular")
		if [ -z "$FONTWORK" ]; then
			FONTWORK=$(fc-list 2>/dev/null | grep "${FNAME}.*style=Book")
		fi
		if [ -z "$FONTWORK" ]; then
			FONTWORK=$(fc-list 2>/dev/null | grep "${FNAME}.*style=Light")
		fi
		# VL-Gothic is 'regular' with a small r
		if [ -z "$FONTWORK" ]; then
			FONTWORK=$(fc-list 2>/dev/null | grep "${FNAME}.*style=regular")
		fi
		# the sawarabi fonts are Medium
		if [ -z "$FONTWORK" ]; then
			FONTWORK=$(fc-list 2>/dev/null | grep "${FNAME}.*style=Medium")
		fi
		# bail if a font exists but was not matched to the type
		if [ -z "$FONTWORK" ]; then
			echo "Error: no suitable style found for $FNAME" >&2
		fi
		# take the part between ': ... :', up to any comma,
		# and remove the leading space
		FONTNAME=$(echo $FONTWORK | cut -d ':' -f2 | cut -d ',' -f1 \
		 | sed 's/^ *//')
		echo -n "$FONTNAME"
	else
		echo -n ''
	fi
else
	echo -n ''
fi
}

texfont () {
# for testing : ensure no texlive fonts are found		
# for fontconfig it is fairly easy to move fonts around,	
# but with tex that is not true.				
#DUMMY=true							
# if $1 is found below $TEXFONTS return it, else return empty string
if [ -z "$TEXFONTS" ] || [ -n "$DUMMY" ]; then
	echo -n ''
else
# set TEXDEBUG for debugging - it may break the seds which set font names
	if [ -n "$TEXDEBUG" ]; then
		echo "Running find $TEXFONTS -name $1 | sed 's%^.*/%%' "
		find $TEXFONTS -name $1 && echo found || echo notfound
		RESULT=$(find $TEXFONTS -name $1 | sed 's%^.*/%%')
		echo $RESULT
		echo
	fi
	echo -n $(find $TEXFONTS -name $1 | sed 's%^.*/%%')
fi
}

check_for () {
	# use kpsewhich to check if a sty file is present
	# call with name without .sty and variablename
	# on success, variable is set to 0
	# otherwise to 1
	if [ $# -ne 2 ]; then
		echo "INTERNAL ERROR: check_for called with $*"
		exit 2
	fi
	echo -n "testing for $1 ... "
	kpsewhich $1.sty >/dev/null
	if [ $? -eq 0 ]; then
		echo "yes"
		let $2=0
	else
		echo "no"
		let $2=1
	fi
}

exists () {
	# check if a program is found
	if [ $# -ne 1 ]; then
		echo "exists was called with $*"
		exit 2
	fi
	echo -n "checking for $1 ... "
	# If /bin is symlinked to /usr/bin, type -pa finds
	# three entries for a prog in /usr/bin, first two
	# are in /bin.  But if tail or head are used, this
	# breaks when a prog is missing - on my BLFS systems
	# the only such program is biber and it gets reported
	# as existing.
	EXISTQTY=$(type -pa $1 | wc -l)
	if [ $EXISTQTY -lt 2 ]; then
		type -pa $1
	else
		type -pa $1 | tail -n 1
	fi
	if [ $? -eq 0 ]; then
		return 0
	else
		echo "no"
		return 1
	fi
}

desire () {
	# test if an optional TeX program exists,
	# populate variable with its name if it does
	# report what cannot be run if it doesn't.
	# and issue a warning
	# args progname VARNAME 'fu and bar'
	if [ $# -ne 3 ]; then
		echo "desire called with $# args: $*"
		exit 2
	fi
	exists $1
	if [ $? -eq 0 ]; then
		declare -g "$2=$1"
	else
		echo "WARNING: I need $1 to run $3"
		let WARNINGS=$WARNINGS+1
	fi
}
# - - - - - - - - - - - - - - - - - - - -
# main line

echo "This is the configure script for latex-test $VERSION"

ERRORS=0
WARNINGS=0

# first check the basic system
echo "Preparing to create the LaTeX test scripts"
echo "checking prerequisites"

# system programs required for configure to run.
exists cat  || let ERRORS=$ERRORS+1
exists grep || let ERRORS=$ERRORS+1
exists make || let ERRORS=$ERRORS+1
exists rm   || let ERRORS=$ERRORS+1
exists sed  || let ERRORS=$ERRORS+1
exists tr   || let ERRORS=$ERRORS+1
exists wc   || let ERRORS=$ERRORS+1
# texlive program  required for configure to run.
exists kpsewhich || let ERRORS=$ERRORS+1

if [ $ERRORS -gt 0 ]; then
	echo "ERROR: your basic kit is not complete"
	echo "As well as shell tools, I need kpsewhich."
	exit 1
fi

# - - - - - - - - - - - - - - - - - - - -
# Now check that only ONE texlive is on $PATH
# if more than one, most tests seem to pass but
# the duplicated match_parens causes a sed to
# fail with an odd message.  The point is that
# with muiltiple texlive instances the tests
# might find things from the non-default instance,
# e.g. if the first version on $PATH is incomplete.
# Now that pdflatex is not absolutely required,
# check ALL likely programs
echo "sanity check that only one instance of texlive is on PATH"

# Unfortunately, if the /bin -> /usr/bin symlink exists, that
# shows anything in /usr/bin twice.  I think that debian installs
# now have that symlink, current LFS certainly does.  Work around
# it by looking at inode numbers.  For BLFS this applies to biber.
PRGS="asy biber bibtex dvipdfmx dvisvgm latex lualatex makeglossaries pdflatex texindy uplatex xelatex xindy"
for PROG in $PRGS ; do
	INSTANCES=$(type -pa $PROG | wc -l)
	if [ $INSTANCES -gt 1 ]; then
		FIRSTINODE=
		WHERES=$(type -pa $PROG  | tr '\n' ' ')
		for PATHEDPROG in $WHERES ; do
			THISINODE=$(ls -i $PATHEDPROG | awk '{ print $1 }')
			if [ -z "$FIRSTINODE" ]; then
				FIRSTINODE=$THISINODE
			elif [ "$FIRSTINODE" == "$THISINODE" ]; then
				# Need to do something if they match
				/bin/true
			else
				echo "ERROR: more than one instance of $PROG is present on PATH"
				echo "Please fix this to test the desired version."
				exit 2
			fi
		done
	fi
done

# - - - - - - - - - - - - - - - - - - - -
echo "checking any switches you supplied"
# if any parms were supplied, sed will be required.
while [ $# -gt 0 ]; do
	echo "\$1 is now $1"
	case $1 in	--without-*)
				check_omit_parm $1
				;;
			--with-*-font=*) # needs double quotes for spaces
				check_font_parm "$1"
				;;
			--prefer=*) # needs double quotes for spaces
				check_prefer "$1"
				;;
			*)
				BADPARM="$BADPARM $1"
				;;
	esac
	shift
done

# - - - - - - - - - - - - - - - - - - - -
# now check the optional TeX programs and any related other progs
echo "checking for optional TeX programs"

# pdflatex comes first because most of my first tests used it
if [ -n "$TESTALLPDFLATEX" ]; then
	desire pdflatex PDFLATEX "many tests (collection-latex)"
else
	echo "all tests that use pdflatex deselected by request"
fi

if [ -n "$TESTASY" ]; then
	desire asy ASY 'triangles and cylinder diagrams (collection-binextra)'
	if ! [ -z "$ASY" ]; then
		# asy uses gs at runtime, it is actually pulled in dynamically
		# by dvisvgm which, like,asy, is in collection-binextra.
		desire gs GS "triangles and cylinder diagrams, needed on system"
	fi
else
	echo "asy deselected by request"
fi

if [ -n "$TESTBIBER" ]; then
	desire biber BIBER 'several tests (collection-bibtexextra)'
	# pdftotext is only used in checking if biber worked
	desire pdftotext PDFTOTEXT 'check to confirm biber succeeded'
else
	echo "biber deselected by request"
fi

if [ -n "$TESTDVISVGM" ]; then
	desire dvisvgm DVISVGM "dvisvgm test"
	# latex command is only explicitly used in dvisvgm test
	desire latex LATEX "dvisvgm and periodicsvg tests (collection-binextra)"
else
	echo "dvisvgm deselected by request"
fi

if [ -z "$TESTLUALATEX" ]; then
	echo "lualatex deselected by request"
else
	desire luaotfload-tool LUAOTFLOADTOOL 'fontspec to find fonts for lualatex'

	if [ -z "$TESTLIG" ]; then
		echo "ligatures deselected by request"
		# otherwise, font looked for in font tests below
		# fontspec desired above, for lualatex on which this depends
	fi
fi

if [ -z "$TESTMAKEINDEX" ]; then
	echo "makeindex deselected by request"
fi

if [ -n "$TESTUPLATEX" ]; then
	desire uplatex UPLATEX "oldjap test (collection-langjapanese)"
fi

if [ -n "$TESTXINDY" ]; then
	desire xindy XINDY 'xindy, xindynonlua (collection-binextra)'
	desire makeglossaries MAKEGLOSSARIES 'xindy'
	desire pdfinfo PDFINFO 'check to confirm xindy or xindynonlua succeeded'
	desire texindy TEXINDY 'xindynonlua'
else
	echo "xindy and xindynonlua deselected by request"
fi

if [ -n "$TESTXELATEX" ]; then
	desire xelatex XELATEX 'various UTF-8 tests (collection-xetex)'
else
	echo "xelatex deselected by request"
fi

if [ -n "$TESTRUBY" ]; then
	desire ruby RUBY 'parens'
	if [ -n "$RUBY" ]; then
		desire match_parens MATCHPARENS 'parens (collection-binextra)'
	fi
else
	echo "ruby (i.e. match_parens) deselected by request"
fi

# - - - - - - - - - - - - - - - - -
# all fonts are tested in order of preference.

# check for fonts for lualatex and xelatex
if [ -z "$TESTFONTS" ]; then
	echo "All TTF and OTF fonts deselected by request"
else
	echo "Checking for useful texlive TTF and OTF fonts"
	FOUNDFONTS=
	if [ -n "$TESTLUALATEX" ] || [ -n "$TESTXELATEX" ] || [ -n  "$TESTOLDJAP" ]; then
		echo "looking to see if I can search for texlive fonts"
        # kpsepath is only installed by scheme-medium and scheme-full
		exists kpsepath && KPSEPATH=kpsepath
        exists tr && TR=tr
		if [ -z "$KPSEPATH" ] || [ -z "$TR" ]; then
			echo "I need both kpsepath and tr to find texlive OTF/TTF fonts"
		fi
		# check for texlive fonts
		if [ -n "$KPSEPATH" ] && [ -n "$TR"  ]; then
			DISTFONTS=$(kpsepath vf | tr ':' '\n' | grep 'texmf-dist/fonts' | \
			head -n 1 | sed -e 's%^!*%%' -e 's%\(fonts/\).*%\1%')
			if [ -z "$DISTFONTS" ]; then
				echo "cannot determine where texlive fonts are"
			elif [ -d "$DISTFONTS" ]; then
				# assume the fonts directory might not exist!
				TEXFONTS=$DISTFONTS
				FOUNDFONTS=yes
			else
				echo "cannot find texlive fonts : will only check fontconfig"
			fi
		fi
		echo "looking to see if I can search for system fonts"
		exists fc-list && FCLIST=fc-list
		if [ -z "$FCLIST" ]; then
			echo "no"
			echo "WARNING, fc-list not found, cannot check system fonts"
			let WARNINGS=$WARNINGS+1
		else
			FOUNDFONTS=yes
		fi
	fi
fi

if [ -n "$FOUNDFONTS" ]; then
	if [ -n "$MAINFONT" ]; then
		# font was requested, confirm it exists and convert to name
		# NB when the Name is specified (instead of the filename), do NOT use
		# sysfont.
		SOUGHTFONT="$MAINFONT"
		MAINFONT=$(sysfont "$MAINFONT")
		test -z "$MAINFONT" && MAINFONT=$(texfont "$SOUGHTFONT")
		if [ -z "$MAINFONT" ]; then
			echo "ERROR: specified main font $MAINFONT not found"
			exit 1
		else
			echo "will use $MAINFONT for main text in lualatex and xelatex"
		fi
	else
	# begin by looking for useful texlive fonts : i.e. those which contain
	# all of the latin/greek/cyrillic letters which I might use.
	# initially, just store the file names because that is how xelatex will
	# have to access them, and that also works for lualatex.
		if [ -n "$TEXFONTS" ]; then
			echo "Will search for specific fonts in $TEXFONTS, this may take some time"
			# test that texfont reports blank for non-existant font
			MAINFONT=$(texfont fubar.ttf)
			test -z "$MAINFONT" && MAINFONT=$(texfont LinLibertine_R.otf)
			test -z "$MAINFONT" && MAINFONT=$(texfont DejaVuSerif.ttf)
			test -z "$MAINFONT" && MAINFONT=$(texfont FreeSerif.otf)
			test -z "$MAINFONT" && echo "no useful texlive fonts were found"
		fi
		# There are better fonts than these, but these two are common
		# AND have adequate coverage including cyrillic, greek, vietnamese
		test -z "$MAINFONT" && MAINFONT=$(sysfont DejaVuSerif.ttf)
		test -z "$MAINFONT" && MAINFONT=$(sysfont FreeSerif.otf)
		if [ -z "$MAINFONT" ]; then
			echo "Neither DejaVu Serif nor FreeSerif were found - OTF/TTF will not be used"
			let WARNINGS=$WARNINGS+1
			TESTFONTS= # do not test for other CJK or ligatures fonts
		else
			 echo "will use $MAINFONT for non-CJK text in lualatex and xelatex"
		fi
    fi
fi

if [ -n "$TESTFONTS" ]; then
	# For ligatures, use LinLibertine but by name (to allow the bold variant)
	# This is my first choice for a main font, but user might override it, so
	# the test for the ligature font needs to be separate.
	LIGFONT=$(texfont LinLibertine_R.otf)
	if [ "$LIGFONT" = "LinLibertine_R.otf" ]; then
		LIGFONT="Linux Libertine O"
	fi
fi

# repeat for the various CJK options, if TESTFONTSset and MAINFONT found
if [ -n "$TESTFONTS" ] && [ -n "$MAINFONT" ]; then
	# For CJK, use common free fonts.
	# Note that these fonts seem to only support one of chinese, japanese,
	# korean.

	if [ -n "$CFONT" ]; then
		# font was requested, confirm it exists
		SOUGHTFONT="$CFONT"
		CFONT=$(sysfont "$CFONT")
		test -z "$CFONT" && CFONT=$(texfont "$SOUGHTFONT")
		if [ -z "$CFONT" ]; then
			echo "ERROR: specified chinese font $SOUGHTFONT not found"
			exit 1
		else
			echo "will use specified font $CFONT for chinese text"
		fi
	else
		# Use system fonts - Ming and Sung should be preferred, but Kai is also
		# good before falling back to Hei (Sans).  N.B. the old fireflysung is
		# a traditional font, odosung replaced it and covers simplified.
		# Fandol fots come from collection-langchinese, but for me they were
		# pulled in by collection-langjapanese.
		if [ -n "$TESTCHINESE" ]; then
			echo -n "Looking for a Simplified Chinese font ... "
			# Begin with Ming/Song/Sung (serif)
			test -z "$CFONT" && CFONT=$(texfont FandolSong-Regular.otf )
			test -z "$CFONT" && CFONT=$(sysfont uming.ttc )
			test -z "$CFONT" && CFONT=$(texfont gbsn00lp.ttf )
			test -z "$CFONT" && CFONT=$(sysfont gbsn00lp.ttf )
			test -z "$CFONT" && CFONT=$(sysfont odosung.ttc )
			# Now try Kai (script)
			test -z "$CFONT" && CFONT=$(sysfont ukai.ttc )
			test -z "$CFONT" && CFONT=$(texfont gkai00mp.ttf )
			test -z "$CFONT" && CFONT=$(sysfont gkai00lp.ttf )
			test -z "$CFONT" && CFONT=$(sysfont odokai.ttf )
			# Next try Hei (sans)
			test -z "$CFONT" && CFONT=$(texfont FandolHei-Regular.otf )
			test -z "$CFONT" && CFONT=$(sysfont NotoSansCJKsc-Regular.otf )
			test -z "$CFONT" && CFONT=$(sysfont odohei.ttf )
			test -z "$CFONT" && CFONT=$(sysfont wqy-zenhei.ttc )
			# Versions of Droid Sans that includes Chinese and Japanese.
			# For minimal use, oldest known version of DroidSansFallback works.
			# The FallbackFull variant adds lots of other writing systems,
			# but in 2010 dropped Hangul (for Korean) and is therefore a
			# last-ditch fallback for Chines and Japanese only.
			test -z "$CFONT" && CFONT=$(sysfont DroidSansFallback.ttf )
			test -z "$CFONT" && CFONT=$(sysfont DroidSansFallbackFull.ttf )
			if [ -n "$CFONT" ]; then
				echo "$CFONT"
			else
				echo "WARNING: None found"
				let WARNINGS=$WARNINGS+1
			fi
		else
			echo "chinese deselected"
		fi
	fi

	if [ -n "$JFONT" ]; then
		# font was requested, confirm it exists
		SOUGHTFONT="$JFONT"
		JFONT=$(sysfont "$JFONT")
		test -z "$JFONT" && JFONT=$(texfont "$SOUGHTFONT")
		if [ -z "$JFONT" ]; then
			echo "ERROR: specified japanese font $SOUGHTFONT not found"
			exit 1
		else
			echo "will use specified font $JFONT for japanese text"
		fi
	else
		if [ -n "$TESTJAPANESE" ]; then
			echo -n "Looking for a Japanese font ... "
			# first. try mincho (serif)
			# texlive includes 7 weights of HaranoajiMincho, but Bold and
			# Heavy are much darker than the chinese and korean scripts.
			#  To me, it seems that (arguably) Regular is heavier then
			# either SemiBold or Medium when viewed on screen.
			# These come from collection-langjapanese
			# After that try ipaexm which is the modern version of ipam, including
			# proportional-width letters for english text
			test -z "$JFONT" && JFONT=$(texfont HaranoAjiMincho-Regular.otf)
			test -z "$JFONT" && JFONT=$(texfont ipaexm.ttf)
			test -z "$JFONT" && JFONT=$(sysfont ipaexm.ttf )
			test -z "$JFONT" && JFONT=$(texfont ipam.ttf)
			test -z "$JFONT" && JFONT=$(sysfont ipam.ttf)
			# continue with other known mincho fonts
			test -z "$JFONT" && JFONT=$(sysfont HanaMinA.ttf )
			test -z "$JFONT" && JFONT=$(sysfont kochi-mincho-subst.ttf )
			# for non-obvious reasons, lualatex (tl2015) cannot find Sawarabi Moncho
			# although xelatex is fine with it.
			#test -z "$JFONT" && JFONT=$(sysfont sawarabi-mincho-medium.ttf )
			# then try gothic (sans)
			# again, HaranoAji first,  try ipaex before ipa
			test -z "$JFONT" && JFONT=$(texfont HaranoAjiGothic-Regular.otf)
			test -z "$JFONT" && JFONT=$(texfont ipaexg.ttf)
			test -z "$JFONT" && JFONT=$(sysfont ipaexg.ttf )
			test -z "$JFONT" && JFONT=$(texfont ipag.ttf)
			test -z "$JFONT" && JFONT=$(sysfont ipag.ttf )
			# then try vlgothic which is currently maintained
			test -z "$JFONT" && JFONT=$(sysfont VL-Gothic-Regular.ttf )
			test -z "$JFONT" && JFONT=$(sysfont NotoSansCJKjp-Regular.otf )
			test -z "$JFONT" && JFONT=$(sysfont kochi-gothic-subst.ttf )
			test -z "$JFONT" && JFONT=$(sysfont sawarabi-gothic-medium.ttf )
			# try pan-unicode fonts as a fallback (i.e. chinese, but they do cover japanese)
			test -z "$JFONT" && JFONT=$(sysfont wqy-zenhei.ttc )
			# Versions of Droid Sans that includes Chinese and Japanese.
			# For minimal use, oldest known version of DroidSansFallback works.
			# The FallbackFull variant adds lots of other writing systems,
			# but in 2010 dropped Hangul (for Korean) and is therefore a
			# last-ditch fallback for Chines and Japanese only.
			test -z "$JFONT" && CFONT=$(sysfont DroidSansFallback.ttf )
			test -z "$JFONT" && CFONT=$(sysfont DroidSansFallbackFull.ttf )
			if [ -n "$JFONT" ]; then
				echo "$JFONT"
			else
				echo "WARNING: None found"
				let WARNINGS=$WARNINGS+1
			fi
		else
			echo "japanese deselected"
		fi
	fi

	if [ -n "$KFONT" ]; then
		# font was requested, confirm it exists
		SOUGHTFONT="$KFONT"
		KFONT=$(sysfont "$KFONT")
		test -z "$KFONT" && KFONT=$(texfont "$SOUGHTFONT")
		if [ -z "$KFONT" ]; then
			echo "ERROR: specified korean font $SOUGHTFONT not found"
			exit 1
		else
			echo "will use specified font $KFONT for korean text"
		fi
	else
		if [ -n "$TESTKOREAN" ]; then
			echo -n  "Looking for a Korean font ... "
			# As elsewhere, prefer serif-equivalent to sans,
			# then fall back to one chinese pan-unicode font
			# (Droid Sand Fallback does not include hangul glyphs).
			# Nanum fonts are, or were, used in Ubuntu
			# Unfonts and Baekmuk come from collection-langkorean.
			# serif
			test -z "$KFONT" && KFONT=$(texfont UnGungseo.ttf )
			test -z "$KFONT" && KFONT=$(sysfont UnGungseo.ttf )
			test -z "$KFONT" && KFONT=$(texfont UnBatang.ttf )
			test -z "$KFONT" && KFONT=$(sysfont UnBatang.ttf )
			test -z "$KFONT" && KFONT=$(sysfont NanumMyeongjo.ttf )
			test -z "$KFONT" && KFONT=$(texfont batang.ttf )
			test -z "$KFONT" && KFONT=$(sysfont batang.ttf )
			# sans
			test -z "$KFONT" && KFONT=$(texfont UnDotum.ttf )
			test -z "$KFONT" && KFONT=$(sysfont UnDotum.ttf )
			test -z "$KFONT" && KFONT=$(texfont UnGraphic.ttf )
			test -z "$KFONT" && KFONT=$(sysfont UnGraphic.ttf )
			test -z "$KFONT" && KFONT=$(sysfont NanumGothic.ttf )
			test -z "$KFONT" && KFONT=$(texfont gulim.ttf )
			test -z "$KFONT" && KFONT=$(sysfont gulim.ttf )
			test -z "$KFONT" && KFONT=$(texfont dotum.ttf )
			test -z "$KFONT" && KFONT=$(sysfont dotum.ttf )
			# Noto CJK - all include korean hangul
			test -z "$KFONT" && KFONT=$(sysfont NotoSansCJKkr-Regular.otf )
			test -z "$KFONT" && KFONT=$(sysfont NotoSansCJKjp-Regular.otf )
			test -z "$KFONT" && KFONT=$(sysfont NotoSansCJKsc-Regular.otf )
			test -z "$KFONT" && KFONT=$(sysfont NotoSansCJKtc-Regular.otf )
			# pan-unicode
			test -z "$KFONT" && KFONT=$(sysfont wqy-zenhei.ttc )
			# All known versions of DrioidSansFallback work for Hangul, but
			# in 2010 Hangul was dropped from DroidSansFallbackFull in favour
			# of NanumGothic.
			# https://android.googlesource.com/platform/frameworks/base.git/+/
			# 562c45cc841681ed80d4e94515b23c28eb60eae4
			test -z "$JFONT" && CFONT=$(sysfont DroidSansFallback.ttf )
			if [ -n "$KFONT" ]; then
				echo "$KFONT"
			else
				echo "WARNING: None found"
				let WARNINGS=$WARNINGS+1
			fi
		else
			echo "korean deselected"
		fi
	fi
fi

if [ -n "$TESTFONTS" ]; then
	if [ -z "$TESTBIDIPOEM" ]; then
		echo "bidipoem deselected, will not test for poem font"
	fi
	if [ -n "$POEMFONT" ]; then
		# font was requested, confirm it exists
		SOUGHTFONT="$POEMFONT"
		POEMFONT=$(sysfont "$POEMFONT")
		test -z "$POEMFONT" && POEMFONT=$(texfont "$SOUGHTFONT")
		if [ -z "$POEMFONT" ]; then
			echo "ERROR: specified poem font $POEMFONT not found"
			exit 1
		else
			echo "will use specified font $POEMFONT for bidipoem"
		fi
	else
		echo -n "Looking for a font for english/persian poems: "
		# persian modern regular used to be supplied with texlive
		# but is no longer included.
		test -z "$POEMFONT" && POEMFONT=$(texfont persian-modern-regular.ttf)
		# When looking at hte persion text on farsi wikipedia
		# https://fa.wikipedia.org/wiki/%D8%B1%D8%A8%D8%A7%D8%B9%DB%8C%D8%A7%D8%AA_%D8%AE%DB%8C%D8%A7%D9%85?useskin=vector
		# similarly, DejaVu Sans might be from texlive or system
		test -z "$POEMFONT" && POEMFONT=$(texfont DejaVuSans.ttf)
		test -z "$POEMFONT" && POEMFONT=$(sysfont DejaVuSans.ttf )
		# FreeSerif is supplied with texlive or might have been installed
		test -z "$POEMFONT" && POEMFONT=$(texfont FreeSerif.otf)
		test -z "$POEMFONT" && POEMFONT=$(sysfont FreeSerif.otf )
		# NB neither nazli nor FreeFarsi will support the English text in my
		# example (fancy double-quotes and apostrophe are missing).
		if [ -n "$POEMFONT" ]; then
			echo $POEMFONT
		else
			TESTBIDIPOEM=
			echo "no suitable font found, bidipoem deselected"
		fi
	fi
fi

# - - - - - - - - - - - - - - - - -
# for safety, now check that all used packages have been installed.
# these all check for .sty files
echo "checking for a stylesheet from each collection used by one or more tests"

# babel.sty implies collection-latex
check_for babel HAVEBABEL

# biblatex.sty is from collection-bibtexextra
# but biber is in the same collection, so any
# test using biber does not need to test for this.
check_for biblatex HAVEBIBLATEX

# bidi.sty is from collection-langarabic
check_for bidi HAVEBIDI

# dejavu-otf.sty is from collection-fontsextra
check_for dejavu-otf HAVEDEJAVU

# fontspec implies collection-latexrecommended
# which in turn implies collection-latex
check_for fontspec HAVEFONTSPEC

# lipsum.sty implies collection-latexextra,
# that implies collection latex
check_for lipsum HAVELIPSUM

# luacode.sty implies collection-luatex
check_for luacode HAVELUACODE

# luatexja.sty implies collection-langjapanese
check_for luatexja HAVEJALUA

# tikz.sty implies collection-pictures
check_for tikz HAVETIKZ

# try to see if blend mode is supported
TIKZCODE=$(kpsewhich tikz.code.tex)
if [ -n "$TIKZCODE" ]; then
	echo -n "looking to see if blend mode is supported ... "
	grep -q 'blend mode' $TIKZCODE
	if [ $? -eq 0 ]; then
		echo "yes"
		HAVEBLEND=0
	else
		echo "no"
		HAVEBLEND=1
	fi
fi

# siunitx.sty implies collection-mathscience.
check_for siunitx HAVESIUNITX

# tipa.sty implies collection-fontsrecommended.
check_for tipa HAVETIPA

# xecjk.sty implies collection-langcjk.
check_for xeCJK HAVEXECJK

# counters for tests in make all and other tests
let ENABLED=0
let EXTRA=0

#-----------------------------------------------------------------------
# now, disable any enabled target for which required packages are missing

# biblatex.in deps for biblatex-biber
RUNBIBLB=1
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTBIBER" ] ||
   [ -z "$BIBER" ] || [ -z "$PDFLATEX" ] ||  [ -z "$PDFTOTEXT" ]; then
	test -n "$TESTBIBER"       ||
		echo "biblatex-biber cannot be tested, biber deselected"
	test -n "$TESTALLPDFLATEX" ||
		echo "biblatex-biber cannot be tested, allpdflatex deselected"
	test -n "$BIBER"           ||
		echo "biblatex-biber: biber was not found (collection-bibtexextra)"
	test -n "$PDFLATEX"        ||
		echo "biblatex-biber: pdflatex was not found (collection-latex)"
	test -n "$PDFTOTEXT"       ||
		echo "biblatex-biber: pdftotext was not found (system program)"
elif [ -n "$PDFLATEX" ] && [ -n "$BIBER" ]; then
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVELIPSUM -eq 0   ||
		echo "biblatex-biber: missing lipsum.sty (collection-latexextra)"
	test $HAVEFONTSPEC -eq 0 ||
		echo "biblatex-biber: missing fontspec.sty (collection-latexrecommended) "
	if [ $HAVELIPSUM -eq 0 ] && [ $HAVEFONTSPEC -eq 0 ]; then
		RUNBIBLB=0
		let ENABLED=$ENABLED+1
	fi
fi
echo "completed biblatex-biber tests"

# biblatex.in deps for biblatex-bibtex
# Note that bibtex is in collection-basic so must be present.
RUNBIBLT=1
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTBIBTEX" ]; then
	test -n "$TESTALLPDFLATEX" ||
		echo "biblatex-bibtex cannot be tested, allpdflatex deselected"
	test -n "$TESTBIBTEX" ||
		echo "biblatex-bibtex cannot be tested, bibtex deselected"
elif [ -n "$PDFLATEX" ]; then
	test $HAVEBIBLATEX -eq 0 ||
		echo "biblatex-bibtex: missing biblatex.sty (collection-bibtexextra)"
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVELIPSUM -eq 0   ||
		echo "biblatex-bibtex: missing lipsum.sty (collection-latexextra)"
	test $HAVEFONTSPEC -eq 0 ||
		echo "biblatex-bibtex: missing fontspec.sty (collection-latexrecommended) "
	if [ $HAVEBIBLATEX -eq 0 ] && [ $HAVELIPSUM -eq 0 ] && [ $HAVEFONTSPEC -eq 0 ]; then
		RUNBIBLT=0
		let ENABLED=$ENABLED+1
	fi
else
	test -n "$PDFLATEX" || echo "biblatex-bibtex: pdflatex was not found (collection-latex)"
fi
echo "completed biblatex-bibtex tests"

# biblatex-unibiber.tex deps
RUNBIBLU=1
# Progs might be deselected or missing
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTBIBER" ] ||
   [ -z "$BIBER" ] || [ -z "$PDFLATEX" ] ||  [ -z "$PDFTOTEXT" ]; then
	test -n "$TESTBIBER"       ||
		echo "biblatex-unibiber cannot be tested, biber deselected"
	test -n "$TESTALLPDFLATEX" ||
		echo "biblatex-unibiber cannot be tested, allpdflatex deselected"
	test -n "$BIBER"           ||
		echo "biblatex-unibiber: biber was not found (collection-bibtexextra)"
	test -n "$PDFLATEX"        ||
		echo "biblatex-unibiber: pdflatex was not found (collection-latex)"
	test -n "$PDFTOTEXT"       ||
		echo "biblatex-unibiber: pdftotext was not found (system program)"
elif [ -n "$PDFLATEX" ] &&  [ -n "$BIBER" ]; then
	TRYBIBLU=1
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVELIPSUM -eq 0 ||
		echo "biblatex-unibiber: missing lipsum.sty (collection-latexextra)"
	test -n "$TRYBIBLU" && test $HAVELIPSUM -eq 0 || TRYBIBLU=
fi
if [ -n "$TRYBIBLU" ]; then
	RUNBIBLU=0
	let ENABLED=$ENABLED+1
fi
echo "completed biblatex-unibiber tests"

# bibtexidx.tex deps
# Note that bibtex and makeindex are in collection-basic so must be present
RUNBTX=1
if  [ -z "$TESTBIBTEX" ] || [ -z "$TESTMAKEINDEX" ] || [ -z "$TESTALLPDFLATEX" ]; then
	test -n "$TESTBIBTEX"      ||
		echo "bibtexidx cannot be tested, bibtex deselected"
	test -n "$TESTMAKEINDEX"   ||
		echo "bibtexidx cannot be tested, makeindex deselected"
	test -n "$TESTALLPDFLATEX" ||
		echo "bibtexidx cannot be tested, allpdflatex deselected"
	TRYBTX=
elif [ -n "$PDFLATEX" ]; then
	TRYBTX=1
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVEFONTSPEC -eq 0 ||
		echo "bibtexidx: missing fontspec.sty (collection-latexrecommended)"
	test -n "$TRYBTX" && test $HAVEFONTSPEC -eq 0 || TRYBTX=
	if [ -n "$TRYBTX" ]; then
		RUNBTX=0
		let ENABLED=$ENABLED+1
	fi
else
	test -n "$PDFLATEX"  || echo "bibtexidx: pdflatex was not found (collection-latex)"
fi
echo "completed bibtexidx tests"


RUNBLUA=

# bibxelatex.in deps
# use TRYBIBXEL to identify that the required packages are present
# this will NOT prove that the necessary other deps are present
# so move those tests to just after this.
RUNBIBXELATEX=1
if [ -n "$TESTFONTS" ]; then # cannot enable bibxelatex without fonts
	# Progs might be deselected or missing
	if [ -z "$TESTBIBXEL" ] || [ -z "$TESTBIBER" ] || [ -z "$TESTXELATEX" ] ||
       [ -z "$BIBER" ]; then
		test -n "$TESTBIBXEL"  ||
			echo  "bibxelatex cannot be tested, bibxelatex deselected"
		test -n "$TESTBIBER"   ||
			echo "bibxelatex cannot be tested, biber deselected"
		test -n "$TESTXELATEX" ||
			echo "bibxelatex cannot be tested, xelatex deselected"
		test -n "$BIBER" ||
			echo "bibxelatex: biber was not found (collection-bibtexextra)"
	elif [ -n "$TESTBIBXEL" ] &&
	     [ -n "$BIBER" ] && [ -n "$PDFTOTEXT" ] && [ -n "$XELATEX" ]; then
		TRYBIBXEL=1
		# collection-latexrecommended ind collection-latexextra both imply
		# collection-latex, no need to test for BABEL
		test $HAVEFONTSPEC -eq 0 ||
			echo "bibxelatex: missing fontspec.sty (collection-latexrecommended)"
		test $HAVELIPSUM -eq 0   ||
			echo "bibxelatex: missing lipsum.sty (collection-latexextra)"
		test $HAVEXECJK -eq 0    ||
			echo "bibxelatex: missing xeCJK.sty (collection-langcjk)"

		test -n "$CFONT" || echo "bibxelatex - need a chinese font, none found"
		test -n "$JFONT" || echo "bibxelatex - need a japanese font, none found"

		test -n "$TRYBIBXEL" && test $HAVEFONTSPEC -eq 0 || TRYBIBXEL=
		test -n "$TRYBIBXEL" && test $HAVELIPSUM -eq 0 || TRYBIBXEL=
		test -n "$TRYBIBXEL" && test $HAVEXECJK -eq 0 || TRYBIBXEL=
		if [ -n "$TRYBIBXEL" ]; then
		# bibxelatex needs both CFONT AND JFONT
			if [ -n "$CFONT" ] && [ -n "$JFONT" ]; then
				RUNBIBXELATEX=0
				let ENABLED=$ENABLED+1
			fi
		fi
	else
		# The possibilities for deselecting this test, or for missing programs,
		# are complex, I hope I've got them all but add a longstop!
		echo "bibxelatex: logic error in tests!"
		exit 1
	fi
	echo "completed bibxelatex tests"
else
	echo "skipped bibxelatex tests"
fi

# bidipoem.in deps
# Need the poemfont, but 
RUNBIDI=1
if [ -n "$TESTFONTS" ]; then
	if [ -z "$TESTXELATEX" ]; then
		test -n "$TESTXELATEX" ||
			echo "bidipoem cannot be tested, xelatex deselected"
	elif [ -n "$XELATEX" ] && [ -n "$TESTBIDIPOEM" ]; then
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test -n "$TESTBIDIPOEM" && test $HAVEFONTSPEC -eq 0 ||
			echo "bidipoem: missing fontspec.sty (collection-latexrecommended)"
		test -n "$TESTBIDIPOEM" && test $HAVELIPSUM -eq 0  ||
			echo "bidipoem: missing lipsum.sty (collection-latexextra)"
		test -n "$TESTBIDIPOEM" && test $HAVEBIDI -eq 0     ||
			echo "bidipoem: missing bidi.sty (collection-langarabic)"
		test -n "$TESTBIDIPOEM" && TRYBIDI=1
		test -n "$TRYBIDI" && test $HAVEFONTSPEC -eq 0 || TRYBIDI=
		test -n "$TRYBIDI" && test $HAVEBIDI -eq 0     || TRYBIDI=
		test -n "$TRYBIDI" && test $HAVELIPSUM -eq 0  || TRYBIDI=
		if [ -n "$TRYBIDI" ]; then
			let ENABLED=$ENABLED+1
			RUNBIDI=0
		fi
	else
		test -n "$XELATEX" || echo "bidipoem: xelatex was not found (collection-xetex)"
	fi
	echo "completed bidipoem tests"
else
	echo "skipped bidipoem tests"
fi

# blended-circles.tex depends on HAVETIKZ and HAVEBLEND
RUNBLC=1
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTTIKZ" ]; then
	test -n "$TESTALLPDFLATEX" ||
		echo "blended-circles cannot be tested, allpdflatex deselected"
	test -n "$TESTTIKZ"        ||
		echo "blended-circles cannot be tested, tikz deselected"
else
	test -n "$PDFLATEX"   || echo "blanded-circles: pdflatex was not found (collection-latex)"
	test $HAVETIKZ -eq 0  || echo "blended-circles: missing tikz.sty (collection-pictures)"
	test $HAVEBLEND -eq 0 || echo "blended-circles: blend mode is not available in your tikz"
	if [ -n "$PDFLATEX" ] && [ $HAVETIKZ -eq 0 ] && [ $HAVEBLEND -eq 0 ]; then
		let ENABLED=$ENABLED+1
		RUNBLC=0
	fi
fi
echo "completed blended-circles tests"

# deps for cylinder.tex which uses asy
RUNCYL=1
TRYCYL=true
test -n "$TESTALLPDFLATEX" || echo "cylinder cannot be tested, allpdflatex deselected"
test -n "$TESTALLPDFLATEX" || TRYCYL=
test -n "$TESTASY"         || echo "cylinder cannot be tested, asymptote deselected"
test -n "$TESTASY"         || TRYCYL=
test -n "$TESTCYL"         || echo "cylinder cannot be tested, cylinder deselected"
test -n "$TESTCYL"         || TRYCYL=
if [ -n "$TRYCYL" ]; then
	test -n "$ASY"      || echo "cylinder: asy was not found (collection-binextra)"
	test -n "$GS"       || echo "cylinder: ghostscript was not found (system dynamic lib)"
	test -n "$PDFLATEX" || echo "cylinder: pdflatex was not found (collection-latex)"
	test -n "$ASY"      || TRYCYL=
	test -n "$GS"       || TRYCYL=
	test -n "$PDFLATEX" || TRYCYL=
fi
if [ -n "$TRYCYL" ]; then
	RUNCYL=0
	let ENABLED=$ENABLED+1
fi
echo "completed cylinder tests"

echo "running lualatexbib tests"
# Dependencies for lualatexbib
if [ -z "$HAVEDEJAVU" ]; then
	echo "dejavu-otf.sty from collection-fontsextra not found"
	echo "lualatexbib deselected"
fi
if [ -z "$HAVEBABEL" ]; then
	echo "babel.sty from collection-latex not found"
	echo "lualatexbib deselected"
fi
if [ -z "$HAVELUACODE" ]; then
	echo "luacode.sty from collection-luatex not found"
	echo "lualatexbib deselected"
fi

if [ -n "$HAVEDEJAVU" ] && [ -n "$HAVEBABEL" ] && [ -n "$HAVELUACODE" ]; then
	RUNTBLUA=0
	let ENABLED=$ENABLED+1
	echo "lualatexbib can be run"
fi
echo "completed lualatexbib tests"

# dvisvgm test depends on DVISVGM, LATEX, HAVETIKZ
RUNDVI=1
if [ -z "$TESTDVISVGM" ] || [ -z "$TESTTIKZ" ]; then
	test -n "$TESTDVISVGM" || echo "dvisvgm cannot be tested, dvisvgm deselected"
	test -n "$TESTTIKZ"    || echo "dvisvgm cannot be tested, tikz deselected"
elif [ -z "$LATEX" ]; then
	echo "dvisvgm: latex was not found"
elif [ -n "$DVISVGM" ] && [ -n "$LATEX" ]; then
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVELIPSUM -eq 0 || echo "dvisvgm: fontspace.sty is missing (collection-latexextra)"
	test $HAVETIKZ -eq 0   || echo "dvisvgm: tikz.sty is missing (collection-pictures)"
	if [ $HAVELIPSUM -eq 0 ] && [ $HAVETIKZ -eq 0 ]; then
		RUNDVI=0
		let ENABLED=$ENABLED+1
	fi
fi
echo "completed dvisvgm tests"

# jalua.in deps
RUNJALUA=1
if [ -n "$TESTFONTS" ]; then # cnnto enable jalua without fonts
	if  [ -z "$TESTLUALATEX" ]; then
		echo "jalua cannot be tested, lualatex deselected by request"
	else
		TRYJALUA=1
		test $HAVEJALUA -eq 0    ||
			echo "jalua: luatexja.sty missing (collection-langjapanese)"
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test $HAVEFONTSPEC -eq 0 ||
			echo "jalua: fontspec.sty missing (collection-latexrecommended)"
		test $HAVEJALUA -eq 0 || TRYJALUA=
		test $HAVEFONTSPEC -eq 0 || TRYJALUA=
		if [ -n "$TRYJALUA" ]; then
			# Specific fonts for mincho and gothic,
			# for each I prefer heavy but allow regular
			JALGOT=$(texfont HaranoAjiGothic-Heavy.otf)
			if [ -n "$JALGOT" ]; then
				echo "jalua: found $JALGOT"
			else
				JALGOT=$(texfont HaranoAjiGothic-Regular.otf)
				if [ -n "$JALGOT" ]; then
					echo "jalua: found $JALGOT"
				else
					TRYJALUA=
				fi
			fi
		fi
		if [ -n "$JALGOT" ]; then
			JALMIN=$(texfont HaranoAjiMincho-Heavy.otf)
			if [ -n "$JALMIN" ]; then
				echo "jalua: found $JALMIN"
			else
				JALMIN=$(texfont HaranoAjiMincho-Regular.otf)
				if [ -n "$JALMIN" ]; then
					echo "jalua: found $JALMIN"
				else
					TRYJALUA=
				fi
			fi
		fi
		if [ -z "$TRYJALUA" ]; then
			echo "Cannot run jalua, missing font(s)"
		fi
	fi
	if [ -n "$TRYJALUA" ]; then
		RUNJALUA=0
		let ENABLED=$ENABLED+1
	fi
	echo "completed jalua tests"
else
	echo "skipped jalua tests"
fi

# deps to run ligatures
# lualatex is in collection-basic, but might have been deselected
RUNLIG=1
if [ -n "$TESTFONTS"  ]; then
	if [ -n "$TESTLUALATEX" ] && [ -n "$TESTLIG" ]; then
		TRYLIG=1
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test $HAVEFONTSPEC -eq 0  ||
			echo " ligatures missing fontspec.sty (collection-latexrecommended)"
		test $HAVELUACODE -eq 0   ||
			echo " ligatures missing luacode.sty (collection-luatex"
		test -n "$LIGFONT"        ||
			 echo "Cannot test ligatures, need Linux Libertine O font"
		test -n "$LIGFONT"                             || TRYLIG=
		test -n "$TRYLIG" && test $HAVELUACODE -eq 0   || TRYLIG=
		test -n "$TRYLIG" && test $HAVEFONTSPEC -eq 0  || TRYLIG=
		if [ -n "$TRYLIG" ]; then
			RUNLIG=0
			let ENABLED=$ENABLED+1
		fi
	else
		test -n "$TESTLIG      "||
			echo "ligatures cannot be tested, deselected by request"
		test -n "$TESTLUALATEX" ||
			echo "ligatures cannot be tested, lualatex deselected by request"
	fi
	echo "completed ligatures tests"
else
	echo "skipped ligatures tests"
fi

# lualatex.in deps
RUNLUALATEX=1
if [ -n "$TESTFONTS" ]; then
	# lualatex is in collection-basic, but might have been deselected
	if [ -n "$TESTLUALATEX" ]; then
		TRYLUA=1
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test -n "$TRYLUA" && test $HAVEFONTSPEC -eq 0  ||
			echo "lualatex: missing fontspec.sty (collection-latexrecommended)"
		test -n "$TRYLUA" && test $HAVELUACODE -eq 0   ||
			echo "lualatex: missing luacode.sty (collection-luatex)"
		test -n "$TRYLUA" && test $HAVELIPSUM -eq 0   ||
			echo "lualatex: missing lipsum.sty (collection-latexextra)"
		test -n "$TRYLUA" && test $HAVEFONTSPEC -eq 0  || TRYLUA=
		test -n "$TRYLUA" && test $HAVELUACODE -eq 0   || TRYLUA=
		test -n "$TRYLUA" && test $HAVELIPSUM -eq 0   || TRYLUA=
		# lualatex depends on LUALATEX, MAINFONT
		# and for complete coverage CFONT JFONT KFONT
		if [ -n "$TRYLUA" ] && [ -n "$MAINFONT" ]; then
			RUNLUALATEX=0
			let ENABLED=$ENABLED+1
		fi
	else
		echo "lualatex cannot be tested, deselected by request"
	fi
	echo "completed lualatex tests"
else
	echo "skipped lualatex tests"
fi

# mosfet.tex deps
RUNMOS=1
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTTIKZ" ]; then
	test -n "$TESTALLPDFLATEX" ||
		echo "mosfet cannot be tested, allpdflatex deselected"
	test -n "$TESTTIKZ"  ||
		echo "mosfet cannot be tested, tikz deselected"
elif [ -z "$PDFLATEX" ]; then
	echo "mosfet: pdflatex was not found (collection-latex)"
else
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVELIPSUM -eq 0  ||
		echo "mosfet: missing lipsum.sty (collection-latexextra)"
	test $HAVESIUNITX -eq 0 ||
		echo "mosfet: missing siunitx.sty (collection-mathscience"
	test $HAVETIKZ -eq 0    ||
		echo "mosfet: missing tikz.sty (collection-pictures)"
	if [ $HAVETIKZ -eq 0 ] && [ $HAVESIUNITX -eq 0 ] && [ $HAVELIPSUM -eq 0 ]; then
		let ENABLED=$ENABLED+1
		RUNMOS=0
	fi
fi
echo "completed mosfet tests"

# oldjap deps
# Note that dvipdfmx is in collection-basic so must be present.
# I specify tikz to check collection-pictures, where xcolor.sty comes from
RUNOJA=1
if [ -n "$TESTFONTS" ]; then
	if [ -z "$TESTDVIPDFMX" ] || [ -z "$TESTUPLATEX"i ] || [ -z "$TESTTIKZ" ]; then
		test -n "$TESTDVIPDFMX" || echo  "oldjap cannot be tested, dvipdfmx deselected"
		test -n "$TESTTIKZ"     || echo  "oldjap cannot be tested, tikz deselected"
		test -n "$TESTUPLATEX"  || echo  "oldjap cannot be tested, uplatex deselected"
	else
		TRYOJA=1
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test $HAVELIPSUM -eq 0 ||
			echo "oldjap: missing lipsum.sty (collection-latexextra)"
		test $HAVEJALUA -eq 0  ||
			echo "oldjap: missing luatexja.sty (collection-langjapanese)"
		test $HAVETIKZ -eq 0   ||
			echo "oldjap: missing tikz.sty (collection-pictures)"

		# Specific texlive fonts for mincho and gothic, no choice
		# These ought to come from collection-langjapanese
		OLDJAPGOT=$(texfont HaranoAjiGothic-Medium.otf)
		test -n "$OLDJAPGOT"   || echo "oldjap: missing HaranoAjiGothic-Medium.otf font"
		OLDJAPMIN=$(texfont HaranoAjiMincho-Regular.otf)
		test -n "$OLDJAPMIN"  || echo "oldjap: missing HaranoAjiMincho-Regular.otf font"

		test $HAVELIPSUM -eq 0 || TRYOJA=
		test $HAVEJALUA -eq 0  || TRYOJA=
		test $HAVETIKZ -eq 0   || TRYOJA=
		test -n "$OLDJAPGOT"   || TRYOJA=
		test -n "$OLDJAPMIN"   || TRYOJA=
		if [ -n "$TRYOJA" ]; then
			RUNOJA=0
			let ENABLED=$ENABLED+1
		fi
	fi
	echo "completed oldjap tests"
else
	echo "skipped oldjap tests"
fi

# paren depends on PDFLATEX, RUBY and MATCHPARENS
# and is driven by RUNPAREN
RUNPAREN=1
if [ -z "$TESTALLPDFLATEX" ]; then
	echo "paren cannot be tested, allpdflatex deselected"
elif [ -z "$TESTRUBY" ]; then
	echo "paren cannot be tested, ruby deselected"
else
	test -n "$PDFLATEX"    || echo "paren: pdflatex was not found (collection-latex)"
	test -n "$RUBY"        || echo "paren: ruby was not found (system program)"
	# MATCHPARENS only looked for if ruby was found
	test -n "$MATCHPARENS" || echo "paren: match_parens was not found"
	if [ -n "$PDFLATEX" ] &&  [ -n "$RUBY" ] && [ -n "$MATCHPARENS" ]; then
		RUNPAREN=0
		let ENABLED=$ENABLED+1
	fi
fi
echo "completed paren tests"

# pdflatex as a program used to always be required, now optional
# but deps for my pdflatex.tex file might be missing:
# tikz checks for collection-pictures, needed for xcolor.sty.
RUNPDF=1
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTPDFLATEX" ] || [ -z "$TESTTIKZ" ]; then
	test -n "$TESTALLPDFLATEX" || echo "pdflatex cannot be tested, allpdflatex deselected"
	test -n "$TESTPDFLATEX"    || echo "pdflatex cannot be tested, pdflatex deselected"
	test -n "$TESTTIKZ"        || echo "pdflatex cannot be tested, tikz deselected"
elif [ -z "$PDFLATEX" ]; then
	echo "pdflatex: pdflatex was not fouud (collection-latex)"
else
	TRYPDF=yes
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVELIPSUM -eq 0   ||
		echo "pdflatex: missing lipsum.sty (collection-latexextra)"
	test $HAVEFONTSPEC -eq 0 ||
		echo "pdflatex: missing fontspec.sty (collection-latexrecommended)"
	test $HAVETIKZ -eq 0     ||
		echo "pdflatex: missing tikz.sty (collection-pictures)"

	test $HAVELIPSUM -eq 0   || TRYPDF=
	test $HAVEFONTSPEC -eq 0 || TRYPDF=
	test $HAVETIKZ -eq 0     || TRYPDF=

	if [ -n "$TRYPDF" ]; then
		RUNPDF=0
		let ENABLED=$ENABLED+1
	fi
fi
echo "completed pdflatex tests"

# periodicsvg.tex depends on not being disabled, needs latex, dvisvgm
# uses graphics so tests for tikz
RUNPRS=1
if [ -z "$TESTDVISVGM" ] || [ -z  "$TESTPERIODICSVG" ] || [ -z "$TESTTIKZ" ]; then
	test -n "$TESTDVISVGM"     || echo "cannot test periodicsvg, dvisvgm deselected"
	test -n "$TESTPERIODICSVG" || echo "cannot test periodicsvg, periodicsvg deselected"
	test -n "$TESTTIKZ"        || echo "cannot test periodicsvg, tikz deselected"
else
	test -n "$DVISVGM" || echo "periodicsvg: dvisvgm was not found"
	test -n "$LATEX"   || echo "periodicsvg: latex was not found"
	if [ -n "$LATEX" ] && [ -n "$DVISVGM" ]; then
		TRYPRS=true
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test $HAVELIPSUM -eq 0   ||
			echo "periodicsvg: missing lipsum.sty (collection-latexextra)"
		test $HAVEFONTSPEC -eq 0 ||
			echo "periodicsvg: missing fontspec.sty (collection-latexrecommended)"
		test $HAVETIKZ -eq 0     ||
			echo "periodicsvg: missing tikz.sty (collection-pictures)"
		test $HAVELIPSUM -eq 0   || TRYPRS=
		test $HAVEFONTSPEC -eq 0 || TRYPRS=
		test $HAVETIKZ -eq 0     || TRYPRS=
		if [ -n "$TRYPRS" ]; then
			let ENABLED=$ENABLED+1
			RUNPRS=0
		fi
	fi
fi
echo "completed periodicsvg tests"

# periodictab.tex depends on not being disabled, needs pdflatex
# again, tikz is a proxy for colleciton-pictures.
RUNPRT=1
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTPERIODICTAB" ] || [ -z "$TESTTIKZ"  ]; then
	test -n "$TESTALLPDFLATEX" || echo "cannot test periodictab, allpdflatex deselected"
	test -n "$TESTPERIODICTAB" || echo "cannot test periodictab, periodictab deselected"
	test -n "$TESTTIKZ"        || echo "cannot test periodictab, tikz deselected"
else
	if [ -n "$PDFLATEX" ]; then
		TRYPRT=true
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test $HAVELIPSUM -eq 0 ||
			echo "periodictab: missing lipsum.sty (collection-latexextra)"
		test $HAVETIKZ -eq 0   ||
			 echo "periodictab: missing tikz.sty (collection-pictures)"
		test $HAVELIPSUM -eq 0 || TRYPRT=
		test $HAVETIKZ -eq 0   || TRYPRT=
		if [ -n "$TRYPRT" ]; then
			let ENABLED=$ENABLED+1
			RUNPRT=0
		fi
	else
		echo "periodictab: pdflatex was not found (collection-latex)"
	fi
fi
echo "completed periodictab tests"

# deps for triangles.tex which uses asy
RUNTRI=1
TRYTRI=true
test -n "$TESTALLPDFLATEX" || echo "triangles cannot be tested, allpdflatex deselected"
test -n "$TESTALLPDFLATEX" || TRYTRI=
test -n "$TESTASY"         || echo "triangles cannot be tested, asymptote deselected"
test -n "$TESTASY"         || TRYTRI=
test -n "$TESTTRI"         || echo "triangles cannot be tested, triangles deselected"
test -n "$TESTTRI"         || TRYTRI=
if [ -n "$TRYTRI" ]; then
	test -n "$ASY"      || echo "triangles: asy was not found (collection-binextra)/"
	test -n "$GS"       || echo "triangles: ghostscript was not found (system dynamic lib)"
	test -n "$PDFLATEX" || echo "triangles: pdflatex was not found (collection-latex)"
	test -n "$ASY"      || TRYTRI=
	test -n "$GS"       || TRYTRI=
	test -n "$PDFLATEX" || TRYTRI=
fi
if [ -n "$TRYTRI" ]; then
	RUNTRI=0
	let ENABLED=$ENABLED+1
fi
echo "completed triangles tests"

# xelatex.in deps : it needs xelatex and a main font
RUNXELATEX=1
if [ -n "$TESTFONTS" ]; then
	if [ -z "$TESTXELATEX" ]; then
		echo "xelatex cannot be tested, xelatex deselected"
	else
		if [ -z "$XELATEX" ] || [ -z "$MAINFONT" ]; then
			test -n "$XELATEX"  ||
				echo "xelatex: xelatex was not found (collection-xetex)"
			test -n "$MAINFONT" ||
				echo "xelatex: suitable font not found"
		else
			TRYXELATEX=1
			# note that latexextra and latexrecommended both imply latex
			# so no need to test for BABEL
			test $HAVEFONTSPEC -eq 0 ||
				echo "xelatex: missing fontspec.sty (collection-latexrecommended)"
			test $HAVEFONTSPEC -eq 0 || TRYXELATEX=
			if [ -n "$TRYXELATEX" ]; then
				RUNXELATEX=0
				let ENABLED=$ENABLED+1
			fi
		fi
	fi
	echo "completed xelatex tests"
else
	echo "skipped xelatex tests"
fi

# xindy.in deps
if [ "$TESTFO~NTS" ]; then
	RUNXINDY=1
	if [ -z "$TESTXINDY" ] || [ -z "$TESTLUALATEX" ]; then
		test -n "$TESTXINDY"    ||
			echo "xindy cannot be tested, xindy deselected by request"
		test -n "$TESTLUALATEX" ||
			echo "xindy cannot be tested, lualatex deselected by request"
	elif [ -z "$XINDY" ] || [ -z "$MAKEGLOSSARIES" ] || [ -z "$PDFINFO" ]; then
		test -n "$XINDY"          || echo "xindy: xindy was not found"
		test -n "$MAKEGLOSSARIES" || echo "xindy: make glossaries was not found"
		test -n "$PDFINFO"        || echo "xindy: pdfinfo was not found"
	else
		TRYXINDY=1
		# note that latexextra and latexrecommended both imply latex
		# so no need to test for BABEL
		test $HAVEFONTSPEC -eq 0 ||
			echo "xindy: missing fontspec.sty (collection-latexrecommended)"
		test $HAVELIPSUM -eq 0   ||
			echo "xindy: missing lipsum.sty (collection-latexextra)"
		test -n "$MAINFONT"      ||
			echo "xindy: no suitable font found"

		test -n "$TRYXINDY" && test $HAVEFONTSPEC -eq 0 || TRYXINDY=
		test -n "$TRYXINDY" && test $HAVELIPSUM -eq 0 || TRYXINDY=
		test -n "$TRYXINDY" && test -n "$MAINFONT" || TRYXINDY=
		if [ -n "$TRYXINDY" ]; then
			RUNXINDY=0
			let ENABLED=$ENABLED+1
		fi
	fi
	echo "completed xindy tests"
else
	echo "skipped xindy tests"
fi

# xindynonlua.tex deps
RUNXINDYBAS=1
if [ -z "$TESTALLPDFLATEX" ] || [ -z "$TESTXINDY" ]; then
	test -n "$TESTALLPDFLATEX" ||
		echo "cannot test xindynonlua, allpdflatex deselected by request"
	test -n "$TESTXINDY" ||
		echo "cannot test xindynonlua, xindy deselected by request"
elif [ -z "$TEXINDY" ] || [ -z "$PDFINFO@" ]; then
	test -n "$TEXINFO" || echo "xindynonlua: texindy was not found"
	test -n "$PDFINFO" || echo "xindynonlua: pdfinfo was not found"
else
	TRYXINDYNON=1
	test $HAVETIPA -eq 0 ||
		echo "xindynonlua: tipa.sty is missing (collection-fontsrecommended)"
	# note that latexextra and latexrecommended both imply latex
	# so no need to test for BABEL
	test $HAVEFONTSPEC -eq 0 ||
		echo "xindynonlua: fontspace.sty is missing (collection-latexrecommended)"
	test $HAVELIPSUM -eq 0 ||
		echo "xindynonlua: lipsum.sty is missing (collection-latexextra)"

	test $HAVETIPA -eq 0 || TRYXINDYNON=
	test $HAVEFONTSPEC -eq 0 || TRYXINDYiNON=
	test $HAVELIPSUM -eq 0 || TRYXINDYNON=
	test $HAVELIPSUM -eq 0 || TRYXINDYNON=
	if [ -n "$TRYXINDYNON" ]; then
		RUNXINDYBAS=0
		let ENABLED=$ENABLED+1
	fi
fi
echo "completed xindynonlua tests"

# non-automatic tests come here, not included in ENABLED
# remote1 and remote2 depend on pdflatex and biber
# They are NOT run as default tests
RUNREMOTE=1
if [ -n "$PDFLATEX" ] && [ -n "$BIBER" ]; then
	RUNREMOTE=0
	let EXTRA=2
fi
echo "completed (non-auto) remote tests"

# - - - - - - - - - - - - - - - - -
# Now document which tests can, and can, and cannot not, be made.
# Extra tests mentioned after documenting all the makeable test,
# they need biber so if no tests were found then it is fine to exit.
echo "Summary of available tests:"

if [ $ENABLED -eq 0 ]; then
	echo "No tests can be run"
	exit 2
fi

# these should be in alphabetical order
test $RUNBIBLB -eq 0 && echo " biblatex-biber can be tested" \
 || echo " biblatex-biber will NOT be tested"
test $RUNBIBLT -eq 0 && echo " biblatex-bibtex can be tested" \
 || echo " biblatex-bibtex will NOT be tested"
test $RUNBIBLU -eq 0 && echo " biblatex-unibiber can be tested" \
 || echo " biblatex-unibiber will NOT be tested"

# lualatexbib should come between biblatex-unibiber and bibxelatex.	
echo "RUNBLUA is $RUNBLUA at line 1564"

if [ $RUNBIBXELATEX -eq 0 ]; then
	if [ -n "$KFONT" ]; then
		echo " bibxelatex can be tested"
	else
		echo " bibxelatex can be tested, but without korean"
	fi
	if [ "$PREFER" = "chinese" ]; then
		echo "  using $CFONT for Han glyphs"
	else
		echo "  using $JFONT for Han glyphs"
	fi
	echo "  using $JFONT for Kana glyphs"
	if [ -n "$KFONT" ]; then
		echo "  using $KFONT for Hangul glyphs"
	fi
else
	echo " bibxelatex will NOT be tested"
fi
test $RUNBTX -eq 0 && echo " bibtexidx can be tested" || echo " bibtexidx will NOT be tested"
test $RUNBIDI -eq 0 && echo " bidipoem can be tested" || echo " bidipoem will NOT be tested"
test $RUNBLC -eq 0 && echo " blended-circles can be tested" || \
  echo " blended-circles will NOT be tested"

test $RUNCYL -eq 0 && echo " cylinder (asy 3D) can be tested" ||
							echo " cylinder will NOT be tested"

test $RUNDVI -eq 0 && echo " dvisvgm can be tested" ||	echo " dvisvgm will NOT be tested"

test $RUNJALUA -eq 0 && echo " jalua can be tested" ||	echo " jalua will NOT be tested"

test $RUNLIG -eq 0 && echo " ligatures can be tested" ||
							echo " ligatures will NOT be tested"

if [ $RUNLUALATEX -eq 0 ]; then
	if [ -n "$CFONT" ] && [ -n "$JFONT" ] && [ -n "$KFONT" ]; then
		LUAMSG="completely"
	else
		LUAMSG="but not for all CJK languages"
	fi
	echo " lualatex can be tested $LUAMSG"
	if [ -n "$LUAOTFLOADTOOL" ]; then
		# If you change the available fonts, and run fc-cache,
		# everything will find them.  EXCEPT lualatex, which uses
		# luaotfload-tool to maintain its own database and will
		# make its own decision about when to update.  So force it.
		echo "- running luaotfload-tool to ensure fonts are found"
		eval $LUAOTFLOADTOOL --update
	fi
else
	echo " lualatex will NOT be tested"
fi
test $RUNMOS -eq 0 && echo " mosfet can be tested" || echo " mosfet will NOT be tested"
test $RUNOJA -eq 0 && echo " oldjap can be tested" || echo " oldjap will NOT be tested"
test $RUNPAREN -eq 0 && echo " paren can be tested" || echo " paren will NOT be tested"
test $RUNPDF -eq 0 && echo " pdflatex can be tested" || echo " pdflatex will NOT be tested"
test $RUNPRS -eq 0 && echo " periodicsvg can be tested" || echo " periodicsvg will NOT be tested"
test $RUNPRT -eq 0 && echo " periodictab can be tested" || echo " periodictab will NOT be tested"
test $RUNTRI -eq 0 && echo " triangles (asy 2D) can be tested" || echo " triangles will NOT be tested"
if [ $RUNXELATEX -eq 0 ]; then
	if [ -n "$CFONT" ] && [ -n "$JFONT" ] && [ -n "$KFONT" ]; then
		XELMSG="completely"
	else
		XELMSG="but not for all CJK languages"
	fi
	echo " xelatex can be tested $XELMSG"
else
	echo " xelatex will NOT be tested"
fi
test $RUNXINDY -eq 0 && echo " xindy can be tested" || echo " xindy will NOT be tested"
test $RUNXINDYBAS -eq 0 && echo " xindynonlua can be tested" \
 || echo " xindynonlua will NOT be tested"

# I describe these as can be made rather than can be run because in each
# case they are invoked with 'make'.
echo "$ENABLED tests (out of $MAXTESTS) can be made"
if [ $EXTRA -gt 0 ]; then
	# currently, the remote tests for biber
	echo "$EXTRA non-default tests (out of $MAXEXTRA) can be manually made."
fi

# - - - - - - - - - - - - - - - - -
# write the Makefile here
# NB the 'all' target means all tests which can be run

# targets on which all: depends
TBID=
TBIX=
TBLR=
TBLT=
TBLU=
TBTX=
TCYL=
TDVI=
TDEJA=
TJLA=
TLUA=
TLIG=
TMOS=
TOJA=
TPAR=
TPRS=
TPRT=
TTRI=
TVOC=
TXEL=
TXIN=
TXNL=

# convert actual rules to # or uncommented
RBID=#
RBIX=#
RBLC=#
RBLR=#
RBLT=#
RBLU=#
RBTX=#
RCYL=#
RDVI=#
RJLA=#
RLIG=#
RLUA=#
RMOS=#
ROJA=#
RPAR=#
RPDF=#
RPRS=#
RPRT=#
RTRI=#
RXEL=#
RXIN=#
RXNL=#

if [ $RUNPDF -eq 0 ]; then
	TPDF=pdflatex
	RPDF=
fi

if [ $RUNTRI -eq 0 ]; then
	TTRI=triangles
	RTRI=
fi

if [ $RUNCYL -eq 0 ]; then
	TCYL=cylinder
	RCYL=
fi


if [ $RUNBIBLU -eq 0 ]; then
	TBLU=biblatex-unibiber
	RBLU=
fi

if [ $RUNBIBLB -eq 0 ]; then
	TBLR=biblatex-biber
	RBLR=
	echo "creating biblatex-biber.tex"
	sed 's/%BACKEND%/biber/g' biblatex.in >biblatex-biber.tex
fi

if [ $RUNBIBLT -eq 0 ]; then
	TBLT=biblatex-bibtex
	RBLT=
	echo "creating biblatex-bibtex.tex"
	sed 's/%BACKEND%/bibtex/g' biblatex.in >biblatex-bibtex.tex
fi

if [ $RUNBTX -eq 0 ]; then
	TBTX=bibtexidx
	RBTX=
fi

# lualatexbib currently only depends on dejavu-otf
# This one is NOT an integer
if [ "$RUNTBLUA" = "0" ]; then
	TBLUA=lualatexbib
	RBLUA=
else
	TBLUA=
	RBLUA=#
fi

if [ $RUNBIBXELATEX -eq 0 ]; then
	echo "creating bibxelatex.tex"
	sed "s%FONT%$MAINFONT%" bibxelatex.in >bibxelatex.tex
	# PREFER defaults to japanese, only acceptable alternative is chinese
	cp bibxelatex.tex{,.bak}
	if [ "$PREFER" = "chinese" ]; then
		sed -e "s/PREFER/$CFONT/" \
		    -e "s/FALLBACK/$JFONT/" bibxelatex.tex.bak >bibxelatex.tex
	else
		sed -e "s/PREFER/$JFONT/" \
		    -e "s/FALLBACK/$CFONT/" bibxelatex.tex.bak >bibxelatex.tex
	fi
	cp bibxelatex.tex{,.bak}
	if [ -n "$KFONT" ]; then
		sed "s%KOREAN%$KFONT%" bibxelatex.tex.bak >bibxelatex.tex
	else
		sed '/ %KR%/d' bibxelatex.tex.bak >bibxelatex.tex
	fi
	rm bibxelatex.tex.bak

	TBIX=bibxelatex
	RBIX=

fi

if [ $RUNBIDI -eq 0 ]; then
	TBID=bidipoem
	RBID=
	echo "creating bidipoem.tex"
	# expected DejaVu font is Sans, which uses too much space
	# for the persian (the two parts of the line are offset)
	# and needs to be scaled down.
	POEMSCALE=
	echo $POEMFONT | grep DejaVu && POEMSCALE="[Scale=0.9]"
	cat bidipoem.in | sed "s/%POEMFONT%/$POEMSCALE{$POEMFONT}/" > bidipoem.tex
fi

# blended-circles test - does not work on older versions of TikZ.
if [ "$RUNBLC" = 0 ]; then
	TBLC=blended-circles
	RBLC=
fi

if [ $RUNJALUA -eq 0 ]; then
	TJLA=jalua
	RJLA=

fi

if [ $RUNLIG -eq 0 ]; then
	TLIG=ligatures
	RLIG=
	sed -e "s/JALGOT/$JALGOT/" -e "s/JALMIN/$JALMIN/" jalua.in >jalua.tex
fi

if [ $RUNLUALATEX -eq 0 ]; then
	echo "creating lualatex.tex"
	if [ -n "$CFONT" ] || [ -n "$JFONT" ] || [ -n "$KFONT" ]; then
		sed 's/GOOGLE/CJK example(s) from translate.google.com./' \
		 lualatex.in >lualatex.tex
	else
		sed 's/GOOGLE/CJK examples omitted./' lualatex.in >lualatex.tex
	fi
	# do not use sed -i, it might not be GNU sed
	cp lualatex.tex{,.bak}
	if [ -n "$CFONT" ]; then
		sed "s%CHINESE%$CFONT%" lualatex.tex.bak >lualatex.tex
	else
		sed 's/.*CHINESE.*/(Simplified Chinese omitted, no suitable font)/' \
		 lualatex.tex.bak >lualatex.tex
	fi
	cp lualatex.tex{,.bak}
	if [ -n "$JFONT" ]; then
		sed "s%JAPANESE%$JFONT%" lualatex.tex.bak >lualatex.tex
	else
		sed 's/.*JAPANESE.*/(Japanese omitted, no suitable font)/' \
		 lualatex.tex.bak >lualatex.tex
	fi
	cp lualatex.tex{,.bak}
	if [ -n "$KFONT" ]; then
		sed "s%KOREAN%$KFONT%" lualatex.tex.bak >lualatex.tex
	else
		sed 's/.*KOREAN.*/(Korean omitted, no suitable font)/' \
		 lualatex.tex.bak >lualatex.tex
	fi
	cp lualatex.tex{,.bak}
	sed "s%FONT%$MAINFONT%" lualatex.tex.bak >lualatex.tex
	rm lualatex.tex.bak

	TLUA=lualatex
	RLUA=
fi

if [ $RUNMOS -eq 0 ]; then
	TMOS=mosfet
	RMOS=
fi

if [ $RUNOJA -eq 0 ]; then
	TOJA=oldjap
	ROJA=
fi

if [ $RUNPAREN -eq 0 ]; then
	TPAR=paren
	RPAR=
	# here we need the path to match_parens
	PARFILE=$(type -pa match_parens)
else
	PARFILE=
fi

if [ $RUNPRS -eq 0 ]; then
	TPRS=periodicsvg
	RPRS=
fi

if [ $RUNPRT -eq 0 ]; then
	TPRT=periodictab
	RPRT=
fi

if [ $RUNXELATEX -eq 0 ]; then
	echo "creating xelatex.tex"
	sed "s%FONT%$MAINFONT%" xelatex.in  >xelatex.tex
	# as before, do not assume sed can do sed -i
	cp xelatex.tex{,.bak}
	if [ -n "$CFONT" ]; then
		sed "s%CHINESE%$CFONT%" xelatex.tex.bak >xelatex.tex
	else
		sed '/\\chinese/d' xelatex.tex.bak >xelatex.tex
	fi
	cp xelatex.tex{,.bak}
	if [ -n "$JFONT" ]; then
		sed "s%JAPANESE%$JFONT%" xelatex.tex.bak >xelatex.tex
	else
		sed '/\\japanese/d' xelatex.tex.bak >xelatex.tex
	fi
	cp xelatex.tex{,.bak}
	if [ -n "$KFONT" ]; then
		sed "s%KOREAN%$KFONT%" xelatex.tex.bak >xelatex.tex
	else
		sed '/\\korean/d' xelatex.tex.bak >xelatex.tex
	fi
	rm xelatex.tex.bak

	TXEL=xelatex
	RXEL=
fi

if [ $RUNXINDY -eq 0 ]; then
	echo "creating xindy.tex"
	sed "s%FONT%$MAINFONT%" xindy.in >xindy.tex
	TXIN=xindy
	RXIN=
fi

if [ $RUNXINDYBAS -eq 0 ]; then
	TXNL=xindynonlua
	RXNL=
fi

if [ $RUNDVI -eq 0 ]; then
	TDVI=dvisvgm
	RDVI=
fi

if [ $RUNREMOTE -eq 0 ]; then
	# the remote tests for biber ...
	# T regexes are for reporting or commenting main tests,
	# so not applicable here
	# R regexes enable these if biber is present, else comment them
	REM1=
	REM2=
else
	REM1='#'
	REM2='#'
fi

echo "RBLUA is $RBLUA, TBLUA is $TBLUA"				

echo "creating Makefile"
cat Makefile.in | sed \
 -e "s/@TBID@/$TBID/" -e "s/@RBID@/$RBID/g" \
 -e "s/@TBIX@/$TBIX/" -e "s/@RBIX@/$RBIX/g" \
 -e "s/@TBLC@/$TBLC/" -e "s/@RBLC@/$RBLC/g" \
 -e "s/@TBLR@/$TBLR/" -e "s/@RBLR@/$RBLR/g" \
 -e "s/@TBLT@/$TBLT/" -e "s/@RBLT@/$RBLT/g" \
 -e "s/@TBLU@/$TBLU/" -e "s/@RBLU@/$RBLU/g" \
 -e "s/@TBTX@/$TBTX/" -e "s/@RBTX@/$RBTX/g" \
 -e "s/@TCYL@/$TCYL/" -e "s/@RCYL@/$RCYL/g" \
 -e "s/@TBLUA@/$TBLUA/" -e "s/@RBLUA@/$RBLUA/g" \
 -e "s/@TDVI@/$TDVI/" -e "s/@RDVI@/$RDVI/g" \
 -e "s/@TJLA@/$TJLA/" -e "s/@RJLA@/$RJLA/g" \
 -e "s/@TLIG@/$TLIG/" -e "s/@RLIG@/$RLIG/g" \
 -e "s/@TLUA@/$TLUA/" -e "s/@RLUA@/$RLUA/g" \
 -e "s/@TMOS@/$TMOS/" -e "s/@RMOS@/$RMOS/g" \
 -e "s/@TOJA@/$TOJA/" -e "s/@ROJA@/$ROJA/g" \
 -e "s/@TPDF@/$TPDF/" -e "s/@RPDF@/$RPDF/g" \
 -e "s/@TPAR@/$TPAR/" -e "s/@RPAR@/$RPAR/g" \
 -e "s/@TPRS@/$TPRS/" -e "s/@RPRS@/$RPRS/g" \
 -e "s/@TPRT@/$TPRT/" -e "s/@RPRT@/$RPRT/g" \
 -e "s/@TTRI@/$TTRI/" -e "s/@RTRI@/$RTRI/g" \
 -e "s/@TXEL@/$TXEL/" -e "s/@RXEL@/$RXEL/g" \
 -e "s/@TXIN@/$TXIN/" -e "s/@RXIN@/$RXIN/g" \
 -e "s/@TXNL@/$TXNL/" -e "s/@RXNL@/$RXNL/g" \
 -e "s/@REM1@/$REM1/g" -e "s/@REM2@/$REM2/g" \
 -e "s%@PARFILE@%$PARFILE%" \
 >Makefile

echo "latex-test $VERSION is now configured for your system."
if [ -n "$BADPARM" ]; then
	echo "unrecognized parameter\(s\) $BADPARM"
fi
# finish by noting if some things will not be run
if [ $WARNINGS -eq 1 ]; then
	echo "There was $WARNINGS warning"
elif [ $WARNINGS -ne 0 ]; then
	echo "There were $WARNINGS warnings"
fi
if [ $WARNINGS -ne 0 ] || [ -n "$BADPARM" ]; then
	echo "please review before continuing"
else
	echo "you can now run 'make'"
fi

# vim: ts=4
