#!/bin/bash -x
##
##   BISMON  ---- its Configure script ---- see github.com/bstarynk/bismon/
##   Copyright © 2018 - 2022 CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
##   contributed by Basile Starynkevitch (working at CEA, LIST, France)
##   <basile@starynkevitch.net> or <basile.starynkevitch@cea.fr>
##
##   This program 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.
##
##   This program 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/>.
##
##   ----
##   Contact me (Basile Starynkevitch) by email
##   basile@starynkevitch.net and/or basile.starynkevitch@cea.fr
########################################

# the C++ and C compilers; some GCC (see http://gcc.gnu.org/ ...)
if [ -z "$CXX" ]; then
    CXX=g++
fi

if [ -z "$CC" ]; then
    CC=gcc
fi

# the short git id.... 
BISMONCONF_SHGIT1=$(git log --format=oneline -q -1 | cut '-d '  -f1 | tr -d '\n' | head -c16)
BISMONCONF_SHGIT2=$(if git status | grep 'nothing to commit' > /dev/null; then echo; else echo +; fi)
BISMONCONF_SHORT_GIT="$BISMONCONF_SHGIT1$BISMONCONF_SHGIT2"
# Now BISMONCONF_SHORT_GIT could be a string like 6f60bfd0a2dad6bd+ if the
# git repository is dirty, and like 9f60bfd0a2dad6bd if it is clean of
# local changes.

if [ "$1" == "--version" ]; then
    printf "%s: version gitid %s\n" $(realpath $0) $BISMONCONF_SHORT_GIT
    exit 0
fi

BISMON_CONFIGURE_REAL_PATH=$(realpath $0)
BISMON_CONFIGURE_DIR=$(dirname $BISMON_CONFIGURE_REAL_PATH)
$BISMON_CONFIGURE_DIR/timestamp-emit.sh __timestampconf.c
if [ -f BISMON-config ]; then
    /bin/mv -vf BISMON-config BISMON-config~
fi

printf "%s: our compiler for C is %s\n" $0 $CC
$CC --version || exit 1
echo

printf "%s: our compiler for C++ is %s\n" $0 $CXX
$CXX --version || exit 1
echo

$CC -O -Wall -c __timestampconf.c || (printf "%s failed to compile __timestampconf.c\n" > /dev/stderr; exit 1)

/bin/mv -f __timestampconf.c __timestampconf.c~
    
if $CXX -Wall -Wextra -O -g -DBISMON_SHORTGIT=\"$BISMONCONF_SHORT_GIT\" \
     -DBISMON_CONFIG_LABEL=\"from-$BISMON_CONFIGURE_REAL_PATH\" \
     BISMON-config.cc  __timestampconf.o -o BISMON-config -lreadline ; then
    printf "%s: successfully build %s\n" "$0" $(realpath BISMON-config)
else
    printf "%s: failed to build BISMON-config in %s\n" "$0" $(pwd) > /dev/stderr
    exit 1
fi


if [ "$1" == "--dry" -a -x BISMON-config ]; then
    printf "%s: dry configuration, did build BISMON-config but do not run it in %s\n" "$0" $(pwd)
    exit 0
fi

exec ./BISMON-config "$@"


### end of Configure script in github.com/bstarynk/bismon/
