#!/bin/sh

## from lily's aclocal

function get_version ()
{
    ##set -x
    ## Assume and hunt for dotted version multiplet.
    ## use eval trickery, because we cannot use multi-level $() instead of ``
    ## for compatibility reasons.
    ## FIXME: what systems still do not have $() in /bin/sh?
    eval _ver=\"`("$1" --version || "$1" -V) 2>&1 | grep '[0-9]\.[0-9]' \
        | head -n 1 \
	| sed -e 's/.*[^-.0-9]\([0-9][0-9]*\.[0-9][.0-9]*\).*/\1/' \
	    -e 's/^[^.0-9]*//' -e 's/[^.0-9]*$//'`\"
    if test -z "$_ver"; then
        ## If empty, try date [fontforge]
        eval _ver=\"`("$1" --version || "$1" -V) 2>&1 | grep '[0-9]\{6,8\}' \
	    | head -n 1 \
	    | sed -e 's/^[^.0-9]*//' -e 's/[^.0-9]*$//'`\"
    fi
    echo "$_ver"
}

function numeric_verion ()
{
    echo "$1" | awk -F. '
    {
      if ($3) {three = $3}
      else {three = 0}
    }
    {printf "%.0f\n", $1*1000000 + $2*1000 + three}'
}

mftrace_required=1.1.1
echo -n "checking mftrace version... "  1>&2
mftrace_version=$(get_version mftrace)
echo -e $mftrace_version 1>&2
if [ $(numeric_verion "$mftrace_version") \
    -lt $(numeric_verion "$mftrace_required") ]; then
    echo need mftrace $mftrace_required or newer 1>&2
    exit 1
fi


#ugr, hack up --srcdir support

if [ ! -f configure ]; then
    # --scrdir build
    srcdir=$(cd $(dirname $0) && pwd)
    echo -e "creating GNUmakefile... " 1>&2
    cat > GNUmakefile <<EOF
builddir=$(pwd)
srcdir=$srcdir
export SRCMAKE=\$(MAKE) -C \$(srcdir) -f GNUmakefile builddir=\$(builddir)

default: all

%:
	\$(SRCMAKE) \$@
EOF
    echo done 1>&2
fi

cat <<EOF

Now do

    make all
    make [DESTDIR=...] install
    mktexlsr
    updmap --enable Map ec-mftrace.map

Compiling ec-fonts-mftraced takes a lot of resources.
Consider using a binary package, see INSTALL.
EOF