#!/bin/sh
# usage: configure gappath
# this script creates a `Makefile' from `Makefile.in'

set -e

DEFAULT_GAPPATH=../../..

if test -z "$1"; then
  GAPPATH=${DEFAULT_GAPPATH}; echo "Using ${DEFAULT_GAPPATH} as default GAP path";
else
  GAPPATH=$1;
fi

if test ! -r $GAPPATH/sysinfo.gap ; then
    echo
    echo "No file $GAPPATH/sysinfo.gap found."
    echo
    echo "Usage: ./configure [GAPPATH]"
    echo "       where GAPPATH is a path to your GAP installation"
    echo "       (The default for GAPPATH is \"${DEFAULT_GAPPATH}\")"
    echo
    echo "Either your GAPPATH is incorrect or the GAP it is pointing to"
    echo "is not properly compiled (do \"./configure && make\" there first)."
    echo
    echo Aborting... No Makefile is generated.
    echo
    exit 1
fi

echo "Using config in $GAPPATH/sysinfo.gap"

. "$GAPPATH/sysinfo.gap"

if test "x$JULIA_LIBS" = "x" ; then
    echo "JULIA_LIBS not set in sysinfo.gap. Maybe your GAP version is too old"
    exit 1
fi

sed \
    -e "s;@GAPARCH@;$GAParch;g" \
    -e "s;@GAPPATH@;$GAPPATH;g" \
    -e "s;@JULIA_LDFLAGS@;$JULIA_LDFLAGS;g" \
    -e "s;@JULIA_LIBS@;$JULIA_LIBS;g" \
    Makefile.in > Makefile
