#!/usr/bin/env bash
# Copyright 2010-2015 RethinkDB, all rights reserved.

# This script detects the build settings local to the local build
# environment and saves them in the config.mk file.
#
# See ./configure --help for more information.

set -u

# Lists of dependencies and versions
init () {
    min_gcc_version=$(read_version 4.7.4)
    min_clang_version=0
    min_icc_version=0
    min_lessc_version=1.3.1
    max_lessc_version=99.99.99
    min_coffee_version=1.7.0
    max_coffee_version=99.99.99
    min_browserify_version=13.0.0
    max_browserify_version=13.1.0
    min_python_version=2.6.0

    must_fetch_list='bluebird v8'
    please_fetch_list="handlebars gtest re2 $must_fetch_list"

    optional_libs="gtest termcap boost_system"
    required_libs="protobuf v8 re2 z crypto ssl curl"
    other_libs="unwind tcmalloc jemalloc"
    all_libs="$required_libs $optional_libs $other_libs"
    default_static="tcmalloc jemalloc"

    web_assets_deps="npm coffee browserify bluebird"
    required_bin_deps="protoc python"
    optional_bin_deps="wget curl"
    bin_deps="cxx $web_assets_deps $required_bin_deps $optional_bin_deps"
    all_deps="$bin_deps $all_libs boost"

    default_allocator_linux=jemalloc
    default_allocator_osx=system

    allowed_arg_vars="CXXFLAGS LDFLAGS"
}

# The main configuration steps
configure () {
    require "Bash"
    show "$BASH_VERSION"
    var FETCH_LIST ""
    var FETCH_VERSIONS ""
    for var in $allowed_arg_vars; do
        local val
        if lookup "$arg_vars" $var val; then
            require $var
            var $var "$val"
        fi
    done
    var LIB_SEARCH_PATHS $(for path in $lib_paths; do echo " -L$path"; done)
    require "Use ccache"
    boolvar USE_CCACHE $use_ccache
    check_cxx
    require "Host System"
    var MACHINE $($CXX -dumpmachine)
    require "Build System"
    show "`uname -msr`"
    check_cross_compiling
    require "Host Operating System"
    case "${MACHINE#*-}" in
        *apple-darwin*)
            var OS "Darwin"
            default_allocator=$default_allocator_osx
            var PTHREAD_LIBS "" ;
            var RT_LIBS "" ;;
        *linux*)
            var OS "Linux"
            default_allocator=$default_allocator_linux
            var PTHREAD_LIBS -pthread ;
            var RT_LIBS -lrt ;;
        *) error "unsupported operating system: $MACHINE" ;;
    esac
    case "${MACHINE%%-*}" in
        x86_64|i?86)
            true ;;
        arm*)
            var_append LDFLAGS -ldl
            final_warning="ARM support is still experimental" ;;
        *)
            error "unsupported architecture: $MACHINE"
    esac
    var M_LIBS -lm
    require "Build Architecture"
    var GCC_ARCH $(uname -m | grep '^[A-Za-z0-9_]*$' | head -n 1)
    var GCC_ARCH_REDUCED $(echo "$GCC_ARCH" | sed -e 's/^i[56]86$$/i486/g')
    if [ "$COMPILER" = CLANG ]; then
        require stdlib
        case "$OS" in
            Darwin) var_append LDFLAGS -lc++
                    var_append CXXFLAGS -stdlib=libc++ ;;
            *) var_append LDFLAGS -lstdc++ ;;
        esac

        check_clang_bug_5424

    fi
    check_cxx11
    require "Precompiled web assets"
    if [[ -z "$enable_precompiled_web" ]]; then
        if test -d precompiled/web_assets; then
            enable_precompiled_web=true
        else
            enable_precompiled_web=false
        fi
    fi
    boolvar USE_PRECOMPILED_WEB_ASSETS $enable_precompiled_web
    for bin in $required_bin_deps; do
        require_dep $bin
        check_bin $bin
    done
    for bin in $web_assets_deps; do
        if $enable_precompiled_web; then
            optional_dep $bin
        else
            require_dep $bin
        fi
        check_bin $bin
    done
    check_admin_deps
    for bin in $optional_bin_deps; do
        optional_dep $bin
        check_bin $bin
    done
    build_try_lib_paths
    for lib in $optional_libs; do
        check_lib $lib optional
    done
    for lib in $required_libs; do
        check_lib $lib require
    done

    check_v8_pre_3_19

    require malloc
    var ALLOCATOR "${custom_allocator:-$default_allocator}"
    var DEFAULT_ALLOCATOR "$default_allocator"
    if [[ "$ALLOCATOR" = system ]]; then
        var MALLOC_LIBS ""
        var MALLOC_LIBS_DEP ""
        boolvar STATIC_MALLOC false
    else
        check_lib "$ALLOCATOR" require
        boolvar STATIC_MALLOC contains "$static_libs" "$ALLOCATOR"
        macro MALLOC_LIBS     "\$($(uc $ALLOCATOR)_LIBS)"
        macro MALLOC_LIBS_DEP "\$($(uc $ALLOCATOR)_LIBS_DEP)"
    fi

    test_protobuf
    check_boost
    check_openssl
    boolvar STATIC_V8 contains "$static_libs" v8
    boolvar ALLOW_FETCH $allow_fetch
    require Installation prefix
    var PREFIX ${arg_prefix:-/usr/local}
    require Configuration prefix
    var SYSCONFDIR ${arg_sysconfdir:-$PREFIX/etc}
    require Runtime data prefix
    var LOCALSTATEDIR ${arg_localstatedir:-$PREFIX/var}
    for pkg in $must_fetch_list; do
        if ! contains "$FETCH_LIST" $pkg; then
            require_dep $pkg
            fetch_pkg $pkg
        fi
    done
    if [ -n "${final_warning:-}" ]; then
        echo "* Warning: $final_warning"
    fi
}

# Entry point
main () {
    init

    if test -e configure.default; then
        echo "* Reading arguments from 'configure.default'"
        default_args=$(cat configure.default)
        echo "* Prepending the following arguments:" $default_args
    fi

    read_args ${default_args:-} "$@"

    echo "* Detecting system configuration"

    trap "show error; echo '* Aborting configure'" EXIT

    write "# Automatically generated by $0" 3> "$config"
    write "# Command line: $@" 3>> "$config"
    write "CONFIGURE_STATUS := started" 3>> "$config"
    write "CONFIGURE_ERROR := " 3>> "$config"
    write "CONFIGURE_COMMAND_LINE := $(echo ${default_args:-}) $@" 3>> "$config"
    write "CONFIGURE_MAGIC_NUMBER := 2" 3>> "$config"
    configure 3>> "$config"

    trap - EXIT

    if ! $failed; then
        write "CONFIGURE_STATUS := success" 3>> "$config"
        echo "* Wrote configuration to $config"
    else
        write "CONFIGURE_STATUS := failed" 3>> "$config"
        echo "* Aborting configure"
        exit 1
    fi
}

# Parse the command line arguments
read_args () {
    local no_arg
    local has_arg
    local arg
    local option
    local dep

    exit_on_error=true
    config=config.mk
    failed=false

    allow_fetch=false
    force_paths='=|'
    can_show=false
    required=false
    arg_vars=$'\1\2'
    lib_paths=
    static_libs=$default_static
    arg_prefix=
    arg_sysconfdir=
    arg_localstatedir=
    custom_allocator=
    enable_precompiled_web=
    use_ccache=false

    while [[ $# -ne 0 ]]; do
        arg=${1#*=}
        if [[ "$arg" = "$1" ]]; then
            no_arg=shift
            if [[ $# -eq 1 ]]; then
                has_arg="error_missing_arg $(quote "$1")"
            else
                arg=$2
                has_arg='shift 2'
            fi
        else
            no_arg="error_no_arg $(quote "$1")"
            has_arg=shift
        fi
        option="${1%%=*}"
        case "$option" in
            --debug-configure) $no_arg; set -x ;;
            --config) $has_arg; config=$arg ;;
            --continue) $no_arg; exit_on_error=false ;;
            --allow-fetch) $no_arg; allow_fetch=true ;;
            --fetch) $has_arg
                if [[ "$arg" = "all" ]]; then
                    please_fetch_list="$(all_fetch_deps)"
                elif can_fetch $(guess_pkg_name "$arg"); then
                    please_fetch_list="$please_fetch_list $(guess_pkg_name "$arg")"
                else
                    die "Don't know how to fetch '$arg'"
                fi ;;
            --static) $has_arg
                if [[ "$arg" = none ]]; then
                    static_libs=
                elif [[ "$arg" = all ]]; then
                    static_libs=$all_libs
                elif contains "$all_libs" "$arg"; then
                    static_libs="$static_libs $arg"
                else
                    die "Unknown library: $arg"
                fi ;;
            --dynamic) $has_arg
                if [[ "$arg" = none ]]; then
                    static_libs=$all_libs
                elif [[ "$arg" = all ]]; then
                    static_libs=
                elif contains "$all_libs" "$arg"; then
                    static_libs=$(remove "$static_libs" "$arg")
                    please_fetch_list=$(remove "$please_fetch_list" $(guess_pkg_name "$arg"))
                else
                    die "Unknown library: $arg"
                fi ;;
            --lib-path) $has_arg; lib_paths="$lib_paths $arg" ;;
            --with-tcmalloc) $no_arg; set_custom_allocator tcmalloc ;;
            --with-jemalloc) $no_arg; set_custom_allocator jemalloc ;;
            --with-system-malloc) $no_arg; set_custom_allocator system ;;
            --enable-precompiled-web) $no_arg; enable_precompiled_web=true ;;
            --disable-precompiled-web) $no_arg; enable_precompiled_web=false ;;
            --ccache) $no_arg; use_ccache=true ;;
            --prefix) $has_arg; arg_prefix=$arg ;;
            --sysconfdir) $has_arg; arg_sysconfdir=$arg ;;
            --localstatedir) $has_arg; arg_localstatedir=$arg ;;
            --help) show_help; exit ;;
            -*) die "Unknown option '$option'" ;;
            *) if contains "$all_deps" "$(lc $option)"; then
                 $has_arg
                 force_paths="$force_paths|$(lc $option)=$arg|"
               elif contains "$allowed_arg_vars" "$option"; then
                 $has_arg
                 arg_vars="$arg_vars"$'\2'"$option"$'\1'"$arg"
               else
                 die "Unknown variable argument: $option"
               fi ;;
        esac
    done
}

set_custom_allocator () {
    if [[ -n "$custom_allocator" ]]; then
        die "incompatible options: --with-$custom_allocator and --with-$1"
    fi
    custom_allocator=$1
}

# Description of some of the dependencies
dep_descrs=':
cxx:C++ Compiler
protoc:Protobuf compiler
npm:Node.js package manager
unwind:libunwind
v8:v8 javascript engine
protobuf:Protobuf library
browserify:Browserify
v8:V8 JavaScript Engine
re2:RE2
z:zlib
gtest:Google Test'

# Output of --help
show_help () {
    cat <<EOF
Configure a RethinkDB build from source

Usage: $0 [arguments]

  --help                  Display this help
  --config <file>         Output file (default config.mk)
  --continue              Do not stop after encountering an error

  --allow-fetch           Allow fetching missing dependencies
  --lib-path <dir>        Add dir to the library search path
  <var>=<val>             Set the value of a variable
                            CXXFLAGS  C++ compiler arguments
                            LDFLAGS   C++ linker arguments
  --fetch <dep>           Force fetching <dep>.
  <dep>=<path>            Library or executable path. <dep> can be
EOF
    for dep in $all_deps; do
        local padded="$dep                        "
        local descr
        lookup "$dep_descrs" $dep descr || descr=
        echo "                            ${padded:0:24} $descr"
    done
    cat <<EOF
  --static <lib>          Statically link some libraries. <lib> is a library name, 'all' or 'none'
  --dynamic <lib>         Dynamically link some libraries. <lib> is a library name, 'all' or 'none'
  --prefix <dir>          Installation prefix. Defaults to /usr/local
  --sysconfdir <dir>      Configuration prefix. Defaults to /usr/local/etc
  --localstatedir <dir>   Runtime data prefix. Defaults to /usr/local/var
  --with-tcmalloc         Use TCMalloc
  --with-jemalloc         Use jemalloc (default on Linux)
  --with-system-malloc    Use the system malloc (default on OS X)
  --enable-precompiled-web
  --disable-precompiled-web Use precompiled web assets located in precompiled/web (default: autodetect)
  --ccache                Speed up the build using ccache
EOF
}

# Quote for re-use in the shell
# quote 'a b' -> 'a\ b'
quote () {
    printf %q "$1"
}

# Some error messages
error_no_arg () { die "option ${1%%=*} does not take any arguments"; }
error_missing_arg () { die "option ${1%%=*} takes an argument"; }

# error <message>
# Try to generate an error
# If $delayed_errors, then save the error for later
# If not $required, turn it into a warning
error () {
    local req=$required
    if ${delay_errors:-false}; then
        delayed_error="$*"
        return
    fi
    local type
    if $req; then
        show "error"
        type=Error
    else
        show "no"
        type=Warning
    fi
    if $req || ! ${ignore_warnings:-false}; then
        echo "* $type: $*" >&2
    fi
    write "CONFIGURE_ERROR := $*"
    if $req; then
        echo "${error_details:-}" | head -n 5
        if $exit_on_error; then
            exit 1
        fi
    fi
    failed=$req
}

# Like error, but fatal
die () {
    show error
    required=true
    error "$@"
    exit 1
}

# not_found <name>
# Generate a not found error
not_found () {
    if $required; then
        if can_fetch $(guess_pkg_name "`lc $1`"); then
            error "missing $1. Install it, specify the full path with $(uc $1)= or run ./configure with --allow-fetch"
        else
            error "missing $1. Install it or specify the full path with $(uc $1)="
        fi
    else
        show no
    fi
}

# write <line>
# Write to the config file
write () {
    echo "$*" >&3
}

# show_descr <description>
# Describe what the script is looking for next
# A call to show will complete the line
show_descr () {
    if ${can_show:-false}; then
        show_no
    fi
    local padded="$*:                               "
    echo -n "${padded:0:32}"
    write "# $*"
    can_show=true
    error_details=
}

# show_no [package_descr] [package_name]
# Like 'show no', but show the delayed error if there is one
show_no () {
    local req=$required
    local err=$delayed_error
    local info=""
    show no
    if $req; then
        if can_fetch "$(guess_pkg_name "${2:-$1}")"; then
            info=" Try running ./configure with the --fetch ${2:-$1} or --allow-fetch flags."
        fi
        error "${err:-${1:-} not found.$info}"
    elif [[ -n "$delayed_error" ]]; then
        error "$err"
    fi
}

# show <value>
# Display the value that was found
show () {
    if $can_show; then
        echo "$*"
        can_show=false
    fi
    delayed_error=
    delay_errors=false
}

# Like show_descr, but set $required to true
require () {
    required=true
    show_descr "$@"
}

# Like show_descr, but set $required to false
optional () {
    required=false
    show_descr "$@"
}

# var <name> <value>
# Set the value of a variable in the config file
var () {
    local name=$1
    shift
    local val=$*
    show "$val"
    write "$name := $val"
    eval "$name=$(quote "$val")" 2>/dev/null
}

# macro <name> <value>
# Set the value of a macro variable in the config file
macro () {
    local name=$1
    shift
    local val=$*
    show "$val"
    write "$name = $val"
}

# var_append <name> <value>
# Append a value to a variable in to the config file
var_append () {
    local name=$1
    shift
    local val=$*
    show "$val"
    write "$name += $val"
    eval "$name=\${$name:-}\ $(quote "$val")"
}

# boolvar <name> <command> <arguments ...>
# Run the command and set the variable to its boolean return value
boolvar () {
    local name=$1
    shift
    if "$@"; then
        show yes
        var $name 1
    else
        show no
        var $name 0
    fi
}

# Find a C++ compiler that rethinkdb supports
check_cxx () {
    require "C++ Compiler"
    local force_CXX
    lookup "$force_paths" cxx force_CXX || :
    CXX="${force_CXX:-${CXX:-}}"
    if test -z "$CXX" || ! $CXX --version >/dev/null 2>&1; then
        if [[ -n "${force_CXX:-}" ]]; then
            error "unable to run $force_CXX"
            return
        fi
        CXX=$(command -v c++ 2>/dev/null || command -v g++ 2>/dev/null || command -v clang 2>/dev/null || command -v icc 2>/dev/null)
        if [[ -z "$CXX" ]]; then
            not_found CXX
            return
        fi
    fi
    local description=$($CXX --version 2>/dev/null)$($CXX --help 2>/dev/null)
    local version_string=$(echo "$description" | extract_version_string)
    local type=$(echo "$description" | egrep -io 'gcc|g\+\+|clang|icc' | head -n 1)
    case "$(uc $type)" in
        GCC|G++) min_version=$min_gcc_version
                 type=GCC ;;
        CLANG) min_version=$min_clang_version ;;
        ICC) min_version=$min_icc_version
             type=INTEL ;;
        *) show "unknown"
           error "Could not determine C++ compiler type of $CXX (gcc, clang or icc is required)"
           return
    esac
    if [[ -z "$version_string" ]]; then
        show "$(uc $type) (unknown version)"
        error "Could not determine C++ compiler version (>= $(write_version $min_version) required)"
        return
    else
        show "$(uc $type) $version_string ($CXX)"
        test_version_ge $(uc $type) $(read_version "$version_string") $min_version || return
    fi

    var COMPILER "$(uc $type)"
    var CXX "$CXX"
}

# foo --version | extract_version_string
# find a version string in the output of foo --version
extract_version_string () {
    sed 's/([^)]*)//g' | egrep -o '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n 1
}

# read_version "1.2.3" -> 10203
# read_version "2.14" -> 21400
read_version () {
    local one=${1%%.*}
    local rest=${1#*.}
    local two=${rest%%.*}
    rest=${rest#*.}
    local three=${rest%%.*}
    if [[ "$three" != "$rest" ]]; then three=0; fi
    printf %d%02d%02d "$one" "$two" "$three"
}

# write_version "10203" -> "1.2.3"
write_version () {
    local v=$(printf %06d "$1")
    local v1=${v:0:2}
    local v2=${v:2:2}
    local v3=${v:4:2}
    printf %d.%d.%d "${v1#0}" "${v2#0}" "${v3#0}"
}

# test_version_ge <name> <version> <min version>
test_version_ge () {
    local fetch_message=
    if ! [[ $2 -ge $3 ]]; then
        if can_fetch "$(bin_pkg_name "$1")"; then
            fetch_message=" Try running ./configure with --allow-fetch"
        fi
        error "$1 $(write_version $2) is too old. At least $1 $(write_version $3) is required.$fetch_message"
        return 1
    fi
}

# test_version_le <name> <version> <max version>
test_version_le () {
    local fetch_message=
    if ! [[ $2 -le $3 ]]; then
        if can_fetch "$(bin_pkg_name "$1")"; then
            fetch_message=" Try running ./configure with --allow-fetch"
        fi
        error "$1 $(write_version $2) is too recent. At most $1 $(write_version $3) is required.$fetch_message"
        return 1
    fi
}

# test_bin_version <name> <path>
test_bin_version () {
    local min_var=min_$1_version
    local min=${!min_var:-}
    local max_var=max_$1_version
    local max=${!max_var:-}
    local invalid_var=invalid_$1_versions
    local invalid=${!invalid_var:-}
    test -z "$min$max$invalid" && return 0
    local output
    local ver
    if output=$("$2" --version 2>&1) &&
       ver=$(echo "$output" | extract_version_string) &&
       test -n "$ver" ;
    then
        local version=$(read_version "$ver")
        test -z "$min" || test_version_ge $1 $version $(read_version $min) || return 1
        test -z "$max" || test_version_le $1 $version $(read_version $max) || return 1
        if contains "$invalid" "$ver" ; then
            error "$1 ($ver) is not supported"
            return 1
        fi
    else
        error "Could not determine $1 version"
        return 1
    fi
    show "$1 $ver"
}

# contains <haystack> <needle>
# Test if the space-separated list haystack contains needle
contains () {
    local d=" $1 "
    [[ "${d% $2 *}" != "$d" ]]
}

# remove <list> <item>
# remove an item from a space-separated list
remove () {
    if contains "$1" "$2"; then
        local outputlist=""
        for item in $1; do
            if [ "$item" != "$2" ]; then
                if [ -z "$outputlist" ]; then
                    outputlist="$item"
                else
                    outputlist="$outputlist $item"
                fi
            fi
        done
        echo -n "$outputlist"
    else
        echo -n "$1"
    fi
}

# any "<list>" <command>
# Test if the command is true for any element of the space seperated list
any () {
    local list=$1
    shift
    for x in $list; do
        if "$@" $x; then
            return 0
        fi
    done
    return 1
}

# lookup <dict> <key> [var]
# dict is an assoc-list composed of two seperators followed by a list of pairs
# eg: ':|foo:bar|test:123|baz:quux' or '= a=b c=d'
lookup () {
    local _val=${1#*${1:1:1}$2${1:0:1}}
    if [[ "$_val" = "$1" ]]; then
        unset ${3:-$2}
        return 1
    fi
    eval "${3:-$2}=$(quote "${_val%%${1:1:1}*}")"
}

# check_bin <name>
# Check for a binary
check_bin () {
    local force_bin
    lookup "$force_paths" $1 force_bin || force_bin=
    if test -z "$force_bin" && contains "$please_fetch_list" $(bin_pkg_name "$1"); then
        fetch_bin $1
        return
    fi
    local ucbin=$(uc $1)
    local bin=${force_bin:-${!ucbin:-$1}}
    bin=$(command -v "$bin" 2>/dev/null)
    if [[ ! -x "$bin" ]]; then
        if [[ -n "${force_bin:-}" ]]; then
            bin=$force_bin
        elif fetch_allowed $(bin_pkg_name $1); then
            fetch_bin $1
            return
        else
            not_found $(uc $1)
            return
        fi
    fi
    local version
    delay_errors=true
    if ! test_bin_version $1 "$bin"; then
        if fetch_allowed $(bin_pkg_name $1); then
            fetch_bin $1
            return
        else
            show_no "$1"
            return
        fi
    fi
    var "$(uc $1)" "$bin"
    var "$(uc $1)_BIN_DEP" ""
}

# Invoke pkg.sh with the required environment variables
pkg () {
    FETCH_LIST="$FETCH_LIST" bash $root/mk/support/pkg/pkg.sh "$@"
}

# add_fetch <name>
# Instruct make to fetch and build the dependency
# Sets the version variable
add_fetch () {
    local pkg
    pkg="$1"
    if ! version=$(pkg version "$pkg"); then
        error "Internal error: cannot fetch $pkg"
        return 1
    fi
    show external/${pkg}_$version
    if ! contains "$FETCH_LIST" "$pkg"; then
        local depends
        depends=$(pkg depends "$pkg")
        for dep in $depends; do
            fetch_pkg $dep
        done
        var_append FETCH_LIST "$pkg"
        var "${pkg}_VERSION" "$version"
        var "${pkg}_DEPENDS" "$depends"
    fi
}

# Wrapper around add_fetch
fetch_pkg () {
    local version
    add_fetch "$1"
}

# Chose to fetch the given binary
fetch_bin () {
    local version
    pkg=$(bin_pkg_name "$1")
    add_fetch "$pkg"
    macro "$(uc "$1")"         "\$(abspath \$(SUPPORT_BUILD_DIR)/${pkg}_$version/bin/$1)"
    macro "$(uc "$1")_BIN_DEP" "\$(SUPPORT_BUILD_DIR)/${pkg}_$version/bin/$1"
}

# Chose to fetch the given library
fetch_lib () {
    local pkg
    local version
    pkg=$(lib_pkg_name "$1")
    add_fetch "$pkg"
    var_append "$pkg"_LIB_NAME "$(uc "$1")"
    var "HAS_$(uc "$1")" 1
    macro "$(uc "$1")_LIBS_DEP"         "\$(SUPPORT_BUILD_DIR)/${pkg}_$version/lib/lib$1.a"
    macro "$(uc "$1")_INCLUDE" "-isystem \$(SUPPORT_BUILD_DIR)/${pkg}_$version/include"
    if pkg separate-install-include "$pkg"; then
        macro "$(uc "$1")_INCLUDE_DEP"  "\$(SUPPORT_BUILD_DIR)/${pkg}_$version/include"
    else
        macro "$(uc "$1")_INCLUDE_DEP"  "\$(SUPPORT_BUILD_DIR)/${pkg}_$version/lib/lib$1.a"
    fi
    static_libs="$static_libs $1"
}

# require_dep <name>
# Like require, but looks up the description for name
require_dep () {
    local descr
    lookup "$dep_descrs" $1 descr || descr=$1
    require $descr
}

# optional_dep <name>
# Like optional, but looks up the description for name
optional_dep () {
    local descr
    lookup "$dep_descrs" $1 descr || descr=$1
    optional $descr
}

# An assoc-list of c++ code to test if a library works
lib_test_code=':~

~termcap:
#include <termcap.h>
int main(){ tgetent(0, "xterm"); return 0; }

~boost:
#include <boost/bind.hpp>
int main(){ return 0; }

~cxx11:
// Verify that std::map uses the move constructor

#include <map>

struct C {
    C(const C&) = delete;

    C() { }
    C(C &&) { }
};

int main() {
    std::map<int, C> m;
    m.insert(std::make_pair(0, C()));
}

~jemalloc:

#include <jemalloc/jemalloc.h>

int main(){
    // Jemalloc 4 fixes some bugs concerning the handling of OOM conditions
    static_assert(JEMALLOC_VERSION_MAJOR >= 4, "jemalloc version 4 or above is required");
}

~openssl:

#include <openssl/evp.h>
#include <openssl/crypto.h>
#include <openssl/ssl.h>
#include <openssl/bn.h>
int main(){ return 0; }

~'

# An assoc-list of possible library aliases
lib_alias=':
termcap:termcap tinfo ncurses
tcmalloc:tcmalloc_minimal'

# List the paths where static libraries can be found into the try_lib_paths variable
build_try_lib_paths () {
    cc_paths=$($CXX -print-search-dirs 2>/dev/null | grep ^libraries | sed -e 's/^libraries[: =]*//; s/:/ /g')
    LDCONFIG=$(PATH="$PATH:/sbin:/usr/sbin" command -v ldconfig 2>/dev/null)
    if [[ -n "$LDCONFIG" ]]; then
        ld_paths=$($LDCONFIG -v -N -X 2>/dev/null | grep -v '^	' | sed -e 's/:$//' )
    else
        ld_paths="/usr/lib /usr/local/lib" # Default ld search paths on OSX
    fi
    try_lib_paths="$lib_paths $cc_paths $ld_paths"
    try_lib_paths=$(for path in $try_lib_paths; do
            test -d "$path" && niceabspath "$path"
        done | sort -u)
}

# check_lib <name> <required/optional>
# Check for the presence of a library and set the correct make flags for it
# WISHLIST: Properly detect the presence of the headers and check that the
#           installed version is compatible. Try using pkg-config.
check_lib () {
    local info=$2
    local path
    if contains "$please_fetch_list $FETCH_LIST" $(lib_pkg_name "$1"); then
        ${info}_dep $1
        fetch_lib $1
        return
    fi
    if lookup "$force_paths" $1 path; then
        ${info}_dep $1
        check_lib_compile "$path" $1
        if $can_show; then
            show_no "$path" "$1"
            empty_deps_lib $1 false
        fi
        return
    fi
    local check
    if contains "$static_libs" $1; then
        static_info=" (static)"
        check=check_static_lib
    else
        static_info=
        check=check_dyn_lib
    fi
    $info "$1$static_info"
    delay_errors=true
    local aliases
    lookup "$lib_alias" $1 aliases || aliases=$1
    for lib in $aliases; do
        $check $lib $1
        if ! $can_show; then
            empty_deps_lib $1 true
            return
        fi
    done
    if fetch_allowed $(lib_pkg_name $1); then
        fetch_lib $1
    else
        show_no "lib$1$static_info" "$1"
        empty_deps_lib $1 false
    fi
}

empty_deps_lib () {
    if ! "$2"; then
        var "HAS_$(uc $1)" 0
        var "$(uc $1)_LIBS" ""
    else
        var "HAS_$(uc $1)" 1
    fi
    var "$(uc $1)_INCLUDE" ""
    var "$(uc $1)_INCLUDE_DEP" ""
    var "$(uc $1)_LIBS_DEP" ""
}

check_dyn_lib () {
    check_lib_compile -l$1 $2
}

# Test if the c++ compiler knows about the library
check_lib_compile () {
    local bfile=$root/mk/gen/check_$2
    local code
    if ! lookup "$lib_test_code" $2 code; then
        code='int main(){ return 0; }'
    fi
    echo "$code" > "$bfile.cc"
    if $CXX -std=gnu++0x ${CXXFLAGS:-} ${LDFLAGS:-} ${LIB_SEARCH_PATHS:-} "$bfile.cc" $1 -o "$bfile.out" 1>"$bfile.log" 2>&1; then
        show "${1:-ok}"
        var_append $(uc $2)_LIBS "$1"
        var HAS_$(uc $2) 1
    else
        error_details="${error_details:-}"$(cat "$bfile.log")
    fi
}

# Test if we are cross-compiling
check_cross_compiling () {
    require "Cross-compiling"
    local bfile="$root/mk/gen/check_cross_compile"
    echo "int main(){ return 0; }" > "$bfile.cc"
    if ! $CXX ${CXXFLAGS:-} ${LDFLAGS:-} "$bfile.cc" -o "$bfile.out" 1>"$bfile.log" 2>&1; then
        error_details=$(cat "$bfile.log")
        error "Failed to compile"
    else
        if "$bfile.out" >/dev/null 2>&1; then
            boolvar CROSS_COMPILING false
        else
            boolvar CROSS_COMPILING true
        fi
    fi
}

check_static_lib () {
    local found=false
    for path in $try_lib_paths; do
        local l=$path/lib$1.a
        if test -e "$l"; then
            found=true
            check_lib_compile "$l" "$2"
            if ! $can_show; then
                return 0
            fi
        fi
    done
    if ! $found; then
        error_details="${error_details:-}"$"lib$1.a not found\n"
    fi
}

# Some utility functions
# When bash 4 becomes ubiquitous, we can use ${//}, ${^^} and ${,,}
uc () { echo "$*" | tr '[:lower:]' '[:upper:]'; }
lc () { echo "$*" | tr '[:upper:]' '[:lower:]'; }
subst () { echo "${1%%$2*}$3${1#*$2}"; }

# bash's ! as a command
not () {
    ! "$@"
}

# Make the path absolute and prettier (with no /..)
niceabspath () {
    if [[ -d "$1" ]]; then
        (cd "$1" && pwd) && return
    fi
    local dir=$(dirname "$1")
    if [[ -d "$dir" ]] && dir=$(cd "$dir" && pwd); then
        echo "$dir/$(basename "$1")" | sed 's|^//|/|'
        return
    fi
    if [[ "${1:0:1}" = / ]]; then
        echo "$1"
    else
        echo "$(pwd)/$1"
    fi
}

# Test that PROTOC and PROTOBUF_LIBS work together
test_protobuf () {
    require "Test protobuf"
    if any "protoc protobuf" contains "$FETCH_LIST"; then
        if [[ -z "${PROTOC_BIN_DEP:-}" ]]; then
            fetch_bin protoc
        fi
        show fetch
        return
    fi
    if ! test -e "$PROTOC"; then
        required=false
        error "$PROTOC does not exist"
        return
    fi
    local pbdir=$root/mk/gen/protoc/
    mkdir -p "$pbdir"
    echo 'message Foo { enum Bar { Baz = 1; } }' > "$pbdir/test.proto"
    echo 'int main(){ return 0; }' > "$pbdir/main.cc"
    local out
    if ! out=$(
            "$PROTOC" "$pbdir/test.proto" --cpp_out=. 2>&1 &&
            $CXX "$pbdir/test.pb.cc" "$pbdir/main.cc" -I "$root" $PROTOBUF_LIBS $PTHREAD_LIBS $M_LIBS ${CXXFLAGS:-} ${LDFLAGS:-} ${LIB_SEARCH_PATHS:-} -o "$pbdir/a.out" 2>&1)
    then
        error_details="$out"
        error "Unable to compile sample protobuf file. Try running ./configure with the --fetch protoc option"
        return
    fi
    show ok
}

# fetch_allowed <pkg>
# Fails if the package cannot be fetched
fetch_allowed () {
     { contains "$please_fetch_list" $1 ||
         { can_fetch $1 && $allow_fetch ; } ; } || return 1
}

check_clang_bug_5424 () {
    local tmpfile=$root/mk/gen/check_clang_bug_5424
    cat > "$tmpfile.cc" <<EOF
#include <iostream>
int output = 1;
class data_t {
public:
    data_t(int num) {
    }
    ~data_t() {
        output = 0;
    }
};
struct data_struct_t {
    data_t some_data;
    data_t some_other_data;
};
int main() {
  output = 1;
    {
        data_struct_t{1, 2};
    }
    return output;
}
EOF
    if !($CXX ${CXXFLAGS:-} ${LDFLAGS:-} ${LIB_SEARCH_PATHS:-} "$tmpfile.cc" \
          -std=c++11 -o "$tmpfile.out" 1>"$tmpfile.log" 2>&1;
        $tmpfile.out)
    then
       error "You seem to be using a version of Clang with a bug that may affect Rethink DB. Please upgrade Clang or specify a different compiler."
    fi
}

# Checks what version of the v8 API is available
check_v8_pre_3_19 () {
    if contains "$FETCH_LIST" v8; then
        boolvar V8_PRE_3_19 false
        return
    fi
    require "Use pre-3.19 v8 API"
    local tmpfile=$root/mk/gen/check_v8_pre_3_19
    cat > "$tmpfile.cc" <<EOF
#include <v8.h>
int main() {
    v8::Local<v8::Context> lc = v8::Context::New(v8::Isolate::GetCurrent());
    v8::Persistent<v8::Context> ps;
    ps.Reset(v8::Isolate::GetCurrent(), lc);
    v8::Local<v8::Context> lh = v8::Local<v8::Context>::New(v8::Isolate::GetCurrent(), ps);
}
EOF
    if
        $CXX ${CXXFLAGS:-} ${LDFLAGS:-} ${LIB_SEARCH_PATHS:-} "$tmpfile.cc" \
            ${V8_LIBS:-} $PTHREAD_LIBS $M_LIBS $RT_LIBS -o "$tmpfile.out" 1>"$tmpfile.log" 2>&1;
    then
        boolvar V8_PRE_3_19 false
    else
        boolvar V8_PRE_3_19 true
    fi
}

# Check for boost
check_boost () {
    require "Test boost"
    if contains "$please_fetch_list" boost; then
        fetch_lib boost
        return
    fi
    check_lib_compile "" boost
    if $can_show; then
        if fetch_allowed boost; then
            fetch_lib boost
        else
            show_no "boost"
        fi
    else
        empty_deps_lib boost true
    fi
}

check_openssl () {
    require "Test OpenSSL"
    if contains "$please_fetch_list" openssl; then
        fetch_lib openssl
        return
    fi
    check_lib_compile "" openssl
    if $can_show; then
        if fetch_allowed openssl; then
            fetch_lib openssl
        else
            show_no "openssl"
        fi
    else
        empty_deps_lib openssl true
    fi
}

# bin_pkg_name <bin>
# The support package name that contains the application
bin_pkg_name () {
    case "$1" in
        protoc) echo protobuf ;;
        coffee) echo coffee-script ;;
        npm) echo node ;;
        # The curl-bin package doesn't exist. It is placed here to avoid
        # the circular dependency of curl being used to fetch curl.
        curl) echo curl-bin ;;
        *) echo "$1" ;;
    esac
}

# lib_pkg_name <lib>
# The support package name that contains the library
lib_pkg_name () {
    case "$1" in
        tcmalloc) echo gperftools ;;
        z) echo zlib ;;
        boost_system) echo boost ;;
        crypto) echo openssl ;;
        ssl) echo openssl ;;
        *) echo "$1" ;;
    esac
}

# guess_pkg_name <name>
# Find the name of a package associated with the given name
guess_pkg_name () {
    local pkg
    case "$1" in
        # Assume that curl here means libcurl, not curl itself
        curl) echo curl ; return ;;
    esac
    pkg=$(lib_pkg_name "$1")
    if [[ "$pkg" != "$1" ]]; then
        echo "$pkg"
    else
        bin_pkg_name "$1"
    fi
}

# can_fetch <pkg>
# Check if we know how to fetch pkg
can_fetch () {
    test -e $root/mk/support/pkg/"$1".sh
}

# List all the packages that can be fetched
all_fetch_deps () {
    local ret=
    local pkg
    for dep in $all_deps; do
        pkg=$(guess_pkg_name "$dep")
        if can_fetch $pkg; then
            ret="$ret $pkg"
        fi
    done
    echo "$ret"
}

check_cxx11 () {
    require "C++11"
    check_lib_compile "" cxx11
    if $can_show; then
        show_no "C++11"
    fi
}

check_admin_deps () {
    require "web UI dependencies"
    local version
    version=$(pkg version admin-deps)
    add_fetch admin-deps
    macro "GULP"         "\$(abspath \$(SUPPORT_BUILD_DIR)/admin-deps_$version/bin/gulp)"
    macro "GULP_BIN_DEP" "\$(SUPPORT_BUILD_DIR)/admin-deps_$version/bin/gulp"
}

# The root of the source tree
root=$(dirname $0)

# Call the main command with the command line arguments
main "$@" 3>/dev/null
