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