# 
# $Id: makl_conf,v 1.23 2007/07/03 14:50:52 tho Exp $
#

##\brief Process configuration output from cache.
##
##  Process configuration output (\e ${makl_conf_h} and 
##  \e ${makl_makefile_conf}) from internal cache. 
##
makl_process_conf ()
{
    [ -z `makl_get "__noconfig__"` ] || return
    makl_info "processing configuration output from cache"

    f_mk=${makl_run_dir}/vars_mk
    f_h=${makl_run_dir}/vars_h

    # initialise 
    makl_init_makefile_conf
    makl_init_conf_h

    # write vars_mk 
    cat ${f_mk} | {
        while read line; do
            makl_process_mk "${line}"
        done
    }

    # terminate Makefile.conf with relocation include
    makl_term_mk

    # write header variables
    cat ${f_h} | { 
        while read line; do
            makl_process_h "${line}"
        done
    }   

    # terminate 
    makl_term_conf_h
}

##\brief Parse a line and output to mk file.
##
##  Parse a line \e $1 and output to mk file.
##
##   \param $1 line to be processed
##
makl_process_mk ()
{
    var=`makl_tab_elem "$1" 1`
    set=`makl_tab_elem "$1" 2`
    val=`makl_tab_elem "$1" 3`

    if [ ${set} = 1 ]; then
        ${ECHO} "${var} = ${val}" >> ${makl_makefile_conf}
    fi
}

##\brief Parse a line and output to h file.
##
##  Parse a line \e $1 and output to h file.
##
##   \param $1 line to be processed 
##
makl_process_h ()
{
    var=`makl_tab_elem "$1" 1`
    set=`makl_tab_elem "$1" 2`
    val=`makl_tab_elem "$1" 3`

    {
        if [ ${set} = 1 ]; then
            ${ECHO} "#undef ${var}"
            if [ -z ${val} ]; then
                ${ECHO} "#define ${var}"
            else    
                ${ECHO} "#define ${var} ${val}"
            fi    
            ${ECHO}
        else
            ${ECHO} "#undef ${var}" 
        fi

    } >> ${makl_conf_h}
}

##\brief Initialise output header file.
##
##  Initialise output header file \e ${makl_conf_h}.
##
makl_init_conf_h ()
{
    args=`makl_get "__args__"`
    filename=`basename ${makl_conf_h}`
    file=`makl_upper ${filename}` 
    file=`${ECHO} ${file} | sed 's/\./_/g' | sed 's/-/_/g'` 
    {
        ${ECHO} "/* Autogenerated by MaKL - `date` */" 
        ${ECHO} "/* Configure arguments: ${args} */" 
        ${ECHO}
        ${ECHO} "#ifndef _${file}_"
        ${ECHO} "#define _${file}_"
        ${ECHO}

    } > ${makl_conf_h}
}

##\brief Initialise output makefile configuration.
##
##  Initialise output makefile configuration \e ${makl_makefile_conf}.
##
makl_init_makefile_conf ()
{
    args=`makl_get "__args__"`
    {
        ${ECHO} "# Autogenerated by MaKL - `date`" 
        ${ECHO} "# Configure arguments: ${args}" 
        ${ECHO} 

    } > ${makl_makefile_conf}
}

##\brief Terminate output header file.
##
##  Terminate output header file \e ${makl_conf_h}.
##
makl_term_conf_h ()
{
    filename=`basename ${makl_conf_h}`
    file=`makl_upper ${filename}` 
    file=`${ECHO} ${file} | sed 's/\./_/g' | sed 's/-/_/g'` 

    ${ECHO} "#endif /* !_${file}_ */" >> ${makl_conf_h}
}

##\brief Terminate output makefile configuration.
##
##  Terminate output makefile configuration \e ${makl_makefile_conf}
##
makl_term_mk ()
{
    ${ECHO} >> ${makl_makefile_conf}
    ${ECHO} "include reloc.mk" >> ${makl_makefile_conf}
}
