#! /bin/sh

CONFIGURE_VERSION=0.2

PREFIX="/usr/local"
lisp=clisp
lisp_opt=''
lisp_bin=''
dump_path="\$XDG_CACHE_HOME/clfswm/"
clfswm_asd_path="$PREFIX/lib/clfswm"
asdf_path="$PREFIX/lib/clfswm/contrib"

usage () {
    echo "'configure' configures clfswm to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

Defaults for the options are specified in brackets.

Configuration:
  -h, --help                display this help and exit
  -V, --version             display version information and exit
  --prefix=PREFIX           install architecture-independent files in PREFIX [/usr/local]
  -l, --with-lisp           use <lisp> as the common lisp implementation type [$lisp]
  -b, --lisp-bin            use <bin> as the common lisp program [$lisp_bin] (default: same as with-lisp type)
  -o, --lisp-opt            use <opt> as lisp option [$lisp_opt]
  -d, --dump-path           path to the dump directory [$dump_path]
  --with-clfswm             path to clfswm.asd file [$clfswm_asd_path]
  --with-asdf               path to the asdf.lisp file [$asdf_path]

By default, 'make install' will install all the files in
'/usr/local/bin', '/usr/local/lib' etc.  You can specify
an installation prefix other than '/usr/local' using '--prefix',
for instance '--prefix \$HOME/clfswm'."
    exit 0
}


version () {
    echo "Configure version: $CONFIGURE_VERSION"
    exit 0
}

reset_clfswm_asd_path=true
reset_asdf_path=true

while test $# != 0
do
    case "$1" in
	--prefix)
	    shift
	    PREFIX="$1" ;;
	-d|--dump-path)
	    shift
	    dump_path="$1" ;;
	--with-clfswm)
	    shift
	    clfswm_asd_path="$1"
	    reset_clfswm_asd_path=false ;;
	--with-asdf)
	    shift
	    asdf_path="$1"
	    reset_asdf_path=false ;;
	-l|--with-lisp)
	    shift
	    case "$1" in
		'')
		    usage;;
		clisp|sbcl|cmucl|ccl|ecl)
		    lisp="$1" ;;
	    esac
	    ;;
        -b|--lisp-bin)
            shift
            lisp_bin="$1" ;;
	-o|--lisp-opt)
	    shift
	    lisp_opt="$1" ;;
	--)
	    shift
	    break ;;
	*)
	    usage ;;
    esac
    shift
done


DESTDIR=$PREFIX

if [ "$reset_clfswm_asd_path" = "true" ]; then
    clfswm_asd_path="$PREFIX/lib/clfswm"
fi

if [ "$reset_asdf_path" = "true" ]; then
    asdf_path="$PREFIX/lib/clfswm/contrib"
fi


echo "  prefix=$PREFIX
  with-lisp=$lisp
  lisp-bin=$lisp_bin
  lisp-opt=$lisp_opt
  dump-path=$dump_path
  with-clfswm=$clfswm_asd_path
  with-asdf=$asdf_path"



sed -e "s?^lisp=.*# +config+?lisp=\"$lisp\"   # +config+?g" \
    -e "s?^lisp_bin=.*# +config+?lisp_bin=\"$lisp_bin\"   # +config+?g" \
    -e "s?^lisp_opt=.*# +config+?lisp_opt=\"$lisp_opt\"   # +config+?g" \
    -e "s?^dump_path=.*# +config+?dump_path=\"$dump_path\"   # +config+?g" \
    -e "s?^clfswm_asd_path=.*# +config+?clfswm_asd_path=\"$clfswm_asd_path\"   # +config+?g" \
    -e "s?^asdf_path=.*# +config+?asdf_path=\"$asdf_path\"   # +config+?g" \
    $(pwd)/contrib/clfswm > $(pwd)/clfswm

sed -e "s#+DESTDIR+#$DESTDIR#g" \
    -e "s#+BUILD_PATH+#$(pwd)/#g" \
    Makefile.template > Makefile


echo ""
echo "Type 'make' to build clfswm"
echo ""
