#!/bin/sh

#
# This file is part of Bisect.
# Copyright (C) 2008-2012 Xavier Clerc.
#
# Bisect 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.
#
# Bisect 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 ''`
native_dynlink='TRUE'
devel='FALSE'
no_camlp4='FALSE'
ppx='FALSE'

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

# sanity check
if [ "$no_camlp4" = "TRUE" -a "$ppx" = "FALSE" ]; then
  echo 'At least one instrumenter should be chosen (either camlp4 or ppx)'
  exit 1
fi

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

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