#!/bin/bash

RegionID=""
OBSDomain="myhuaweicloud.com"
Port="22"
KunpengMark=""
UniagentVersion="0.1.5"
TelescopeVersion="2.5.6"
KunpengRegionList=("cn-central-202" "cn-east-204" "cn-northwest-204" "cn-north-209" "cn-southwest-204")


function downloadPkg() {
    which curl
    if [[ $? -eq 0 ]]; then
          curl -k -O $1
          return 0
    fi

    which wget
    if [[ $? -eq 0 ]]; then
          wget $1
          return 0
    fi
    return 1
}

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

function batchInstallAgent() {
    BatchInstaller="uniagent_patch_installer_${Arch}"
    BatchInstallerOBSUrl="https://uniagent-${RegionID}.obs.${RegionID}${KunpengMark}.${OBSDomain}/package/${BatchInstaller}"

    downloadPkg ${BatchInstallerOBSUrl}
    if [[ $? -ne 0 ]]; then echo "Download ${BatchInstallerOBSUrl} failed and exit(-1)"; exit -1; fi

    chmod +x ${BatchInstaller}
    InstallParam="cd /usr/local && curl -k -O https://uniagent-${RegionID}.obs.${RegionID}${KunpengMark}.${OBSDomain}/package/agent_install.sh && bash agent_install.sh -r ${RegionID} "
    if [[ -n "${UniagentVersion}" ]]; then
      InstallParam="${InstallParam} -u ${UniagentVersion}"
    fi
    if [[ -n "${TelescopeVersion}" ]]; then
      InstallParam="${InstallParam} -t ${TelescopeVersion}"
    fi
    if [[ -n "${AgentApiDomain}" ]]; then
      InstallParam="${InstallParam} -d ${AgentApiDomain}"
    fi
    if [[ -n "${NicIP}" ]]; then
      InstallParam="${InstallParam} -n ${NicIP}"
    fi
    if [[ -n "${OBSDomain}" ]]; then
      InstallParam="${InstallParam} -o ${OBSDomain}"
    fi

    Port="22"
    if [[ -n "${Port}" ]]; then
      Port=${Port}
    fi
    Pem="false"
    if [[ -f "./user.pem" ]]; then
      Pem="true"
    fi
    ./${BatchInstaller} -c "${InstallParam}" -p ${Port} -pem=${Pem}
    if [[ $? -ne 0 ]]; then echo "Batch install agent failed and exit(-1)"; exit -1; fi
}

function help() {
    echo "Usage:"
    echo "batch_agent_install.sh [-r RegionID] [-u UniagentVersion] [-t TelescopeVersion] [-d AgentApiDomain] [-o OBSDomain] [-p Port]"
    exit -1
}

function prepareRegionIDAndOBSDomain() {
    meta_data=$(curl -s -m 2 http://169.254.169.254/openstack/latest/meta_data.json)
    region_id=$(echo "${meta_data}" | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/region_id\042/){print $(i+1)}}}' | tr -d ' "')
    if [[ -z "${RegionID}" ]]; then
        RegionID=$region_id
    fi
    if [[ -z "${RegionID}" ]]; then
        echo "need param RegionID."
        exit 1
    fi

    if [[ ${RegionID} == "cn-north-7" ]]; then
        OBSDomain="ulanqab.huawei.com"
    fi

    for kunpenRegionID in ${KunpengRegionList[@]}
    do
        if [[ ${kunpenRegionID} == ${RegionID} ]]; then
            KunpengMark="-dev"
        fi
    done
}

function main() {
    Arch=$(getOsArchType)
    prepareRegionIDAndOBSDomain

    batchInstallAgent
}

# start
while getopts ':r:u:t:d:o:p:n:h' OPT; do
    case $OPT in
        r)
            RegionID="$OPTARG";;
        u)
            UniagentVersion="$OPTARG";;
        t)
            TelescopeVersion="$OPTARG";;
        d)
            AgentApiDomain="$OPTARG";;
        o)
            OBSDomain="$OPTARG";;
        p)
            Port="$OPTARG";;
        n)
            NicIP="$OPTARG";;
        h)
            help;;
    esac
done
main
