#!/bin/bash
#
# configure
#
# Copyright (C) 2017-2018 Matthew R. Wette
#
# Copying and distribution of this file, with or without modification
# is granted.  This file is offered as-is, without any warranty.

nyacc_version=0.86.0

help() {
    echo "'configure' sets up the top-level Makefile for your local guile"
    echo " "
    echo "Usage: ./configure [OPTION]"
    echo "       GUILE=/path/to/guile ./configure [OPTION]"
    echo
    echo "Configuration options are:"
    echo "  -h --help            show options and quit"
    echo "  -V --version         version information"
    echo "  --guile=<guile>      guile executable"
    echo "  --guild=<guild>      guild executable"
    echo "  --prefix             guile-relative prefix as usual"
    echo "  --site-scm-dir       where to install site-local scm files"
    echo "  --site-scm-go-dir    where to install site-local go files"
    echo "  --infodir            where to install info files"
    echo " "
    echo "The guile-relative paths are:"
    echo "  site_scm_dir=\$prefix/share/guile/site/\$EV"
    echo "  site_scm_go_dir=\$prefix/share/lib/guile/\$EV/site-ccache"
    echo "  infodir=\$prefix/share/info"
    echo "where \$EV is the effective version (e.g., 2.2)"
    echo " "
}

prefix=
site_scm_dir=
site_scm_go_dir=
infodir=

while [ "$*" ]; do
    case $1 in
	-h|--help)
	    help
	    exit 0
	    ;;
	-V|--version)
	    echo "nyacc version ${nyacc_version}"
	    exit 0
	    ;;
        --guile=*)
	    guile=`echo $1 | sed -e 's/^--guile=//'`
	    ;;
        --guild=*)
	    guild=`echo $1 | sed -e 's/^--guild=//'`
	    ;;
	--prefix=*)
	    prefix=`echo $1 | sed -e 's/^--prefix=//'`
	    ;;
	--site_scm_dir=*)
	    site_scm_dir=`echo $1 | sed -e 's/^--site_scm_dir=//'`
	    ;;
	--site_scm_go_dir=*)
	    site_scm_go_dir=`echo $1 | sed -e 's/^--site_scm_go_dir=//'`
	    ;;
	--site-scm-dir=*)
	    site_scm_dir=`echo $1 | sed -e 's/^--site-scm-dir=//'`
	    ;;
	--site-scm-go-dir=*)
	    site_scm_go_dir=`echo $1 | sed -e 's/^--site-scm-go-dir=//'`
	    ;;
	--infodir=*)
	    infodir=`echo $1 | sed -e 's/^--infodir=//'`
	    ;;
	--*)
	    echo "configure: ignoring $1"
 	    ;;
    esac
    shift
done

if [ "X$guile" != "X" ]; then
    if [ "X$guild" = "X" ]; then
        echo "warning: guile specified but not guild"
    fi
    GUILE=$guile
elif [ "X$GUILE" = "X" ]; then
    GUILE=guile
else
    if [ "X$GUILD" = "X" ]; then
        echo "warning: GUILE specified but not GUILD"
    fi
fi
if [ "X$guild" != "X" ]; then
    GUILD=$guild
elif [ "X$GUILD" = "X" ]; then
    GUILD=guild
fi

if [ "X$site_scm_dir" = "X" ]; then
    if [ "X$prefix" = "X" ]; then
	SITE_SCM_DIR=`$GUILE -c '(display (%site-dir))'`
    else
	EFF_VER=`$GUILE -c "(display (effective-version))"`
	SITE_SCM_DIR=$prefix/share/guile/site/$EFF_VER
    fi
else
    SITE_SCM_DIR=$site_scm_dir
fi

if [ "X$site_scm_go_dir" = "X" ]; then
    if [ "X$prefix" = "X" ]; then
	SITE_SCM_GO_DIR=`$GUILE -c '(display (%site-ccache-dir))'`
    else
	EFF_VER=`$GUILE -c "(display (effective-version))"`
	SITE_SCM_GO_DIR=$prefix/lib/guile/$EFF_VER/site-ccache
    fi
else
    SITE_SCM_GO_DIR=$site_scm_go_dir
fi

if [ "X$infodir" = "X" ]; then
    if [ "X$prefix" = "X" ]; then
	INFODIR=`$GUILE -c "(display (assq-ref %guile-build-info 'infodir))"`
    else
	INFODIR=$prefix/share/info
    fi
else
    INFODIR=$infodir
fi

echo "creating Makefile"
sed -e "s|@prefix@|$prefix|" \
    -e "s|@guile@|$GUILE|" \
    -e "s|@guild@|$GUILD|" \
    -e "s|@site_scm_dir@|$SITE_SCM_DIR|" \
    -e "s|@site_scm_go_dir@|$SITE_SCM_GO_DIR|" \
    -e "s|@infodir@|$INFODIR|" \
    < Makefile.in > Makefile
sleep 1

# --- last line ---
