1
  #   ˆl–äY>”êa,j‚fàš¸'Å£Æ„-6ÁÉ 4Š    #!/usr/bin/sh
#                          -*- mode: shell-script; sh-shell: "/bin/sh" -*-
# Copyright (C) 2018, 2021 g10 Code GmbH
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# SPDX-License-Identifier: FSFULLR

#### start of functions for this script

#
# Bourne shell functions for config file in pkg-config style, so that
# we can share such a config file between pkg-config and script
#

#
# get_var: Get the variable value of NAME
#
# Variables are recorded in the shell variables named "VAR_<NAME>"
#
get_var () {
    ___name=$1

    eval echo \$VAR_$___name
}

#
# get_attr: Get the attribute value of KEY
#
# Attributes are recorded in the shell variables named "ATTR_<KEY>"
#
get_attr () {
    ___name=$1

    eval echo \$ATTR_$___name
}

# variant of get_attr for list (separated by ',')
get_attr_l () {
    (IFS=', '; echo "$(get_attr $1)")
}

# Remove ${varname} part in the beginning of a string.
remove_var_expr () {
    ___varname=$1
    shift

    expr "$*" : "\${$___varname}\\(.*\\)"
}

# Given a string, substitute variables.
substitute_vars () {
    __string="$1"
    __varname=""
    __result=""

    while [ -n "$__string" ]; do
	case "$__string" in
	    \$\$*)
		__result="$__result\$"
		__string="${__string#\$\$}"
		;;
	    \${*}*)
		__varname="${__string#\$\{}"
		__varname="${__varname%%\}*}"
		__result="$__result$(get_var $__varname)"
		__string=$(remove_var_expr $__varname $__string)
		;;
	    *)
		__result="$__result$(printf %c "$__string")"
		__string="${__string#$(printf %c "$__string")}"
		;;
	esac
    done

    echo "$__result"
}

#
# Read a config from stdin
#
# Variables:
# For VAR=VALUE, value is stored in the shell variable VAR_*.
#
# Attributes:
# For KEY: VALUE, value is stored in the shell variable ATTR_*.
#
read_config_from_stdin () {
    _filename=$1
    _line=""
    _varname=""
    _value=""
    _key=""
    _reading_attrs=""

    while read _line; do
	if [ -z "$_line" ]; then
	    _reading_attrs=yes
	    continue
	elif [ -z "$_reading_attrs" ]; then
	    case "$_line" in
		*=*)
		    _varname="${_line%%=*}"
		    _value="${_line#*=}"
		    VAR_list="$VAR_list${VAR_list:+ }VAR_$_varname"
		    read VAR_$_varname <<EOF1
$(substitute_vars "$_value")
EOF1
		    continue
		    ;;
		*) _reading_attrs=yes ;;
	    esac
	fi
	if [ -n "$_reading_attrs" ]; then
	    case "$_line" in
		*:\ *)
		    _key="${_line%%:\ *}"
		    _value="${_line#*:\ }"
		    if expr "$_key" : ".*\..*" >/dev/null; then
			_key="${_key%.*}_${_key#*.}"
		    fi
		    ATTR_list="$ATTR_list${ATTR_list:+ }ATTR_$_key"
		    read ATTR_$_key <<EOF2
$(substitute_vars "$_value")
EOF2
		    ;;
		*:|*:\ ) ;;
		*)
		    echo "Error reading $_filename: $_line" 1>&2
		    exit 1
		    ;;
	    esac
	fi
    done
}


find_file_in_path () {
    _f=$1
    _p=$2
    _saved_IFS="$IFS"
    _arg=""
    IFS=":"		# On Windows it should be ";"???

    for _arg in $_p; do
	if [ -r $_arg/$_f ]; then
	    RESULT="$_arg/$_f"
	    IFS="$_saved_IFS"
	    return 0
	fi
    done
    IFS="$_saved_IFS"
    RESULT=""
    return 1
}

read_config_file () {
    if ! find_file_in_path $1.pc $2; then
	if [ -z "$want_exists" ]; then
	    echo "Can't find $1.pc" 1>&2
	fi
	exit 1
    fi
    read_config_from_stdin $RESULT < $RESULT
}

cleanup_vars_attrs () {
    eval unset $VAR_list VAR_list
    eval unset $ATTR_list ATTR_list
}

not_listed_yet () {
    ___m=$1
    ___arg=""
    shift

    for ___arg; do
	if [ $___m = $___arg ]; then
	    return 1
	fi
    done

    return 0
}

list_only_once () {
    __result=""
    __arg=""

    for __arg; do
	if not_listed_yet $__arg $__result; then
	    __result="$__result${__result:+ }$__arg"
	fi
    done

    echo $__result
}

l