#!/bin/sh

# FIXME: this should all be replaced by "make install" eventually,
# as part of proper integration with the TeX Live build

# script to install xetex (and xdv2pdf on OS X)
# from the binaries created by build-xetex

# this is intended for the Linux and *BSD version;
# on OS X, we build installer packages

# to be run from the top level dir of the xetex tree, after ./build-xetex

# set -o xtrace

echo "###### INSTALLING XETEX"

filelist=Work/xetex-installed-files

if [ -f ${filelist} ]; then rm ${filelist}; fi

if [ ! -e Work/texk/web2c/xetex ]; then
	echo "### error: xetex binary has not been built"
	exit 100
fi

# find the existing tex binary, possibly following a symlink
texbin=`which tex`
if [ ! -x "${texbin}" ]; then
	echo "### error: existing tex binary couldn't be found"
	exit 100
else
	echo "### TeX binary = ${texbin}"
fi
if [ -L ${texbin} ]; then
	texbin=`readlink ${texbin}`
	echo "### TeX binary (${texbin}) seems to be symlinked."
fi
texbindir=`dirname ${texbin}`
echo "### TeX bin directory = ${texbindir}"

# copy the xetex binary to the same dir as standard tex
cp -pf Work/texk/web2c/xetex ${texbindir}/xetex
echo ${texbindir}/xetex >> ${filelist}

if [ -e Work/texk/xdv2pdf/xdv2pdf ]; then
	programs="xdv2pdf T1Wrap"
	for f in ${programs}; do
		cp -pf Work/texk/xdv2pdf/${f} ${texbindir}/${f}
		echo ${texbindir}/${f} >> ${filelist}
	done
fi

if [ -e Work/texk/xdvipdfmx/src/xdvipdfmx ]; then
	cp -pf Work/texk/xdvipdfmx/src/xdvipdfmx ${texbindir}/xdvipdfmx
	echo ${texbindir}/xdvipdfmx >> ${filelist}
fi

# copy our texmf additions into the local texmf tree
texmflocal=`kpsewhich --var-value TEXMFLOCAL`
if [ -z "${texmflocal}" ]; then
	# if --var-value didn't work, try to find the definition in the texmf.cnf file
	CNF=`kpsewhich texmf.cnf`
	texmflocal=`grep '^[ \t]*TEXMFLOCAL[ \t]*=[ \t]*/' ${CNF}`
	if [ -z "${texmflocal}" ]; then
		echo "### error: unable to find usable TEXMFLOCAL definition"
		exit 100
	fi
	texmflocal=`echo ${texmflocal} | awk -F "=[ \t]*" '{print $2}'`
fi
# Figuring out whether texmflocal contains symlinks (as we did in
# runConfigure.sh and build-xetex) is not necessary here, since this
# path is used just for mechanical copying, _except_ for texhash.
# Does texhash's behaviour differ depending on whether the path it
# uses respects symlinks?
# 
# What fmtutil does depends however on kpsewhich, which is beyond
# the scope of this installation script.
echo "### Using TEXMFLOCAL = ${texmflocal}"

mkdir -p ${texmflocal}
if [ -d texmf/.svn ]; then
	# if we're installing from a Subversion WC, make a copy and clean it up
	# (don't rely on svn export here in case svn isn't in the current PATH)
	cp -pRf texmf Work/texmf-nosvn
	find Work/texmf-nosvn -depth \( -name ".svn" -or -name ".DS*" \) -exec rm -rf {} \;
	cp -pRf Work/texmf-nosvn/* ${texmflocal}/
	rm -r Work/texmf-nosvn
else
	cp -pRf texmf/* ${texmflocal}/
fi
find texmf -type f -and -not -path "*/.*" -print | sed -e "s@^texmf@${texmflocal}@" >> ${filelist}

# update kpathsearch databases because we've installed a bunch of stuff
echo "### Running texhash"
texhash ${texmflocal}

# this will build the format files on the system where this script is run;
# for packaged binary installations, we must repeat this on the target

sh ./rebuild-formats
echo "### All done. You installed xetex successfully. Newly installed files are:"
cat ${filelist}
echo "### Output of 'xetex --version:'"
xetex --version

echo "### NOTE: If you have locally installed xe(la)tex.fmt"
echo "### (~/.texlive2007/texmf-var/web2c/xetex/ is TeXLive's"
echo "### default directory), then you can expect the following"
echo "### error when run your new xetex for the first time:"
echo "###     [...]/xelatex.fmt doesn't match xetex.pool"
echo "###     (Fatal format file error; I'm stymied)"
echo "### You should delete the old .fmt files."
# Is installing new local .fmt's using fmtutil --enablefmt/--byfmt necessary?
