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