#
# $Id: makl_tc,v 1.19 2007/09/13 10:31:26 tat Exp $
#

# _makl_tc_cf input output
#
#   Generate a Bourne shell file from input and append to output file.
#   $1 - toolchain template file
#   $2 - output Bourne shell file
#   Return '0' on success, '1' otherwise.
#
_makl_tc_cf ()
{
    sed -e '/^#/d'          \
        -e '/^$/d'          \
        -e '/^[A-Z]/!d'     \
        -e 's/__UNSET__//'  \
        -e 's/ *= */=/1'    \
        -e 's/=/="/1'       \
        -e 's/$/"/' $1 >> $2

    [ $? -ne 0 ] && return 1 
    return 0
}

# _makl_tc_mk input output
#
#   Generate a GNU make file from input and append to output file.
#   $1 - toolchain template file
#   $2 - output GNU make file
#   Return '0' on success, '1' otherwise.
#
_makl_tc_mk ()
{
    sed -e '/^#/d'          \
        -e '/^$/d'          \
        -e '/^[A-Z]/!d'     \
        -e 's/__UNSET__//'  \
        -e 's/^/export\ /' $1 >> $2

    [ $? -ne 0 ] && return 1 
    return 0
}

# makl_tc tc_file tc_sh tc_mk
#
#   Starting from the template 'tc_file', create a Bourne shell and GNU make
#   compatible list of variables.  Any time the 'tc_file' is modified, you
#   should run it through makl_tc to get fresh new 'tc_sh' and 'tc_mk'.
#   $1 - the toolchain template file
#   $2 - the Bourne shell version of 'tc_file'
#   $3 - the GNU make version of 'tc_file'.
#   Return '0' on success, '1' otherwise.   
#
makl_tc ()
{
    [ $# -ne 3 ] && makl_err 1 "makl_tc: ${makl_tc_usage}"
    [ ! -f $1 ] && makl_err 1 "$1 is not a valid file pathname"

    tc_default=${MAKL_DIR}/tc/default.tc

    # create toolchain.cf
    echo "# Bourne shell compatible toolchain file." > $2
    echo "# Autogenerated by MaKL - `date`" >> $2
    _makl_tc_cf ${tc_default} $2      # default settings
    [ $? -ne 0 ] && return 1 
    _makl_tc_cf $1 $2                 # local settings
    [ $? -ne 0 ] && return 1 
    
    # create toolchain.mk
    echo "# GNU Make compatible toolchain file." > $3
    echo "# Autogenerated by MaKL - `date`" >> $3
    _makl_tc_mk ${tc_default} $3      # default settings
    [ $? -ne 0 ] && return 1 
    _makl_tc_mk $1 $3                 # local settings
    [ $? -ne 0 ] && return 1 

    return 0
}

makl_tc_usage="makl_tc tc_file tc_sh tc_mk"
