#
# $Id: makl_utils_tab,v 1.15 2007/07/13 10:47:03 tho Exp $
#


##\brief Find an identifier.
##
##  Check whether id \e $2 exists in table \e $1. 
##
##   \param $1 table file
##   \param $2 id to be searched for
##   \return 0 if found, 1 otherwise.
##
makl_tab_find ()
{
    [ -r $1 ] || return 1

    grep "^$2|" $1 > /dev/null

    return $?
}

##\brief Set the value of an element corresponding to an identifier.
##
##  Set variable entry in table \e $1 with id \e $2 and at column 
##  \e $3 to val \e $4.
##
##   \param $1 table file 
##   \param $2 id (row)
##   \param $3 column
##   \param $@ value
##
makl_tab_set ()
{ 
    tab=$1; id=$2; col=$3
    shift; shift; shift
    line=""
    line_n=""
    l=""
    new=""
    i=1

    # create file if doesn't exist
    [ -e ${tab} ] || touch ${tab}

    line=`grep "^${id}|" ${tab}`

    if [ $? = 0 ]; then     
       # subst the value

       # delete the old line
       grep -v "^${id}|" ${tab} > ${tab}.tmp
       mv ${tab}.tmp ${tab}

       # escape the value
       qt=`${ECHO} -n "$@" | sed 's/\//\\\\\\//g'`

       ${ECHO} "$line" | sed "s/[^|]*|/${qt}|/${col}" >> ${tab}
    else
        # add a new var
        line_n="${id}"

        n=1
        lim=`expr ${col} - 1`   # avoid bash-ism, was "lim=$((${col}-1))"
        while [ ${n} -lt ${lim} ]; do
            line_n="${line_n}|"
            n=`expr ${n} + 1`   # avoid bash-ism, was: "n=$((n+1))"
        done

        line_n="${line_n}|$@|||||||||||"

        ${ECHO} ${line_n} >> ${tab}
    fi

    return 0    
}

##\brief Set row to given arguments.
##
##  Set row of table \e $1 to given arguments \e $@.
##
##   \param $1 table file
##   \param $@ row values
##
makl_tab_set_row ()
{
    line=""
    file=$1
    i=1

    # create file if doesn't exist
    [ -e $1 ] || touch $1

    shift

    makl_tab_find ${file} $1
    [ $? = 0 ] && return

    for arg in "$@"; do
        if [ ${i} = 1 ]; then
            line="${arg}"
        else
            [ -z "${arg}" ] && arg=" "
            line="${line}|${arg}"
        fi
        i=`expr ${i} + 1`   # avoid bash-ism, was: "i=$((${i}+1))"
    done

    ${ECHO} ${line} >> ${file}
}

##\brief Get a column value given an identifier.
##
##  Get column \e $3 of element with id \e $2 in file \e $1.
##
##   \param $1 table file 
##   \param $2 identifier to be retrieved
##   \param $3 target column
##   \return 0 if the element was found, 1 otherwise.
##
makl_tab_get ()
{
    [ -r $1 ] || return 1

    found=`grep "^$2|" $1`
    [ $? = 0 ] || return $? 

    val=`${ECHO} ${found} | cut -s -f $3 -d "|"`
    ret=$?

    ${ECHO} -n ${val}
    return ${ret}
}

##\brief Output an element at an index of a row.
##
##  Output element at index \e $2 of row \e $1.
##
##   \param $1 string of elements
##   \param $2 index of element
##
makl_tab_elem ()
{
    ${ECHO} "$1" | cut -s -f $2 -d "|"
}

##\brief Get variable by name given a list of variables.
##
##  Get variable by name \e $2 given a list of variables \e $1.
##  A semicolon is used as a list separator.
##
##   \param $1 input string
##   \param $2 required var
##   \return 0 if the variable was found, 1 otherwise.
##
makl_tab_var ()
{
    i=1
    found=0

    while true; do
        elem=`${ECHO} $1 | cut -f ${i} -d ";"`
        delim=`${ECHO} $1 | cut -s -f ${i} -d ";"`
        [ -z ${elem} ] && break
        var=`${ECHO} ${elem} | cut -f 1 -d "="`
        val=`${ECHO} ${elem} | cut -f 2 -d "="`
        if [ ${var} = $2 ] || [ -z ${delim} ]; then
            found=1
            ${ECHO} ${val}
            break
        fi
        # stop if we have no separator
        i=`expr ${i} + 1`   # avoid bash-ism, was: "i=$((${i}+1))"
    done

    if [ ${found} = 0 ]; then
        return 1
    else
        return 0
    fi
}
