0
  7   uˆl–Ãa¾”Ên-jq@=p\ÊbÑ¿�þBË"Î7r8ãáÀçø„th?÷Ãõ ?÷     u# Copyright (C) 2017-2024 MariaDB
# Copyright (C) 2012-2015 Codership Oy
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston
# MA  02110-1335  USA.

# This is a common command line parser and common functions to
# be sourced by other SST scripts.

trap 'exit 32' HUP PIPE
trap 'exit 3'  INT QUIT TERM

OS="$(uname)"

# Setting the paths for some utilities on CentOS
export PATH="${PATH:+$PATH:}/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin"
if [ "$OS" != 'Darwin' ]; then
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}/usr/local/lib:/lib:/usr/lib:/opt/lib"
fi

safe()
{
  if [[ "${!1}" = *[\ \'\`\$]* ]]; then
    wsrep_log_error "Invalid value for $1: ${!1}"
    exit 21
  fi
  echo "${!1}"
}

commandex()
{
    if [ -n "$BASH_VERSION" ]; then
        command -v "$1" || :
    elif [ -x "$1" ]; then
        echo "$1"
    else
        which "$1" || :
    fi
}

with_bash_42=0
with_printf=1
if [ -z "$BASH_VERSION" ]; then
    [ -z "$(commandex printf)" ] && with_printf=0
else
    [ "${BASH_VERSINFO[0]}" -eq 4 -a "${BASH_VERSINFO[1]}" -ge 2 -o \
      "${BASH_VERSINFO[0]}" -gt 4 ] && with_bash_42=1
fi

wsrep_log()
{
    local t
    # echo everything to stderr so that it gets into common error log
    # deliberately made to look different from the rest of the log
    if [ "$OS" = 'Linux' ]; then
        t=$(date '+%Y%m%d %H:%M:%S.%3N')
    elif [ $with_bash_42 -ne 0 ]; then
        printf -v t '%(%Y%m%d %H:%M:%S)T.000'
    else
        t=$(date '+%Y%m%d %H:%M:%S.000')
    fi
    echo "WSREP_SST: $* ($t)" >&2
}

wsrep_log_error()
{
    wsrep_log "[ERROR] $*"
}

wsrep_log_warning()
{
    wsrep_log "[WARNING] $*"
}

wsrep_log_info()
{
    wsrep_log "[INFO] $*"
}

trim_string()
{
    if [ -n "$BASH_VERSION" ]; then
        local pattern="[![:space:]${2:-}]"
        local x="${1#*$pattern}"
        local z=${#1}
        x=${#x}
        if [ $x -ne $z ]; then
            local y="${1%$pattern*}"
            y=${#y}
            x=$(( z-x-1 ))
            y=$(( y-x+1 ))
            echo "${1:$x:$y}"
        else
            echo ''
        fi
    else
        local pattern="[[:space:]${2:-}]"
        echo "$1" | sed -E "s/^$pattern+|$pattern+\$//g"
    fi
}

trim_dir()
{
    if [ -n "$BASH_VERSION" ]; then
        local pattern="![:space:]${2:-}"
        local x="${1#*[$pattern]}"
        local z=${#1}
        x=${#x}
        if [ $x -ne $z ]; then
            local y="${1%[$pattern/]*}"
            y=${#y}
            x=$(( z-x-1 ))
            y=$(( y-x+1 ))
            x="${1:$x:$y}"
            [ -z "$x" ] && x='.'
            echo "$x"
        else
            echo ''
        fi
    else
        local pattern="[:space:]${2:-}"
        local x=$(echo "$1" | sed -E "s/^[$pattern]+|[$pattern/]+\$//g")
        if [ -n "$x" ]; then
            echo "$x"
        elif "${1#*/}" != "$1"; then
            echo '.'
        else
            echo ''
        fi
    fi
}

trim_right()
{
    if [ -n "$BASH_VERSION" ]; then
        local pattern="[![:space:]${2:-}]"
        local z=${#1}
        local y="${1%$pattern*}"
        y=${#y}
        if [ $y -ne $z ]; then
            y=$(( y+1 ))
            echo "${1:0:$y}"
        else
            echo ''
        fi
    else
        local pattern="[[:space:]${2:-}]"
        echo "$1" | sed -E "s/$pattern+\$//g"
    fi
}

trim_left()
{
    if [ -n "$BASH_VERSION" ]; then
        local pattern="[![:space:]${2:-