#!/usr/bin/env bash

DIR="$( cd "$( dirname "${0}" )" && pwd )"
TMP_TARGET_FILE_NAME="$(mktemp Makefile.$(date +"%Y-%m-%d_%T_XXXXXX"))"
TARGET_FILE_NAME=Makefile

# Configurable options
prefix=/usr/local
debugsym=false
sanitizer=false
coverage=false
profile=false
pkgconfig=false
generator=Ninja

# Figure out project name:
project_name=$(basename $DIR)
if [ -f "PROJECT.name" ]; then
    project_name="$(cat 'PROJECT.name')"
else
    echo "Warning: no file 'PROJECT.name' found, guessing project name: '${project_name}'"
fi


for arg in "$@"; do
    case "$arg" in
    --prefix=*)
        prefix=`echo $arg | sed 's/--prefix=//'`
        ;;

    --conan-profile=*)
        conan_profile=`echo $arg | sed 's/--conan-profile=//'`
        # Check selected profile actually exist:
        if ! $(conan profile list | grep -q $conan_profile); then
            echo "Error: selected conan profile '$conan_profile' does not exist. valid local profiles are:" 1>&2;
            conan profile list 1>&2;
            exit 1
        fi
        ;;
    --generator=*)
        generator=`echo $arg | sed 's/--generator=//'`
        ;;

    --enable-debug )
        debugsym=true
        ;;
    --disable-debug )
        debugsym=false
        ;;

    --enable-sanitizer )
        sanitizer=true
        ;;
    --disable-sanitizer )
        sanitizer=false
        ;;

    --enable-coverage )
        coverage=true
        ;;
    --disable-coverage )
        coverage=false
        ;;

    --enable-profile )
        profile=true
        ;;
    --disable-profile )
        profile=true
        ;;

    --enable-pkgconfig )
        pkgconfig=true
        ;;
    --disable-pkgconfig )
        pkgconfig=true
        ;;

    --help)
        echo 'usage: ./configure [options]'
        echo 'options:'
        echo '  --prefix=<path>: installation prefix'
        echo '  --generator=<generator>: CMake generator'
        echo '  --conan-profile=<profile>: Use custom conan profil'
        echo '  --enable-debug include debug symbols'
        echo '  --disable-debug do not include debug symbols'
        echo '  --enable-sanitizer To enable -fsanitize compiler option'
        echo '  --disable-sanitizer To disable -fsanitize compiler option'
        echo '  --enable-coverage To enable compiler coverage option'
        echo '  --disable-coverage To disable compiler coverage option'
        echo '  --enable-profile To enable compiler profiler info generation'
        echo '  --disable-profile To disable compiler profiler info generation'
        echo '  --enable-pkgconfig To include generated .PC library descriptor when installing'
        echo '  --disable-pkgconfig To exclude generated .PC library descriptor when installing'
        echo ''
        echo 'all invalid options are silently ignored'
        exit 0
        ;;

    *)
        echo "Unexpected option '$arg'"
        exit 0
        ;;
    esac
done

echo "Generating Makefile for ${project_name}..."
echo "# Generated Makefile for ${project_name}: $(date)" >> "${TMP_TARGET_FILE_NAME}"
echo "PREFIX ?= ${prefix}" >> "${TMP_TARGET_FILE_NAME}"
echo "PROJECT ?= ${project_name}" >> "${TMP_TARGET_FILE_NAME}"
echo "GENERATOR ?= ${generator}" >> "${TMP_TARGET_FILE_NAME}"


if $debugsym; then
    echo 'dbg = -g' >> "${TMP_TARGET_FILE_NAME}"
fi

if $sanitizer; then
    echo 'sanitize = address,undefined,leak' >> "${TMP_TARGET_FILE_NAME}"
fi

if $coverage; then
    echo 'coverage = -coverage' >> "${TMP_TARGET_FILE_NAME}"
fi

if $profile; then
    echo 'profile = -pg' >> "${TMP_TARGET_FILE_NAME}"
fi

if $pkgconfig; then
  echo 'PKG_CONFIG = ON' >> "${TMP_TARGET_FILE_NAME}"
fi

if [ ! -z "$conan_profile" ] ; then
    echo "CONAN_PROFILE ?= ${conan_profile}" >> "${TMP_TARGET_FILE_NAME}"
fi


if [ ! -z "${CXX}" ] || [ ! -z "${CC}" ] ; then
    echo "Using custom copiler: CXX=$CXX CC=$CC"
    printf "\n# Compiler config\n" >> "${TMP_TARGET_FILE_NAME}"

    if [ ! -z "${CXX}" ] ; then
        echo "CXX ?= ${CXX}" >> "${TMP_TARGET_FILE_NAME}"
    fi

    if [ ! -z "${CC}" ]; then
        echo "CC ?= ${CC}" >> "${TMP_TARGET_FILE_NAME}"
    fi
fi

echo '' >> "${TMP_TARGET_FILE_NAME}"
cat Makefile.in >> "${TMP_TARGET_FILE_NAME}"

# Atomic move if above was successful
mv "${TMP_TARGET_FILE_NAME}" "${TARGET_FILE_NAME}"

# Congrats
echo "Configuration complete, type 'make' to build ${project_name}."
