# 
# $Id: makl_conf,v 1.17 2006/05/23 18:08:54 stewy 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
    }

    # 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
        if [ -z ${val} ]; then
            ${ECHO} "#define ${var}" >> ${makl_conf_h}
        else    
            ${ECHO} "#define ${var} ${val}" >> ${makl_conf_h}
        fi    
    else
        ${ECHO} "#undef ${var}" >> ${makl_conf_h}
    fi
}

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

##\brief Initialise output makefile configuration.
##
##  Initialise output makefile configuration \e ${makl_makefile_conf}.
##
makl_init_makefile_conf ()
{
    ${ECHO} "# Autogenerated by MaKL - `date`" > ${makl_makefile_conf}
    ${ECHO} >> ${makl_makefile_conf}
}

##\brief Terminate output header file.
##
##  Terminate output header file \e ${makl_conf_h}.
##
makl_term_conf_h ()
{
    ${ECHO} >> ${makl_conf_h}
    ${ECHO} '#endif /* !_CONF_H_ */' >> ${makl_conf_h}
}
