#!/bin/bash

function edoc2_dir_create() {
    $SSH ${DEPLOYED_MANAGER} "mkdir -p ${DATA_PATH}/{edoc2Docs,edrmscore}"
    $SSH ${DEPLOYED_MANAGER} "mkdir -p ${WEBOFFICE_DATA_PATH}/{Data,onlyoffice,postgresql,log}"
}

function subregion_edoc2_dir_create() {
    $SSH ${DEPLOYED_MANAGER} "mkdir -p ${DATA_PATH}/{edoc2Docs,edrmscore,redis}"
    $SSH ${DEPLOYED_MANAGER} "mkdir -p ${WEBOFFICE_DATA_PATH}/{Data,onlyoffice,postgresq,log}"
}

function every_edoc2_dir_create() {
    for ip in ${IP_LIST[@]}; do
        log_info "${ip} create edoc2Docs data directory."
        $SSH root@${ip} "mkdir -p ${DATA_PATH}/{edoc2Docs,edrmscore}"
        $SSH ${DEPLOYED_MANAGER} "mkdir -p ${WEBOFFICE_DATA_PATH}/{Data,onlyoffice,postgresql,log}"
        log_success "${ip} create edoc2Docs data directory complete."
    done
}

function middleware_dir_create() {
    check_middleware_share_storage ${MIDDLEWARE_DATA_PATH}
    for _ip in ${MIDDLEWARE_NODE[@]}; do
        log_info "[${_ip}]: create middleware data directory and database backup directory."
        $SSH root@${_ip} "mkdir -p ${MIDDLEWARE_DATA_PATH}/{mysql,redis,es,rabbitmq}"
        $SSH root@${_ip} "mkdir -p ${DBBACKUP_PATH}/dbbackup"
        $SSH root@${_ip} "mkdir -p ${ESBACKUP_PATH}/esbackup"
        log_info "[${_ip}]: create middleware data directory and database backup directory complete."
    done
}

function data_path_create() {
    case ${STORAGE_MODE} in
    local)
        if [ ${#INDRIVE_NODE[@]} -ne 1 ]; then
            log_error "INDRIVE_NODE num is ${#INDRIVE_NODE[@]} not equal 1, but STORAGE_MODE=local, STORAGE_MODE configure error!!"
            exit 3
        fi
        edoc2_dir_create
        ;;
    nas)
        if [ ${#INDRIVE_NODE[@]} -eq 1 ]; then
            edoc2_dir_create
        else
            log_info "DATA_PATH must be shared storage, check it now."
            check_share_storage ${DATA_PATH}
            if [ "${REGION}" == "mainregion" ]; then
                edoc2_dir_create
            elif [ "${REGION}" == "subregion" ]; then
                subregion_edoc2_dir_create
            fi
        fi
        ;;
    s3 | swift | oss | ceph)
        every_edoc2_dir_create
        ;;
    *)
        log_error "Not supported STORAGE_MODE"
        exit 1
        ;;
    esac
}

function check_share_storage() {
    if [ -z "${1}" ]; then
        log_error "STORAGE_PATH configuration error."
        exit 1
    fi

    if [ ${#INDRIVE_NODE[@]} -ne 1 ]; then
        for _ip in ${INDRIVE_NODE[@]}; do
            # fix grep problem, modified "${1}" --> " ${1} "
            mounted_on=$($SSH root@${_ip} df -h $1 | awk 'NR!=1{print $NF}')
            $SSH root@${_ip} mount | grep -Evw "xfs|ext4" | grep " ${mounted_on} " &>/dev/null || SHARE_STORAGE_STATUS=1
            if [ "${SHARE_STORAGE_STATUS}" == "1" ]; then
                log_error "${_ip}: Please mount share storage on ${1}!"
                exit 1
            fi
        done
    fi
}

function check_middleware_share_storage() {
    if [ -z "${1}" ]; then
        log_error "MIDDLEWARE_DATA_PATH configuration error."
        exit 1
    fi

    if [ ${#MIDDLEWARE_NODE[@]} -ne 1 -a ${MIDDLEWARE_MODE} == "standalone" ]; then
        log_error "Middleware node number MUST BE 1 when MIDDLEWARE_MODE=standalone. Please check you configuration."
        exit 1
    fi

    if [ ${#MIDDLEWARE_NODE[@]} -ne 3 -a ${MIDDLEWARE_MODE} == "cluster" ]; then
        log_error "Middleware node number MUST BE 3 when MIDDLEWARE_MODE=cluster. Please check you configuration."
        exit 1
    fi

    if [ "${MIDDLEWARE_MODE}" == "cluster" ]; then
        for _ip in ${MIDDLEWARE_NODE[@]}; do
            # fix grep problem, modified "${1}" --> " ${1} "
            mounted_on=$($SSH root@${_ip} df -h $1 | awk 'NR!=1{print $NF}')
            $SSH root@${_ip} mount | grep -Evw "xfs|ext4" | grep " ${mounted_on} " &>/dev/null && MIDDLEWARE_SHARE_STORAGE_STATUS=0 || MIDDLEWARE_SHARE_STORAGE_STATUS=1
            if [ "${MIDDLEWARE_SHARE_STORAGE_STATUS}" == "1" ]; then
                continue
            else
                log_error "${_ip}: MIDDLEWARE_DATA_PATH can not be share storage when MIDDLEWARE_MODE=cluster."
                exit 1
            fi
        done
    fi 
}

function middleware_data_path_create() {
    case ${MIDDLEWARE_MODE} in
    standalone)
        middleware_dir_create
        ;;
    cluster)
        middleware_cluster_dir_create
        ;;
    esac
}

