#!/usr/bin/env bash
#***********************************************************************************************************
#
# Starfish Storage Corporation ("Starfish") CONFIDENTIAL
# Unpublished Copyright (c) 2011 - present Starfish Storage Corporation, All Rights Reserved.
#
# NOTICE: This file and its contents (1) constitute Starfish's "External Code" under Starfish's most-recent
# Limited Software End-User License Agreement, and (2) is and remains the property of Starfish. The
# intellectual and technical concepts contained herein are proprietary to Starfish and may be covered by
# U.S. and/or foreign patents or patents in process, and are protected by trade secret or copyright law.
# Dissemination of this information or reproduction of this material is strictly forbidden unless prior
# written permission is obtained from Starfish. Access to the source code contained herein is hereby
# forbidden to anyone except (A) current Starfish employees, managers, or contractors who have executed
# confidentiality or nondisclosure agreements explicitly covering such access, and (B) licensees of
# Starfish's software.
#
# ANY REPRODUCTION, COPYING, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE, OR PUBLIC DISPLAY OF OR
# THROUGH USE OF THIS SOURCE CODE WITHOUT THE EXPRESS WRITTEN CONSENT OF STARFISH IS STRICTLY PROHIBITED
# AND IS IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES. THE RECEIPT OR POSSESSION OF THIS
# FILE OR ITS CONTENTS AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE,
# DISCLOSE, OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN
# WHOLE OR IN PART.
#
# FOR U.S. GOVERNMENT CUSTOMERS REGARDING THIS DOCUMENTATION/SOFTWARE
#   These notices shall be marked on any reproduction of this data, in whole or in part.
#   NOTICE: Notwithstanding any other lease or license that may pertain to, or accompany the delivery of,
#   this computer software, the rights of the Government regarding its use, reproduction and disclosure are
#   as set forth in Section 52.227-19 of the FARS Computer Software-Restricted Rights clause.
#   RESTRICTED RIGHTS NOTICE: Use, duplication, or disclosure by the Government is subject to the
#   restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer
#   Software clause at DFARS 52.227-7013.
#
#***********************************************************************************************************

set -euo pipefail

readonly BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly REDASH_DIR="$(readlink -ev "${BIN_DIR}/..")"
readonly REDASH_VERSION="$(basename "${REDASH_DIR}")"
readonly REDASH_MAJOR_VERSION="$(echo "${REDASH_VERSION}" | cut -f 1 -d .)"
readonly REDASH_PG_DIR="${REDASH_DIR}/pg"
readonly STARFISH_DIR="$(readlink -ev "${BIN_DIR}/../../..")"

# shellcheck source=scripts/installation/_redash_common.sh
source "${STARFISH_DIR}/data/installation/_redash_common.sh"


show_redash_systemd_status() {
    local redash_systemd_unit_name

    redash_systemd_unit_name="$(get_redash_service_manager_name "${REDASH_MAJOR_VERSION}")"
    systemctl --no-pager --all list-units "${redash_systemd_unit_name}*.service" || true
}

show_redash_pg_status() {
    local params="${REDASH_PG_DIR}/_params.sh"
    local pg_common="${REDASH_PG_DIR}/_pg_common.sh"

    if [[ ! -d "${REDASH_PG_DIR}" ]]; then
        echo "${REDASH_PG_DIR} does not exist"
        return
    fi
    if [[ ! -f "${params}" ]]; then
        echo "${params} does not exist"
        return
    fi
    if [[ ! -f "${pg_common}" ]]; then
        echo "${pg_common} does not exist"
        return
    fi

    # shellcheck source=/dev/null
    source "${params}"
    # shellcheck source=scripts/installation/_pg_common.sh
    source "${pg_common}"

    # PG_VERSION, PG_CLUSTER_NAME and PORT are set in _params.sh
    postgres_action status "${PG_VERSION}" "${PG_CLUSTER_NAME}" "${PORT}"
}

show_redash_status() {
    local manage="${REDASH_DIR}/bin/manage"

    if [[ ! -x "${manage}" ]]; then
        echo "${manage} does not exist or is not executable"
        return
    fi
    echo "Redash $(${manage} version)"
    "${manage}" status
}

show_redash_systemd_status
show_redash_pg_status
show_redash_status
