#
# Copyright (c) 2024, Cloudera, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# $1 the value, which may contain opening and/or closing quotes
remove_opening_and_closing_quotes() {
  echo $(sed -e 's/^"//' -e 's/"$//' <<< $1)
}

# Returns the value of the specified config
#
# $1 name of the config
# $2 config file, default ${CMA_SERVER_CONF_FILE}
get_cma_conf_value() {
  local conf_name=$1
  local conf_file=${2:-${CMA_SERVER_CONF_FILE}}

  if ! [ -f "$conf_file" ]; then
    echo "$conf_file does not exists"
    exit 1
  fi

  local conf_value=$(cat $conf_file | grep -v '#' | sed -E -n "s/$conf_name=(.*)/\1/p")
  echo "$conf_value"
}

# Set the value of the config in the config file
#
# $1 name of the env var
# $2 value of the env var
# $3 env file, default ${CMA_SERVER_CONF_FILE}
set_cma_conf_value() {
  local conf_name=$1
  local conf_value=$2
  local conf_file=${3:-${CMA_SERVER_CONF_FILE}}

  if ! [ -f "$conf_file" ]; then
    echo "$conf_file does not exists"
    return
  fi

  sed -i'.tmp' -E "s|^($conf_name=).*|\1${conf_value}|" $conf_file

  # check if the replacement was successful
  cat $conf_file | grep -q "$conf_name="
  if [ $? -ne 0 ]; then
    echo "$conf_name=$conf_value" >> $conf_file
  fi
}

# From https://stackoverflow.com/a/44660519/702738
#
# Compares two tuple-based, dot-delimited version numbers a and b (possibly
# with arbitrary string suffixes). Returns:
# 1 if a<b
# 2 if equal
# 3 if a>b
# Everything after the first character not in [0-9.] is compared
# lexicographically using ASCII ordering if the tuple-based versions are equal.
compare_versions() {
    if [[ $1 == "$2" ]]; then
        return 2
    fi
    local IFS=.
    local i a=(${1%%[^0-9.]*}) b=(${2%%[^0-9.]*})
    local arem=${1#${1%%[^0-9.]*}} brem=${2#${2%%[^0-9.]*}}
    for ((i=0; i<${#a[@]} || i<${#b[@]}; i++)); do
        if ((10#${a[i]:-0} < 10#${b[i]:-0})); then
            return 1
        elif ((10#${a[i]:-0} > 10#${b[i]:-0})); then
            return 3
        fi
    done
    if [ "$arem" '<' "$brem" ]; then
        return 1
    elif [ "$arem" '>' "$brem" ]; then
        return 3
    fi
    return 2
}

# $1 the string to be lowered
to_lower() {
  echo $1 | tr '[:upper:]' '[:lower:]'
}

get_current_ip_addr() {
  if [[ $(uname) == 'Linux' ]]; then
    hostname -I | awk '{print $1}'
  else
    ipconfig getifaddr en0
  fi
}

# $1 name of the env var
# $2 env file
get_cma_environment_variable_value() {
  local env_name=$1
  local env_file=${2:-${CMA_SERVER_ENV_FILE}}

  if ! [ -f "$env_file" ]; then
    echo "$env_file does not exists"
    exit 1
  fi

  local env_value=$(cat $env_file | grep -v '#' | sed -E -n "s/export $env_name=(.*)/\1/p")
  echo "$env_value"
}

# Set the value of the env var in the environment file
# $1 name of the env var
# $2 value of the env var
# $3 env file, default ${CMA_SERVER_ENV_FILE}
set_cma_environment_variable_value() {
  local env_name=$1
  local env_value=$2
  local env_file=${3:-${CMA_SERVER_ENV_FILE}}

  if ! [ -f "$env_file" ]; then
    echo "$env_file does not exists"
    return
  fi

  sed -i'.tmp' -E "s/^(export $env_name=).*/\1${env_value}/" $env_file

  # check if the replacement was successful
  cat $env_file | grep -q "export $env_name="
  if [ $? -ne 0 ]; then
    echo "export $env_name=$env_value" >> $env_file
  fi
}

get_openssl_version() {
  openssl version -v | awk -F ' ' {'print $2'}
}

