a93e80
# Copyright (C) 2017, Red Hat, Inc.
a93e80
#
a93e80
# Core configuration file library.
a93e80
a93e80
# Configurations are determined by sha values.  The way to determine is by
a93e80
# the special text:
a93e80
# $FILE_COMMENT_TYPE -*- cfg-sha: $SHA256 -*-
a93e80
a93e80
export LC_ALL=C
a93e80
a93e80
# check required binaries
a93e80
__check_reqd_binaries() {
a93e80
    local BIN __binaries=("egrep" "sort" "sha256sum" "sed")
a93e80
    for BIN in $__binaries; do
a93e80
        if ! type -P $BIN >/dev/null 2>&1; then
a93e80
            echo "Binary $BIN not found.  Please install."
a93e80
            exit 1
a93e80
        fi
a93e80
    done
a93e80
}
a93e80
a93e80
# Calculates a sha from a file
a93e80
# The algorithm for generating a sha from a config is thus:
a93e80
#
a93e80
# 1. Remove all comment lines and blank lines
a93e80
# 2. Sort the content
a93e80
# 3. generate the sha-256 sum
a93e80
#
a93e80
# From a script perspective, this means:
a93e80
#   egrep -v ^\# %file% | egrep -v ^$ | sort -u | sha256sum
a93e80
#
a93e80
# Params:
a93e80
#  $1 = output variable
a93e80
#  $2 = file to use to calculate the shasum
a93e80
#  $3 = file comment type (defaults to # if unspecified)
a93e80
calc_sha() {
a93e80
    __check_reqd_binaries
a93e80
a93e80
    if [ "$1" == "" ]; then
a93e80
        echo "Please pass in a storage variable."
a93e80
        return 1
a93e80
    fi
a93e80
a93e80
    local __resultvar=$1
a93e80
    __retval=1
a93e80
    shift
a93e80
a93e80
    local __file=$1
a93e80
    local cmnt=${2:-#}
a93e80
a93e80
    if [ -f "$__file" ]; then
a93e80
        local __shasum=$(egrep -v ^"$cmnt" "$__file" | egrep -v ^$ | sort -u | sha256sum -t | cut -d" " -f1)
a93e80
        eval $__resultvar="'$__shasum'"
a93e80
        __retval=0
a93e80
    fi
a93e80
    return $__retval
a93e80
}
a93e80
a93e80
# Retrieves a sha stored in a file
a93e80
# Param:
a93e80
#  $1 = output variable
a93e80
#  $2 = file to use to calculate the shasum
a93e80
#  $3 = file comment type (defaults to # if unspecified)
a93e80
retr_sha() {
a93e80
    __check_reqd_binaries
a93e80
a93e80
    if [ "$1" == "" ]; then
a93e80
        echo "Please pass in a storage variable."
a93e80
        return 1
a93e80
    fi
a93e80
a93e80
    local __resultvar=$1
a93e80
    __retval=1
a93e80
    shift
a93e80
a93e80
    local __file=$1
a93e80
    local cmnt=${2:-#}
a93e80
a93e80
    if [ -f "$__file" ]; then
a93e80
        if grep -q "$cmnt -\*- cfg-sha:" "$__file"; then
a93e80
            local __shasum=$(grep "$cmnt -\*- cfg-sha:" "$__file" | sed -e "s@$cmnt -\*- cfg-sha: @@" | cut -d" " -f1)
a93e80
            eval $__resultvar="'$__shasum'"
a93e80
            __retval=0
a93e80
        fi
a93e80
    fi
a93e80
    return $__retval
a93e80
}
a93e80
a93e80
a93e80
# Set a config value
a93e80
# set_conf dpdk_build_tree parameter value
a93e80
# dpdk_build_tree is the directory where the .config lives
a93e80
# parameter is the config parameter
a93e80
# value is the value to set for the config parameter
a93e80
set_conf() {
a93e80
    c="$1/.config"
a93e80
    shift
a93e80
a93e80
    if grep -q "$1" "$c"; then
a93e80
        sed -i "s:^$1=.*$:$1=$2:g" $c
a93e80
    else
a93e80
        echo $1=$2 >> "$c"
a93e80
    fi
a93e80
}
a93e80