#!/bin/sh

#
# This file is part of Barista.
# Copyright (C) 2007-2014 Xavier Clerc.
#
# Barista is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Barista is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# default values
ocamlbuild=`which ocamlbuild || echo '/usr/local/bin/ocamlbuild'`
bin_path=`dirname $ocamlbuild`
ocaml_prefix=`dirname $bin_path`
ocamlfind=`which ocamlfind 2> /dev/null || echo ''`
devel='FALSE'

# command-line analysis
while [ $# -gt 0 ]
do
    case "$1" in
        -ocaml-prefix)
            ocaml_prefix="$2"; shift;;
        -ocamlfind)
            ocamlfind="$2"; shift;;
        -devel)
            devel='TRUE';;
        *)
            echo "usage: $0 [-ocaml-prefix <path>] [-ocamlfind <path>] [-devel]";
            exit 1;;
        esac
        shift
done

# make options
make_quiet=`make -f - <<EOF
default: gnumake
	@if [ "$^" != "" ]; then echo 'GNU MAKE DETECTED'; fi
gnumake:
EOF`
if [ -n "`echo $make_quiet | grep 'GNU MAKE DETECTED'`" ]; then
    make_quiet='--no-print-directory'
else
    make_quiet=''
fi

# file creation
echo "# timestamp: `date`" > Makefile.config
echo "PATH_OCAML_PREFIX=$ocaml_prefix" >> Makefile.config
echo "PATH_OCAMLFIND=$ocamlfind" >> Makefile.config
echo "WARNINGS=$devel" >> Makefile.config
echo "MAKE_QUIET=$make_quiet" >> Makefile.config
echo "" >> Makefile.config
echo 'Makefile.config successfully created'
