# These mess with just-installed binaries. Should be fixed properly.
rm -rf "${SOURCE_DIRECORY}/old.libraries" &&
cd ${COMPILE_DIRECTORY} &&
default_install  &&

# move old local to new local, can eventually get rid of this
if [ -d "/usr/share/texmf-local" ]; then
  mv ${INSTALL_ROOT}/usr/share/texmf-local ${INSTALL_ROOT}/usr/local/share/texmf
fi &&

ln -svf mf ${INSTALL_ROOT}/usr/bin/mf-nowin &&

fmtutilcnf="$INSTALL_ROOT"/usr/share/texmf-dist/web2c/fmtutil.cnf
# don't create aleph files
sed -i -e '/aleph/d' "$fmtutilcnf" &&

# remove luatex stuff from config file if not enabled
if ! is_depends_enabled $SPELL LUA; then
  sed -i -e '/luatex/d' "$fmtutilcnf"
fi &&

# create symlinks to latex, pdflatex, etc
${INSTALL_ROOT}/usr/bin/texlinks -f "$fmtutilcnf" ${INSTALL_ROOT}/usr/bin &&

# updmap and fmtutil-sys are broken without an install of TexLive::TLUtils
# This comes with the installer, which we don't use, one source:
# ftp://tug.org/texlive/historic/2012/tlnet-final/tlpkg/
# Now, there happens to be a copy here, too ... just using it.
# Rationale for the hack here: This spell installs updmap, so it should
# damn well also ensure that this tool can run.
# Proper use would install tlpkg, which we don't use, just for that lib.
mkdir -p "$INSTALL_ROOT/usr/share/tlpkg" &&
cp -vdR "$SOURCE_DIRECTORY/texk/tests/TeXLive" \
	"$INSTALL_ROOT/usr/share/tlpkg" &&

# update filename db & create all formats
${INSTALL_ROOT}/usr/bin/mktexlsr &&
if ! ${INSTALL_ROOT}/usr/bin/fmtutil-sys --cnffile "$fmtutilcnf" --fmtdir ${INSTALL_ROOT}/usr/share/texmf-dist/web2c --all; then
  message "${ERROR_COLOR}Failure of fmtutil might just mean that your texmf is out of date.$DEFAULT_COLOR"
  exit 1
fi &&

# allow fontconfig to see latex fonts
mkdir -p "${INSTALL_ROOT}/etc/fonts/conf.avail" &&
install -Cv ${SPELL_DIRECTORY}/09-texlive-fonts.conf ${INSTALL_ROOT}/etc/fonts/conf.avail &&
ln -fs ${INSTALL_ROOT}/etc/fonts/conf.avail/09-texlive-fonts.conf ${INSTALL_ROOT}/etc/fonts/conf.d &&

if is_depends_enabled $SPELL LUA; then
  # update filename db & create formats for context
  ${INSTALL_ROOT}/usr/bin/mtxrun --generate &&
  # generate fonts for luatex & context
  OSFONTDIR="${INSTALL_ROOT}/usr/share/texmf-dist/fonts/;${INSTALL_ROOT}/usr/share/texmf/fonts/;${INSTALL_ROOT}/usr/local/share/texmf/fonts" ${INSTALL_ROOT}/usr/bin/mtxrun --script fonts --reload 
fi &&

# Fixup for really stupid fuckup. Newer versions should have this fixed. Please.
if test -e "$INSTALL_ROOT/usr/lib/kpathsea/c-auto.h"; then
  ln -s ../../lib/kpathsea/c-auto.h "$INSTALL_ROOT/usr/include/kpathsea/c-auto.h"
fi
