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