#!/bin/bash
#

MKL_CONFIGURE_ARGS="$0 $*"

# Load base module
source mklove/modules/configure.base

# Read some special command line options right away that must be known prior to
# sourcing modules.
mkl_in_list "$*" "--no-download" && MKL_NO_DOWNLOAD=1
# Disable downloads when --help is used to avoid blocking calls.
mkl_in_list "$*" "--help" && MKL_NO_DOWNLOAD=1
mkl_in_list "$*" "--debug" && MKL_DEBUG=1

# Delete temporary Makefile and header files on exit.
trap "{ rm -f $MKL_OUTMK $MKL_OUTH; }" EXIT



##
## Load builtin modules
##

# Builtin options, etc.
mkl_require builtin

# Host/target support
mkl_require host

# Compiler detection
mkl_require cc


# Load application provided modules (in current directory), if any.
for fname in configure.* ; do
    if [[ $fname = 'configure.*' ]]; then
        continue
    fi

    # Skip temporary files
    if [[ $fname = *~ ]]; then
        continue
    fi

    mkl_require $fname
done




##
## Argument parsing (options)
##
##

# Parse arguments
while [[ ! -z $@ ]]; do
    if [[ $1 != --* ]]; then
        mkl_err "Unknown non-option argument: $1"
        mkl_usage
        exit 1
    fi

    opt=${1#--}
    shift

    if [[ $opt = *=* ]]; then
        name="${opt%=*}"
        arg="${opt#*=}"
        eqarg=1
    else
        name="$opt"
        arg=""
        eqarg=0
    fi

    safeopt="$(mkl_env_esc $name)"

    if ! mkl_func_exists opt_$safeopt ; then
        mkl_err "Unknown option $opt"
        mkl_usage
        exit 1
    fi

    # Check if this option needs an argument.
    reqarg=$(mkl_meta_get "MKL_OPT_ARGS" "$(mkl_env_esc $name)")
    if [[ ! -z $reqarg ]]; then
        if [[ $eqarg == 0 && -z $arg ]]; then
            arg=$1
            shift

            if [[ -z $arg ]]; then
                mkl_err "Missing argument to option --$name $reqarg"
                exit 1
            fi
        fi
    else
        if [[ ! -z $arg ]]; then
            mkl_err "Option --$name expects no argument"
            exit 1
        fi
        arg=y
    fi

    case $name in
        re|reconfigure)
            oldcmd=$(grep '^# configure exec: ' config.log | \
                sed -e 's/^\# configure exec: //')
            if [[ -z $oldcmd ]]; then
                echo "No previous execution found in config.log"
                exit 1
            fi
            echo "Reconfiguring: $oldcmd"
            exec $oldcmd
            ;;

        list-modules)
            echo "Modules loaded:"
            for mod in $MKL_MODULES ; do
                echo "  $mod"
            done
            exit 0
            ;;

        list-checks)
            echo "Check functions in calling order:"
            for mf in $MKL_CHECKS ; do
                mod=${mf%:*}
                func=${mf#*:}
                echo -e "${MKL_GREEN}From module $mod:$MKL_CLR_RESET"
                declare -f $func
                echo ""
            done
            exit 0
            ;;

        update-modules)
            fails=0
            echo "Updating modules"
            for mod in $MKL_MODULES ; do
                echo -n "Updating $mod..."
                if mkl_module_download "$mod" > /dev/null ; then
                    echo -e "${MKL_GREEN}ok${MKL_CLR_RESET}"
                else
                    echo -e "${MKL_RED}failed${MKL_CLR_RESET}"
                    fails=$(expr $fails + 1)
                fi
            done
            exit $fails
            ;;

        help)
            mkl_usage
            exit 0
            ;;

        *)
            opt_$safeopt $arg || exit 1
            mkl_var_append MKL_OPTS_SET "$safeopt"
            ;;
    esac
done

if [[ ! -z $MKL_CLEAN ]]; then
    mkl_clean
    exit 0
fi

# Move away previous log file
[[ -f $MKL_OUTDBG ]] && mv $MKL_OUTDBG ${MKL_OUTDBG}.old


# Create output files
echo "# configure exec: $0 $*" >> $MKL_OUTDBG
echo "# On $(date)" >> $MKL_OUTDBG

rm -f $MKL_OUTMK $MKL_OUTH
mkl_write_mk "# Automatically generated by $0 $*"
mkl_write_h "// Automatically generated by $0 $*"
mkl_write_h "#pragma once"


# Load cache file
mkl_cache_read

# Run checks
mkl_checks_run

# Check accumulated failures, will not return on failure.
mkl_check_fails

# Generate outputs
mkl_generate

# Summarize what happened
mkl_summary

# Write cache file
mkl_cache_write


echo ""
echo "Now type 'make' to build"
trap - EXIT
exit 0
