#!/bin/sh

usage () {
cat<<EOF
usage: configure [ <option> ... ]

where '<option>' is one of the following

  -c    enable assertion checking (implied by '-g')
  -g    debugging build (implies '-s' and '-c')
  -h    print this command line option summary
  -l    compile with logging support (implied by '-g')
  -s    compile with symbols (implied by '-g')

  --coverage   include code to generate coverage with 'gcov'
  --sanitize   sanitize for memory access and undefined behavior

and without any option full optimization is assumed.
EOF
}

debug=no

check=unknown
coverage=no
logging=unknown
sanitize=no
symbols=unknown

while [ $# -gt 0 ]
do
  case $1 in
    -c) check=yes;;
    -g) debug=yes;;
    -h) usage; exit 0;;
    -l) logging=yes;;
    -s) symbols=yes;;
    --coverage) coverage=yes;;
    --sanitize) sanitize=yes;;
    *) echo "configure: error: invalid option '$1'" 1>&2; exit 1;;
  esac
  shift
done

COMPILE="gcc -Wall"

[ $check = unknown ] && check=$debug
[ $logging = unknown ] && logging=$debug
[ $symbols = unknown ] && symbols=$debug

[ $symbols = yes ] && COMPILE="$COMPILE -g -ggdb3"
[ $coverage = yes ] && COMPILE="$COMPILE -ftest-coverage -fprofile-arcs"
[ $sanitize = yes ] && COMPILE="$COMPILE -fsanitize=address,undefined"
[ $debug = no ] && COMPILE="$COMPILE -O3"
[ $coverage = yes ] && COMPILE="$COMPILE -DCOVERAGE"
[ $logging = yes ] && COMPILE="$COMPILE -DLOGGING"
[ $check = no ] && COMPILE="$COMPILE -DNDEBUG"

echo "configure: using '$COMPILE' for compilation"
sed -e "s#@COMPILE@#$COMPILE#" makefile.in > makefile
