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