#!/bin/bash
# 使用场景：不区分arm和amd,统一使用此脚本安装agent
# 注意事项：
#   1. 需要根据region情况更新RegionID、安装包地址
#   2. 需要将uniagent包、telescope包和脚本上传到对应的OBS桶中

RegionID="la-south-2"
TelescopeDirName="telescope"
TelescopeOBSUrl="https://uniagent-${RegionID}.obs.${RegionID}.myhuaweicloud.com/extension/telescope/telescope.tar.gz"
TelescopePkg="${TelescopeDirName}.tar.gz"
TelescopeBin="telescope"
HolderTelescope="/usr/local/uniagent/extension/holder/telescope"
TempDir="/usr/local/uniagent/temp"
UNIAGENT_PROC_NAME="uniagent"
TELESCOPE_PROC_NAME="telescope"

function downloadPkg() {
    local pkgUrl=$1
    curl -k -O ${pkgUrl}
}

function installUniAgent() {
    UniAgent="uniagent_linux_${Arch}"
    UniAgentPkg="${UniAgent}.tar.gz"
    UniAgentOBSUrl="https://uniagent-${RegionID}.obs.${RegionID}.myhuaweicloud.com/package/${UniAgent}.tar.gz"

    downloadPkg ${UniAgentOBSUrl}
    if [[ $? -ne 0 ]]; then echo "Download ${UniAgentOBSUrl} failed and exit(-1)"; exit -1; fi
    tar -zxvf ${UniAgentPkg}
    if [[ $? -ne 0 ]]; then echo "Tar ${UniAgentPkg} failed and exit(-1)"; exit -1; fi
    bash ${UniAgent}/script/install.sh
    if [[ $? -ne 0 ]]; then echo "Install ${UniAgent} failed and exit(-1)"; exit -1; fi
}

function installTelescope() {
    downloadPkg ${TelescopeOBSUrl}
    if [[ $? -ne 0 ]]; then echo "Download ${TelescopeOBSUrl} failed and exit(-1)"; exit -1; fi
    tar -zxvf ${TelescopePkg} -C ${TempDir}
    if [[ $? -ne 0 ]]; then echo "Tar ${TelescopePkg} failed and exit(-1)"; exit -1; fi
    bash ${TempDir}/${TelescopeDirName}/linux_${Arch}_bin/install.sh
    if [[ $? -ne 0 ]]; then echo "Execute install.sh(telescope) failed and exit(-1)"; exit -1; fi
    cp -f ${TempDir}/${TelescopeDirName}/manifest.json ${HolderTelescope}/manifest.json
    echo "{\"state\": \"started\"}" > ${HolderTelescope}/state.json
    chmod 600 ${HolderTelescope}/manifest.json ${HolderTelescope}/state.json
}

function mkdirForTelescope() {
    mkdir -p ${TempDir}
    mkdir -p ${HolderTelescope}
    chmod 700 ${HolderTelescope} $(dirname ${HolderTelescope})
}

function clean() {
    rm -rf ${UniAgentPkg} ${TelescopePkg} ${TempDir}
}

function isProcessRunning() {
    PROC_NAME=$1
    pgrep -l -f -P 1 ${PROC_NAME}

    if [[ $? == 0 ]]; then
       # process exists
       return 0
    else
       # process does not exist
       return 1
    fi
}

function checkStatus() {
    isProcessRunning ${UNIAGENT_PROC_NAME} > /dev/null
    status=$?
    if [[ ${status} == 0 ]]; then
        echo "Upgrading agent to the latest version, please wait..."
        bash /usr/local/uniagent/script/uninstall.sh > /dev/null
        if [[ $? -ne 0 ]]; then echo "${UNIAGENT_PROC_NAME} is running, reinstall ${UNIAGENT_PROC_NAME} failed and exit(-1)"; exit -1; fi
    fi

    isProcessRunning ${TELESCOPE_PROC_NAME}
    status=$?
    if [[ ${status} == 0 ]]; then
        echo "${TELESCOPE_PROC_NAME} is running, so can't be installed."
        echo "Please use \"sh /usr/local/telescope/uninstall.sh\" to uninstall ${TELESCOPE_PROC_NAME} and try to reinstall."
        exit 1
    fi
}

function getOsArchType(){
    if [[ $(arch) == "x86_64" ]]; then
        echo "amd64"
    elif [[ $(arch) == "aarch64" ]]; then
        echo "arm64"
    else
        echo "amd64"
    fi
}

function main() {
    checkStatus
    case "$1" in
        amd64)
            Arch="amd64"
        ;;
        arm64)
            Arch="arm64"
        ;;
        *)
            Arch=$(getOsArchType)
        ;;
    esac

    case "$2" in
        installUniOnly)
            installUniAgent
            clean
        ;;
        *)
            installUniAgent
            mkdirForTelescope
            installTelescope
            clean
        ;;
    esac
}

# start
main $1 $2
