#!/bin/bash

function prepare() {
    hostname_check
    system_version_check
    logserver_resolver
    path_mount_point_compare ${MIDDLEWARE_DATA_PATH} ${DBBACKUP_PATH}
    path_mount_point_compare ${MIDDLEWARE_DATA_PATH} ${ESBACKUP_PATH}
    storage_check
}

# @string_check
# Tools function. Check if $2 is "".
function string_check() {
    if [ -z "$2" ]; then
        log_error "check failed, please config $1 values"
        exit 1
    fi
}

# @hostname_check
# Check hostname is standard or.
# 1. Host name can not contain space
# 2. Host name can not start with digital
# 3. Host name can not contain punctuation except '.','-'
# example:
# bad: 1edoc2-app、edoc2app 01、edoc2@app01
# good: edoc2-app、edoc2app.01、edoc2-app01
function hostname_check() {
    local INVALID_CHAR_POSIX_STYLE=('[[:space:]]' '[[:digit:]]' '[[:punct:]]')
    hostname=$(hostname)
    if ! echo "${hostname}" | grep -P "^\b(?:[A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b)$" &>/dev/null; then
        log_error "1. Host name can not contain space 2. Host name can not start with digital 3. Host name can not contain punctuation except '.','-'"
    fi
}

function system_version_check() {
    if [ -f /etc/redhat-release ]; then
        # ARCH=$(uname -m)
        # KERNEL_MAIN_VERSION=$(uname -r | awk -F '.' '{print $1}')
        RELEASE=$(awk '{print $1}' /etc/redhat-release)
        SYSTEM_MAIN_VERSION=$(awk -F "[ |.]" '{print $4}' /etc/redhat-release)
        SYSTEM_RELEASE_VERSION=$(awk -F "[ |.]" '{print $5}' /etc/redhat-release)
        awk -F "[ |.]" '{if($1=="CentOS" && $4==7 && $5>=4) exit 10;else exit 11}' /etc/redhat-release
        if [ $? -eq 10 ]; then
            log_info "操作系统为 ${RELEASE} ${SYSTEM_MAIN_VERSION}.${SYSTEM_RELEASE_VERSION}，满足安装要求。"
        else
            log_error "系统版本要求RedHat系列发行版，7.4及以上版本"
            exit
        fi
    else
        log_error "操作系统不是Redhat系列，请使用Redhat系列操作系统。"
        exit
    fi
}

function system_version_check() {
    if [ -f /etc/redhat-release ]; then
        # ARCH=$(uname -m)
        # KERNEL_MAIN_VERSION=$(uname -r | awk -F '.' '{print $1}')
        RELEASE=$(awk '{print $1}' /etc/redhat-release)
        SYSTEM_MAIN_VERSION=$(awk -F "[ |.]" '{print $4}' /etc/redhat-release)
        SYSTEM_RELEASE_VERSION=$(awk -F "[ |.]" '{print $5}' /etc/redhat-release)
        awk -F "[ |.]" '{if(($1=="CentOS" && $4==7 && $5>=4) || ($1=="Rocky" && $4==8 && $5>=6)) exit 10;else exit 11}' /etc/redhat-release
        if [ $? -eq 10 ]; then
            log_info "操作系统为 ${RELEASE} ${SYSTEM_MAIN_VERSION}.${SYSTEM_RELEASE_VERSION}，满足安装要求。"
        else
            log_error "系统版本要求RedHat系列发行版，7.4及以上版本"
            exit
        fi
    else
        log_error "操作系统不是Redhat系列，请使用Redhat系列操作系统。"
        exit
    fi
}

# @logserver_resolver
# Backup current /etc/hosts -> /tmp/hosts.$(date +%Y%m%d%H%M%S).bak
# Config ip -> logserver hostname resolve.
function logserver_resolver() {
    rm -f /tmp/hosts.*.bak
    cp -p /etc/hosts /tmp/hosts.$(date +%Y%m%d%H%M%S).bak
    sed -i /logserver/d /etc/hosts
    echo -e "$LocalIP logserver" >> /etc/hosts
}

function path_mount_point_compare() {
    if [ "$(df -h $1 | awk 'NR!=1{print $NF}')" == "$(df -h $2 | awk 'NR!=1{print $NF}')" ]; then
        log_error "当前数据库路径和备份路径在同一个分区内,请将二者路径修改为在不同的分区"
        exit
    fi
}

function storage_check() {
    case ${STORAGE_MODE} in
    local | nas)
        mount_record=$(cat /proc/mounts | grep " ${DATA_PATH} ")
        if [ $(echo ${mount_record} | awk '{print($3 ~ /nfs|cifs/)?0:1}') -eq 0 ]; then
            log_info "current storage is share storage.now check /etc/fstab to add auto mount."
            if cat /etc/fstab | grep -Ev '^#|^$' | grep "${DATA_PATH}" &> /dev/null; then
                log_info "/etc/fstab auto mount check done."
            else
                log_error "Please edit /etc/fstab to add auto mount record."
                exit 1
            fi

            log_info "current storage is share storage.now check mount args."
            if [ $(echo ${mount_record} | awk '{print($4 ~ /noac/)?0:1}') -eq 0 ]; then
                log_info "mount args check done."
            else
                log_error "mount args check done, noac ags not configed. Please config it and remount."
                exit 1
            fi
        fi
    esac
}
