#!/usr/bin/env bash
#
# Copyright (c) 2022-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.
#

ROOT_DIR=${VAULT_HOME:-$(dirname $(dirname $(cd $(dirname $(readlink -f "$0"))/ && pwd)))}
VENV_DIR="${ROOT_DIR}/venv"
LOGS_DIR="${ROOT_DIR}/logs"
DATA_DIR="${ROOT_DIR}/data"

VAULT_RESOURCES_DIR=$ROOT_DIR/resources
VAULT_TEMPLATES_DIR=$VAULT_RESOURCES_DIR/templates

VAULT_DIR=${VAULT_DIR:-$ROOT_DIR/vault}
VAULT_BIN=${VAULT_DIR}/vault
VAULT_CONF_DIR=${VAULT_CONF_DIR:-${VAULT_DIR}}
VAULT_CONF_FILE=${VAULT_CONF_DIR}/vault.hcl
VAULT_DATA_DIR=${DATA_DIR}/vault
VAULT_TOKEN_FILE=${VAULT_CONF_DIR}/.vault
VAULT_TOKEN_FILE_BAK=${VAULT_TOKEN_FILE_BAK:-"${HOME}/.vault-token.json"}
VAULT_SERVER_HOST=${VAULT_SERVER_HOST:-"127.0.0.1"}
VAULT_SERVER_PORT=${VAULT_SERVER_PORT:-"8210"}
VAULT_SERVER_SCHEME=${VAULT_SERVER_SCHEME:-"http"}
VAULT_SERVER_PID_DIR=${VAULT_SERVER_PID_DIR:-/var/run}
VAULT_SERVER_PID_FILE=${VAULT_SERVER_PID_FILE:-${VAULT_SERVER_PID_DIR}/vault.pid}
VAULT_SERVER_LOG_DIR=${VAULT_SERVER_LOG_DIR:-${ROOT_DIR}/logs}
VAULT_SERVER_CONF_DIR=${VAULT_CONF_DIR:-${VAULT_DIR}}
VAULT_SERVER_VERSION=${VAULT_SERVER_VERSION:-"1.14.8"}
VAULT_SERVER_SSL_CONF_FILE=${VAULT_SERVER_SSL_CONF_FILE:-"${VAULT_CONF_DIR}/ssl-server.properties"}
VAULT_SERVER_CONF_FILE=${VAULT_SERVER_CONF_FILE:-"${VAULT_SERVER_CONF_DIR}/vault-server.properties"}
VAULT_SERVER_CONF_FILE_BAK="${VAULT_SERVER_CONF_FILE}.bak"

source "${SCRIPT_PATH:-$ROOT_DIR/bin}"/utils.sh

create_logs_dir() {
  mkdir -p "${VAULT_SERVER_LOG_DIR}"
}

get_vault_token() {
  if [ ! -f "${VAULT_TOKEN_FILE}" ]; then
    exit 1
  fi
  echo $(cat ${VAULT_TOKEN_FILE} | ${PYTHON_EXECUTABLE} -c "import sys, json; print(json.load(sys.stdin)['root_token'])")
}

create_vault_server_conf() {

  mkdir -p ${VAULT_SERVER_CONF_DIR}
  echo 'Create vault-server.properties ...' | tee -a "$LOG_FILE"
  cat >"${VAULT_SERVER_CONF_FILE}" <<EOF
# vault server
vault.server.host=${VAULT_SERVER_HOST}
vault.server.port=${VAULT_SERVER_PORT}
vault.server.scheme=${VAULT_SERVER_SCHEME}
vault.server.pid.file=${VAULT_SERVER_PID_FILE}
vault.server.log.dir=${VAULT_SERVER_LOG_DIR}
vault.server.version=${VAULT_SERVER_VERSION}
EOF

  # secure config file
  chmod 600 ${VAULT_SERVER_CONF_FILE}
}

backup_vault_server_conf() {
  yes | cp "${VAULT_SERVER_CONF_FILE}" "${VAULT_SERVER_CONF_FILE_BAK}"
}

get_vault_server_pid() {
  if [ ! -f "${VAULT_SERVER_PID_FILE}" ]; then
    echo ''
  else
    cat "${VAULT_SERVER_PID_FILE}"
  fi
}

is_vault_server_running() {
  local pid=$(get_vault_server_pid)
  if [ -z "${pid}" -o "${pid}" = '' ]; then
    return 1
  fi
  kill -0 "$pid" >/dev/null 2>&1
}

backup_vault_token_file() {
  yes | cp "${VAULT_TOKEN_FILE}" "${VAULT_TOKEN_FILE_BAK}"
}