#!/bin/bash
# supported os：suse12.2 for sap，suse12.3 for sap
# supported region: cn-east，cn-north, cn-south

meta_data_url=http://169.254.169.254/openstack/latest/meta_data.json
CURL="curl -s --connect-timeout 10"

function get_base_url()
{
    local py_path
    type python > /dev/null 2>&1
    if [ $? -eq 0 ];then
        py_path=$(which python)
    else
        py_path=$(which python3)
    fi

    AZ=$($CURL $meta_data_url | ${py_path} -m json.tool | grep availability_zone | awk -F '"' '{print $4}')
    echo "Current AZ: $AZ"
    region=${AZ:0:-1}
    if [[ ${AZ} == cn-east-2* ]]; then
        sap_bucket="obs-sap"
        SAPMON_BASE_URL=http://${sap_bucket}.obs.myhuaweicloud.com/sapmon
    elif [[ ${AZ} == sa-brazil-1* ]]; then
        sap_bucket="obs-sap-sa-brazil-11"
        SAPMON_BASE_URL=http://${sap_bucket}.obs.myhuaweicloud.com/sapmon
    elif [[ ${AZ} == cn-south* ]]; then
        sap_bucket="obs-sap-cn-south-1"
        SAPMON_BASE_URL=http://${sap_bucket}.obs.myhuaweicloud.com/sapmon
    elif [[ ${AZ} == eu-* ]]; then
        sap_bucket=obs-sap-${region}
        SAPMON_BASE_URL=http://${sap_bucket}.obs.${region}.myhuaweicloud.eu/sapmon
    else
        sap_bucket=obs-sap-${region}
        SAPMON_BASE_URL=http://${sap_bucket}.obs.myhuaweicloud.com/sapmon
    fi
}

function check_param()
{
    usage_info="
DESCRIPTION
    Install the SAP monitor plugin.

    --nwrfcsdk-path <\033[4mnwrfcsdk-path\033[0m>
        This flag must be set if monitor for SAP Netweaver.
    --version <\033[4mversion\033[0m>
        Specify the version of SAP monitor to download.
    --release <\033[4mrelease number\033[0m>
        Specify the release number of SAP monitor to download.
EXAMPLES
    ./install.sh
    Install SAP monitor plugin with the latest version.

    ./install.sh --nwrfcsdk-path /usr/sap/nwrfcsdk/
    Install SAP monitor plugin with the latest version for Netweaver.

    ./install.sh --version 1.0.2 --release 1
    Install the specified version and release number of the SAP monitor plugin.

    ./install.sh --version 1.0.2 --release 1 --nwrfcsdk-path /usr/sap/nwrfcsdk/
    Install the specified version and release number of the SAP monitor plugin for Netweaver.
    "
    while [ "$1" ]; do
        case $1 in
            --version)
                shift
                VERSION=$1
                ;;
            --release)
                shift
                RELEASE=$1
                ;;
            --nwrfcsdk-path)
                shift
                NWRFCSDK_PATH=$1
                ;;
            *)
                err=true
                break
                ;;
        esac
        shift
    done
    if [ $err ]; then
        echo "Unsupported option '$1'."
        echo -e "$usage_info" 1>&2
        exit 1
    fi

    if [ -n "$NWRFCSDK_PATH" ] && [ ! -d "$NWRFCSDK_PATH" ]; then
        echo "NWRFCSDK_PATH: $NWRFCSDK_PATH does not exist."
        exit 2
    fi
}

function verlte() {
    [ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
}

function get_version()
{
    get_base_url
    if [ -z "$VERSION" ] || [ -z "$RELEASE" ]; then
        latest_info=$($CURL "${SAPMON_BASE_URL}/latest" || exit 1)
        if [ -z "$VERSION" ]; then
            VERSION=$(echo "$latest_info" | awk -F "=" '/^version/ {print $2}' | sed 's/ //g')
        fi

        if [ -z "$RELEASE" ]; then
            RELEASE=$(echo "$latest_info" | awk -F "=" '/^release/ {print $2}' | sed 's/ //g')
        fi
    fi
    if [ -z "$VERSION" ] || [ -z "$RELEASE" ]; then
        echo "[Err] Get package version ${SAPMON_BASE_URL}/latest failed. $latest_info"
        exit 1
    fi
    echo "The latest version: $VERSION"
    echo "The latest release: $RELEASE"
}

function download_and_install()
{
    tmp_dir="/tmp/sapmon-install/"
    if [ -d $tmp_dir ]; then
        rm -rf $tmp_dir
    fi
    mkdir $tmp_dir
    sapmon_package_filename=sapmon-$VERSION-$RELEASE.tar.gz
    sapmon_rpm_package_prefix=sapmon-$VERSION-$RELEASE.x86_64
    sapmon_rpm_filename=$sapmon_rpm_package_prefix.rpm
    seccomponent_rpm_filename=seccomponent-1.0.2-2.0.release.x86_64.rpm
    echo "Start downloading package $SAPMON_BASE_URL/$sapmon_package_filename."
    cd $tmp_dir && wget "$SAPMON_BASE_URL/$sapmon_package_filename" -O "$sapmon_package_filename"
    tar zxvf $sapmon_package_filename
    if rpm -Uvh "$seccomponent_rpm_filename"; then
        echo "Install seccomponent successful."
    else
        echo "Install seccomponent with error."
    fi
    if rpm -ivh "$sapmon_rpm_filename"; then
        echo "Install sapmon successful."
    else
        echo "Install sapmon with error."
    fi
    if [ -d $tmp_dir ]; then
        rm -rf $tmp_dir
    fi
}

function set_nwrfcsdk()
{
    if [ -z "$NWRFCSDK_PATH" ]; then
        return
    fi

    echo "Set NWRFCSDK_PATH: $NWRFCSDK_PATH"
    export NWRFCSDK_PATH=$NWRFCSDK_PATH
}

check_param "$@"
set_nwrfcsdk
get_version
download_and_install
