#
# Copyright (c) 2024-2026, 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.
#

echo "CMA_HOME: $CMA_HOME"
echo "CMA_PYTHON_EXECUTABLE: $CMA_PYTHON_EXECUTABLE"

export SCRIPT_PATH="${SCRIPT_PATH:-${CMA_HOME}/bin}"

source "$CMA_HOME"/bin/utils.sh # load utils

if [ -n "${CMA_SERVER_SSL_CONF_FILE}" ]; then

    if [ ! -f "${CMA_SERVER_SSL_CONF_FILE}" ]; then
      echo "Could not open ${CMA_SERVER_SSL_CONF_FILE}!"
      exit 1
    fi

    if [ -z "${CMA_KEYSTORE_TYPE}" ]; then
      export CMA_KEYSTORE_TYPE=$(get_cma_conf_value "cma.server.ssl.key-store-type" ${CMA_SERVER_SSL_CONF_FILE})
    fi

    if [ -z "${CMA_KEYSTORE_FILE}" ]; then
      export CMA_KEYSTORE_FILE=$(get_cma_conf_value "cma.server.ssl.key-store" ${CMA_SERVER_SSL_CONF_FILE})
    fi

    if [ -z "${CMA_KEYSTORE_PASSWORD}" ]; then
      export CMA_KEYSTORE_PASSWORD=$(get_cma_conf_value "cma.server.ssl.key-store-password" ${CMA_SERVER_SSL_CONF_FILE})
    fi

    if [ -z "${CMA_KEY_PASSWORD}" ]; then
      export CMA_KEY_PASSWORD=$(get_cma_conf_value "cma.server.ssl.key-password" ${CMA_SERVER_SSL_CONF_FILE})
    fi

    if [ -z "${CMA_KEY_ALIAS}" ]; then
      export CMA_KEY_ALIAS=$(get_cma_conf_value "cma.server.ssl.key-alias" ${CMA_SERVER_SSL_CONF_FILE})
    fi
fi

if [ -n "${CMA_CLIENT_SSL_CONF_FILE}" ]; then

    if [ ! -f "${CMA_CLIENT_SSL_CONF_FILE}" ]; then
      echo "Creating ${CMA_CLIENT_SSL_CONF_FILE} with default truststore settings..."
      mkdir -p "$(dirname "${CMA_CLIENT_SSL_CONF_FILE}")"
      cat > "${CMA_CLIENT_SSL_CONF_FILE}" <<EOF
cma.client.ssl.trust-store=file:${DEFAULT_TRUSTSTORE_PATH}
cma.client.ssl.trust-store-password=${DEFAULT_TRUSTSTORE_PASSWORD}
cma.client.ssl.trust-store-type=${DEFAULT_TRUSTSTORE_TYPE}
EOF
    fi

    if [ -z "${CMA_TRUSTSTORE_TYPE}" ]; then
      export CMA_TRUSTSTORE_TYPE=$(get_cma_conf_value "cma.client.ssl.trust-store-type" ${CMA_CLIENT_SSL_CONF_FILE})
    fi

    if [ -z "${CMA_TRUSTSTORE_FILE}" ]; then
      export CMA_TRUSTSTORE_FILE=$(get_cma_conf_value "cma.client.ssl.trust-store" ${CMA_CLIENT_SSL_CONF_FILE})
    fi

    if [ -z "${CMA_TRUSTSTORE_PASSWORD}" ]; then
      export CMA_TRUSTSTORE_PASSWORD=$(get_cma_conf_value "cma.client.ssl.trust-store-password" ${CMA_CLIENT_SSL_CONF_FILE})
    fi

    # CM may have created this file with empty values when TLS is disabled (trust-store=file:, password=, type=jks).
    # Detect that case, fill in defaults, and write them back to the file.
    if [ -z "${CMA_TRUSTSTORE_TYPE}" ] && [ -n "${DEFAULT_TRUSTSTORE_TYPE}" ]; then
      export CMA_TRUSTSTORE_TYPE="${DEFAULT_TRUSTSTORE_TYPE}"
      set_cma_conf_value "cma.client.ssl.trust-store-type" "${CMA_TRUSTSTORE_TYPE}" "${CMA_CLIENT_SSL_CONF_FILE}"
    fi
    if [ "${CMA_TRUSTSTORE_FILE}" = "file:" ] || [ -z "${CMA_TRUSTSTORE_FILE}" ]; then
      if [ -n "${DEFAULT_TRUSTSTORE_PATH}" ]; then
        export CMA_TRUSTSTORE_FILE="file:${DEFAULT_TRUSTSTORE_PATH}"
        set_cma_conf_value "cma.client.ssl.trust-store" "${CMA_TRUSTSTORE_FILE}" "${CMA_CLIENT_SSL_CONF_FILE}"
      fi
    fi
    if [ -z "${CMA_TRUSTSTORE_PASSWORD}" ] && [ -n "${DEFAULT_TRUSTSTORE_PASSWORD}" ]; then
      export CMA_TRUSTSTORE_PASSWORD="${DEFAULT_TRUSTSTORE_PASSWORD}"
      set_cma_conf_value "cma.client.ssl.trust-store-password" "${CMA_TRUSTSTORE_PASSWORD}" "${CMA_CLIENT_SSL_CONF_FILE}"
    fi
fi

if [ -f "${CMA_AGENT_AUTH_CONF_FILE}" ]; then
  echo "Skipping creating ${CMA_AGENT_AUTH_CONF_FILE} since it already exists."
  export SKIP_CREATING_CMA_AGENT_AUTH_CONF_FILE=true
fi

export CMA_KEY_ALIAS=${CMA_KEY_ALIAS:-$(hostname -f)}
echo "CMA_KEY_ALIAS=$CMA_KEY_ALIAS"

# Master roles receive *-client.properties from peerConfigGenerators (relative, in CONF_DIR).
# The agent receives *-server.properties under cma-master-conf/ from the CMA Master gateway
# section. cma-agent-server.sh exports CMA_MASTER_*_CONF_FILE pointing to those paths, so
# fall back to them when the *-client.properties names are absent.
_cma_auth_conf=""
if [ -f "cma-auth-client.properties" ]; then
  _cma_auth_conf="cma-auth-client.properties"
elif [ -f "${CMA_MASTER_AUTH_SERVER_CONF_FILE}" ]; then
  _cma_auth_conf="${CMA_MASTER_AUTH_SERVER_CONF_FILE}"
elif [ -f "${DEFAULT_CMA_MASTER_AUTH_SERVER_CONF_FILE}" ]; then
  _cma_auth_conf="${DEFAULT_CMA_MASTER_AUTH_SERVER_CONF_FILE}"
fi
if [ -n "${_cma_auth_conf}" ]; then
  host=$(awk -F: 'NR==1{print $1}' "${_cma_auth_conf}")
  export CMA_AUTH_SERVER_HOSTNAME="$host"
  export CMA_AUTH_SERVER_HTTP_PORT=$(awk -F'=' '/cma.auth.server.http.port/ {print $2}' "${_cma_auth_conf}")
  export CMA_AUTH_SERVER_HTTPS_PORT=$(awk -F'=' '/cma.auth.server.https.port/ {print $2}' "${_cma_auth_conf}")
fi

_cma_gateway_conf=""
if [ -f "cma-gateway-client.properties" ]; then
  _cma_gateway_conf="cma-gateway-client.properties"
elif [ -f "${CMA_MASTER_GATEWAY_SERVER_CONF_FILE}" ]; then
  _cma_gateway_conf="${CMA_MASTER_GATEWAY_SERVER_CONF_FILE}"
elif [ -f "${DEFAULT_CMA_MASTER_GATEWAY_SERVER_CONF_FILE}" ]; then
  _cma_gateway_conf="${DEFAULT_CMA_MASTER_GATEWAY_SERVER_CONF_FILE}"
fi
if [ -n "${_cma_gateway_conf}" ]; then
  host=$(awk -F: 'NR==1{print $1}' "${_cma_gateway_conf}")
  export CMA_GATEWAY_SERVER_HOST="$host"
  export CMA_GATEWAY_SERVER_HTTP_PORT=$(awk -F'=' '/cma.gateway.server.http.port/ {print $2}' "${_cma_gateway_conf}")
  export CMA_GATEWAY_SERVER_HTTPS_PORT=$(awk -F'=' '/cma.gateway.server.https.port/ {print $2}' "${_cma_gateway_conf}")
fi

_cma_server_conf=""
if [ -f "cma-server-client.properties" ]; then
  _cma_server_conf="cma-server-client.properties"
elif [ -f "${CMA_MASTER_SERVER_CONF_FILE}" ]; then
  _cma_server_conf="${CMA_MASTER_SERVER_CONF_FILE}"
elif [ -f "${DEFAULT_CMA_MASTER_SERVER_CONF_FILE}" ]; then
  _cma_server_conf="${DEFAULT_CMA_MASTER_SERVER_CONF_FILE}"
fi
if [ -n "${_cma_server_conf}" ]; then
  host=$(awk -F: 'NR==1{print $1}' "${_cma_server_conf}")
  export CMA_SERVER_HOSTNAME="$host"
  export CMA_SERVER_DISTRIBUTED_HTTP_PORT=$(awk -F'=' '/cma.server.http.port/ {print $2}' "${_cma_server_conf}")
  export CMA_SERVER_DISTRIBUTED_HTTPS_PORT=$(awk -F'=' '/cma.server.https.port/ {print $2}' "${_cma_server_conf}")
  export CMA_SERVER_GRPC_PORT=$(awk -F'=' '/cma\.grpc\.port/ {print $2}' "${_cma_server_conf}")
fi

export CMA_RUN_IN_BACKGROUND=false


__move_configs_from_vault_client_config_to_cma_vault_config() {
  while IFS=: read -r host kv; do
    VAULT_SERVER_HOST="$host"
    key="${kv%%=*}"
    value="${kv#*=}"
    case "$key" in
      vault.server.port) VAULT_SERVER_PORT="$value" ;;
      vault.ssl.enabled) VAULT_SERVER_SCHEME=$([ "$value" = "true" ] && echo https || echo http) ;;
    esac
  done < "${CMA_VAULT_CLIENT_CONF_FILE}"
  if [ ! -f "$CMA_VAULT_SERVER_CONF_FILE" ]; then
      touch "$CMA_VAULT_SERVER_CONF_FILE"
  fi
}

# Parse $EXTERNAL_VAULT_URL → VAULT_SERVER_SCHEME, VAULT_SERVER_HOST, VAULT_SERVER_PORT
parse_external_vault_url() {
  local re='^([a-zA-Z][a-zA-Z0-9+.-]*)://([^/:]+):([0-9]+)(/?|/.*)$'

  if [[ -z "$EXTERNAL_VAULT_URL" ]]; then
    echo "EXTERNAL_VAULT_URL is unset or empty" >&2
    exit 1
  fi

  if [[ $EXTERNAL_VAULT_URL =~ $re ]]; then
    VAULT_SERVER_SCHEME="${BASH_REMATCH[1]}"
    VAULT_SERVER_HOST="${BASH_REMATCH[2]}"
    VAULT_SERVER_PORT="${BASH_REMATCH[3]}"
  else
    echo "Invalid EXTERNAL_VAULT_URL: expected scheme://host:port[/path]" >&2
    exit 1
  fi
}

source "$CMA_HOME"/bin/distributed-common.sh # load commons
create_logs_dir
LOG_FILE="$LOGS_DIR"/$(basename "$0" .sh).log

# Normalize deprecated 'cm_postgres' to 'postgres' early, before any DB type checks.
normalize_db_type

export CMA_INSTANCE_NAME=${CMA_INSTANCE_NAME:-"cma-server"}

#
# locate and verify java_home
# This function is used by CSD!
#
locate_and_verify_java_home() {
  if [ -n "$CMA_JAVA_HOME" ]; then
    export JAVA_HOME=$CMA_JAVA_HOME
    if [ "${CMA_SKIP_JAVA_CHECK}" = true ]; then
      echo "JAVA_HOME: $JAVA_HOME"
      echo "Java verification skipped!"
      return 0
    fi

    # make sure it is selected Java 11 or later.
    get_java_major_version JAVA_MAJOR
    if [ $JAVA_MAJOR -lt ${JAVA_MINIMUM_MAJOR} ]; then
      echo "Java ${JAVA_MAJOR} detected at ${JAVA_HOME}. Need at least version 11. Ignoring and locating a valid Java version"
      unset JAVA_HOME
      locate_cma_java_home
    fi
  else
    locate_cma_java_home
    if [ "${CMA_SKIP_JAVA_CHECK}" = true ]; then
      echo "JAVA_HOME: $JAVA_HOME"
      echo "Java verification skipped!"
      return 0
    fi
  fi

  verify_cma_java_home
}

#
# locate and verify python
# This function is used by CSD!
#
locate_and_verify_python_executable() {
  echo "CMA_PYTHON_EXECUTABLE: $CMA_PYTHON_EXECUTABLE"
  if [ "${CMA_SKIP_PYTHON_CHECK}" = true ]; then
    export PYTHON_EXECUTABLE=$CMA_PYTHON_EXECUTABLE
    echo "Python verification skipped!"
    return 0
  fi

  if [ -n "$CMA_PYTHON_EXECUTABLE" ]; then
    if ! verify_python_version $CMA_PYTHON_EXECUTABLE; then
      local py_version=($(get_python_version $CMA_PYTHON_EXECUTABLE))
      echo "Python ${py_version[@]} detected at ${CMA_PYTHON_EXECUTABLE}. Need at least version Python$PYTHON_MINIMUM_MAJOR.$PYTHON_MINIMUM_MINOR.$PYTHON_MINIMUM_PATCH+. Ignoring and locating a valid Python version"
      unset PYTHON_EXECUTABLE
    fi
  fi

  if [ -z "${PYTHON_EXECUTABLE}" ]; then
    echo "Searching a suitable python executable.."
    locate_and_verify_cma_python_executable
  fi
  echo "Python verification done!"
}

setup_tls_protocol_and_cyphers() {
  if [ ${CMA_TLS_MIN_PROTOCOL} = "TLSv1.2" ]; then
    export CMA_SSL_PROTOCOLS="TLSv1.2,TLSv1.3"
    export CMA_SSL_CIPHERS=${CMA_TLS_V1_2_CIPHERS},${CMA_TLS_V1_3_CIPHERS}
    echo "Setting TLSv1.2 as the minimum protocol version with TLSv1.2 and TLSv1.3 ciphers provided"
  elif [ ${CMA_TLS_MIN_PROTOCOL} = "TLSv1.3" ]; then
    export CMA_SSL_PROTOCOLS="TLSv1.3"
    export CMA_SSL_CIPHERS=${CMA_TLS_V1_3_CIPHERS}
    echo "Setting TLSv1.3 as the minimum protocol version with TLSv1.3 ciphers provided"
  fi
}

#
# Starts the cma.
# This function is called by CSD!
#
csd_start_cma_server() {
  locate_and_verify_java_home
  wait_for_auth_server_running

  if [ "$CMA_VAULT" = true ]; then
    if [[ -n "${EXTERNAL_VAULT_URL}" ]]; then
      parse_external_vault_url
    else
      __move_configs_from_vault_client_config_to_cma_vault_config
    fi
    create_cma_vault_server_conf
    ensure_vault_authentication "csd"
  fi

  CMA_SERVER_PID_FILE="${CMA_SERVER_PID_FILE:-/var/run/cma-server.pid}"

  backup_or_restore_cma_db_if_version_changed

  echo "Reset the cma-server.env"
  create_cma_server_env

  setup_tls_protocol_and_cyphers
  sed -i 's|\(cma\.server\.ssl\.key-store=\)/|\1file:/|' "$CMA_SERVER_SSL_CONF_FILE"

  # Call configure_postgres in the parent shell so its exports
  # (CMA_DBA_USER, CMA_DBA_PASS, CMA_DB_HOST, etc.) persist.
  # get_server_profiles() also calls it, but inside $() those exports are lost.
  if [ "${CMA_DB_TYPE:-h2}" = "postgres" ]; then
    configure_postgres
  fi

  echo "Starting the CMA server"
  export CMA_DISPLAY_SSL_SETTINGS=false
  export SKIP_JAVA_CHECK=$CMA_SKIP_JAVA_CHECK
  export SKIP_PORT_CHECK=$CMA_SKIP_PORT_CHECK
  export SKIP_SSL_CHECK=$CMA_SKIP_SSL_CHECK
  export CMA_SERVER_PROFILES=$(get_server_profiles)
  export CMA_SERVER_ENV_FILE
  ensure_internal_keystore_password "${CMA_CONF_DIR:-${CONF_DIR}}"
  ensure_grpc_keystore "${CMA_CONF_DIR:-${CONF_DIR}}"
  exec $CMA_HOME/cma-server/cma-server.sh --start --am2cm-root-dir="${CMA_HOME}"
}

csd_start_cma_auth_server() {
  locate_and_verify_java_home
  setup_tls_protocol_and_cyphers
  if [ "${CMA_DB_TYPE:-h2}" = "postgres" ]; then
    if configure_postgres; then
      export CMA_AUTH_SERVER_PROFILES="${CMA_AUTH_SERVER_PROFILES:+${CMA_AUTH_SERVER_PROFILES},}postgres"
    fi
  fi
  ensure_internal_keystore_password "${CMA_CONF_DIR:-${CONF_DIR}}"
  exec "$CMA_HOME"/cma-auth-server/cma-auth-server.sh start
}

csd_start_cma_gateway_server() {
  locate_and_verify_java_home
  setup_tls_protocol_and_cyphers
  wait_for_auth_server_running
  sed -i 's|\(cma\.server\.ssl\.key-store=\)/|\1file:/|' "$CMA_SERVER_SSL_CONF_FILE"
  # Needed so that the cma-gateway server can access cma-server via https requests
  if [ -f "$CONF_DIR/cm-auto-host_cert_chain.pem" ] && \
     ! grep -q '^cma\.server\.ssl\.cert\.pem=' "$CMA_SERVER_SSL_CONF_FILE"; then
    echo "cma.server.ssl.cert.pem=$CONF_DIR/cm-auto-host_cert_chain.pem" >> "$CMA_SERVER_SSL_CONF_FILE"
  fi
  exec "$CMA_HOME"/cma-gateway-server/cma-gateway-server.sh start
}

#
# Starts the cma agent.
# This function is called by CSD!
#
csd_start_cma_agent_server() {
  locate_and_verify_java_home

  # Resolve CMA_GATEWAY_URL:
  #   external mode    — CMA_GATEWAY_URL is set explicitly; use it as-is regardless of whether
  #                       a CMA_MASTER dependency is also wired (explicit URL always wins).
  #   co-located mode  — CMA_MASTER_SERVICE dependency is set and CMA_GATEWAY_URL is empty;
  #                       derive URL from the gateway config files parsed at script startup.
  if [ -n "${CMA_GATEWAY_URL}" ]; then
    echo "Using explicitly configured CMA_GATEWAY_URL: ${CMA_GATEWAY_URL}"
  elif [[ -n "${CMA_MASTER_SERVICE}" && "${CMA_MASTER_SERVICE}" != "none" ]]; then
    if [ -z "${CMA_GATEWAY_SERVER_HOST}" ]; then
      echo "Co-located mode: gateway config file was not found or is empty; cannot derive gateway URL." >&2
      exit 1
    fi
    local scheme port
    if [ "${CMA_SERVER_SSL}" = "true" ]; then
      scheme="https"
      port="${CMA_GATEWAY_SERVER_HTTPS_PORT}"
    else
      scheme="http"
      port="${CMA_GATEWAY_SERVER_HTTP_PORT}"
    fi
    export CMA_GATEWAY_URL="${scheme}://${CMA_GATEWAY_SERVER_HOST}:${port}"
  else
    echo "The CMA Agent cannot be started: neither CMA_MASTER_SERVICE nor CMA_GATEWAY_URL is configured." >&2
    exit 1
  fi

  load_cma_agent_auth_conf
  ensure_internal_keystore_password "${CMA_CONF_DIR:-${CONF_DIR}}"

  if [ "$CMA_VAULT" = true ]; then
    __move_configs_from_vault_client_config_to_cma_vault_config
    ensure_vault_authentication "csd"
  fi
  locate_and_verify_python_executable

  backup_or_restore_cma_db_if_version_changed

  echo "Reset the cma-server.env"
  create_cma_server_env

  setup_tls_protocol_and_cyphers
  sed -i 's|\(cma\.server\.ssl\.key-store=\)/|\1file:/|' "$CMA_SERVER_SSL_CONF_FILE"

  # Call configure_agent_postgres in the parent shell so its exports
  # (CMA_AGENT_DBA_USER, CMA_AGENT_DBA_PASS, CMA_AGENT_DB_HOST, etc.) persist.
  # get_server_profiles() also calls configure_postgres, but inside $() those exports are lost.
  if [ "${CMA_DB_TYPE:-h2}" = "postgres" ]; then
    configure_agent_postgres
    create_agent_db_conf
  else
    # H2 mode: write agent-db.properties so the password is file-based (no hardcoded fallback)
    create_agent_db_conf
  fi

  echo "Starting the CMA Agent server"
  export CMA_DISPLAY_SSL_SETTINGS=false
  export SKIP_JAVA_CHECK=$CMA_SKIP_JAVA_CHECK
  export SKIP_PORT_CHECK=$CMA_SKIP_PORT_CHECK
  export SKIP_SSL_CHECK=$CMA_SKIP_SSL_CHECK
  export CMA_AGENT_PROFILES=$(get_server_profiles)
#  export CMA_SERVER_ENV_FILE
  export CMA_AGENT_CONF_FILE
  exec $CMA_HOME/cma-agent/cma-agent-server.sh start
}

#
# Stops the cma auth server.
#
csd_stop_cma_auth_server() {
  echo "Stopping the Auth server.."
  stop_cma_auth_server
}

#
# Stops the cma gateway server.
#
csd_stop_cma_gateway_server() {
  echo "Stopping the Gateway server.."
  stop_cma_gateway_server
}


#
# Stops the cma server.
# This function is called by CSD!
#
csd_stop_cma_server() {
  echo "Stopping the CMA server"
  export CMA_SERVER_ENV_FILE
  $CMA_HOME/cma-server/cma-server.sh --stop
  EXIT_CODE=$?

  if [ "${EXIT_CODE}" != "0" ]; then
    echo "Failed to stop the CMA server!"
    exit $EXIT_CODE
  fi

  echo "Wait for the cma-server to stop"
  wait_for_cma_server_to_stop $CMA_SERVER_LOG_FILE
}

#
# Stops the cma agent.
# This function is called by CSD!
#
csd_stop_cma_agent_server() {
  echo "Stopping the CMA Agent server"
  export CMA_SERVER_ENV_FILE
  export CMA_AGENT_CONF_FILE
  $CMA_HOME/cma-agent/cma-agent-server.sh stop
  EXIT_CODE=$?

  if [ "${EXIT_CODE}" != "0" ]; then
    echo "Failed to stop the CMA Agent server!"
    exit $EXIT_CODE
  fi

  echo "Wait for the CMA Agent server to stop"
  CMA_AGENT_STOP_TIMEOUT=60
  if [ -n "${CMA_AGENT_CONF_FILE}" ]; then
    if [ ! -f "${CMA_AGENT_CONF_FILE}" ]; then
      echo "Could not open ${CMA_AGENT_CONF_FILE}!"
      exit 1
    fi

    CMA_AGENT_LOG_FILE=$(get_cma_conf_value "cma.agent.log.file" ${CMA_AGENT_CONF_FILE})
  fi

  if [ -z "${CMA_AGENT_LOG_FILE}" ]; then
    CMA_AGENT_LOG_FILE="${CMA_LOG_DIR}/cma-agent-server.log"
  fi

  wait_for_cma_agent_to_stop
}

#
# Initializes the python environment on the control node.
# This function is called by CSD!
#
csd_init_cma_python_environment() {
  locate_and_verify_python_executable

  echo "Delete virtual env if exists"
  # delete virtual env
  if ! delete_virtual_env; then
    exit 1
  fi

  echo "Initialize the CMA python environment"
  setup_control_node_python_with_dependencies || exit 1
}

#
# $1: the version of the csd
#
csd_check_parcel_compatibility() {
  local csd_version=${1:-${CSD_VERSION}}
  echo "csd_version: $csd_version"
  if [ -z "${csd_version}" ]; then
    echo "Not compatible due to missing csd version!"
    exit 1
  fi

  local csd_min_version=$(get_csd_min_version)
  echo "csd_min_version: $csd_min_version"
  if [ -z "${csd_min_version}" ]; then
    echo "Not compatible due to missing csd min version!"
    exit 1
  fi

  local res=`compare_versions ${csd_version} ${csd_min_version} >/dev/null 2>&1; echo $?`
#  echo "compare_versions: $res"
  if [ "$res" -lt 2 ]; then
    echo "The parcel is NOT compatible with CSD!"
    exit 1
  fi

  echo "The parcel is compatible with CSD!"
}
