#! /bin/sh -
### ========================================================================
### Build asymptote in a TeX Live YYYY tree after a successful build of
### other components (required order, because asymptote needs -lkpathsea).
###
### Usage:
###	cd /local/build
###	[env CC='...' C'...'=xx CFLAGS='...' CXXFLAGS='...' YEAR=YYYY] \
###		$S/build-asymptote.sh texlive-YYYYMMDD [ configure-options ]
###
### The YEAR [default: current year] value should lead to a suitable
### $prefix/skel/SYS.texlive-$YEAR.sh file
### [26-Apr-2016]
### ========================================================================

IFS='
	 '

YEAR=${YEAR-`date +%Y`}

prefix=${prefix-/usr/uumath}
test -d $prefix/bin || prefix=/usr/local

unset BIBINPUTS CONFIG_SITE DVIPSHEADERS MFINPUTS TEX TEXCONFIG TEXFONTS TEXFORMATS TEXINPUTS TEXPKS VFFONTS XDVIFONTS

unalias amslatex amstex latex latex209 latex2e pdflatex pdftex tex 2>/dev/null

PATH=/bin

### Add vendor-specific package directories, but exclude our local directories
for d in /usr/bin /usr/pkg/bin /usr/local/bin /opt/csw/bin
do
    test "$d" = "$prefix/bin" && continue
    test -d $d && PATH=$PATH:$d
done

### For some reason, on GNU/Hurd, g++ is not installed under that
### name, but rather, only with an attached version number.  We have a
### symlink to a suitable version in our local directory.

test "`uname -s`" = GNU && PATH=$PATH:$prefix/bin

export PATH

test -f $prefix/skel/SYS.texlive-$YEAR.sh && . $prefix/skel/SYS.texlive-$YEAR.sh

echo
echo PATH = $PATH
echo

MAKE=
TEX=

for f in gnumake gmake make
do
    for d in `eval echo $PATH | tr : ' '`
    do
	test -z "$MAKE" -a -x "$d/$f"  && MAKE=$d/$f
	test -z "$TEX"  -a -x "$d/tex" && TEX=$d/tex
    done
done

test -z "$MAKE" && echo ERROR: cannot find suitable make utility && exit 1
test -z "$TEX"  && echo ERROR: cannot find suitable TeX          && exit 1

tlbasename=$1
test ! -d "$1" && echo "Usage: $0 texliveYYYYMMDD [ configure-options ]" && exit 1
shift

asydir=$tlbasename/source/utils/asymptote

GSVER=`gs --version`
case "$GSVER" in
    [2-8]*)	ASYMPTOTE_EPSDRIVER=epswrite ;;
    9.0*)	ASYMPTOTE_EPSDRIVER=epswrite ;;
    9.1[0-3]*)	ASYMPTOTE_EPSDRIVER=epswrite ;;
    *)		ASYMPTOTE_EPSDRIVER=eps2write ;;
esac

export ASYMPTOTE_EPSDRIVER

: ${tlmakeflags=-j8}
CC=${CC-gcc}
CXX=${CXX-g++}
CFLAGS=${CFLAGS-}
CXXFLAGS=${CXXFLAGS-}

echo
echo "asydir              = $asydir"
echo "ASYMPTOTE_EPSDRIVER = $ASYMPTOTE_EPSDRIVER"
echo "CC                  = $CC"
echo "CFLAGS              = $CFLAGS"
echo "CXX                 = $CXX"
echo "CXXFLAGS            = $CXXFLAGS"
echo "GSVER               = $GSVER"
echo "MAKE                = $MAKE"
echo "PATH                = $PATH"
echo "TEX                 = $TEX"
echo

if test -d $asydir
then
    cd $asydir
    svn update || true
    test -d .git && mv .git dot.git
    CC="${CC}" CXX="$CXX" CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" \
	./configure --prefix=/tmp/asyinst --enable-texlive-build --disable-gsl --disable-fftw "$@"
    $MAKE $tlmakeflags
    $MAKE $tlmakeflags check
    test -d dot.git && mv dot.git .git
    strip asy || true
    ls -l asy xasy
    echo "Now do: cp `pwd`/asy $prefix/texlive/YYYY/bin/CPU-OS-VERSION/"
    echo "and make symlink there: ln -s ../../texmf-dist/asymptote/GUI/xasy.py xasy"
else
    echo ERROR: $asydir does not exist
    exit 1
fi
