<commit_before>#!/usr/bin/env bash

# xmake getter
# usage: bash <(curl -s <my location>) [branch|__local__|__run__] [commit/__install_only__]

set -o pipefail

# has sudo?
if [ 0 -ne "$(id -u)" ]; then
    if sudo --version >/dev/null 2>&1
    then
        sudoprefix=sudo
    else
        sudoprefix=
    fi
else
    export XMAKE_ROOT=y
    sudoprefix=
fi

# make tmpdir
if [ -z "$TMPDIR" ]; then
    tmpdir=/tmp/.xmake_getter$$
else
    tmpdir=$TMPDIR/.xmake_getter$$
fi
if [ -d $tmpdir ]; then
    rm -rf $tmpdir
fi

remote_get_content() {
    if curl --version >/dev/null 2>&1
    then
        curl -fSL "$1"
    elif wget --version >/dev/null 2>&1
    then
        wget "$1" -O -
    fi
}

get_host_speed() {
    if [ `uname` == "Darwin" ]; then
        ping -c 1 -t 1 $1 2>/dev/null | egrep -o 'time=\d+' | egrep -o "\d+" || echo "65535"
    else
        ping -c 1 -W 1 $1 2>/dev/null | egrep -o 'time=\d+' | egrep -o "\d+" || echo "65535"
    fi
}

get_fast_host() {
    speed_gitee=$(get_host_speed "gitee.com")
    speed_gitlab=$(get_host_speed "gitlab.com")
    speed_github=$(get_host_speed "github.com")
    if [ $speed_gitee -le $speed_gitlab ]; then
        if [ $speed_gitee -le $speed_github ]; then
            echo "gitee.com" 
        else
            echo "github.com"
        fi
    else
        if [ $speed_gitlab -le $speed_github ]; then
            echo "gitlab.com"
        else
            echo "github.com"
        fi
    fi
}

# get branch
branch=__run__
if [ x != "x$1" ]; then
    brancharr=($1)
    if [ ${#brancharr[@]} -eq 1 ]
    then
        branch=${brancharr[0]}
    fi
    echo "Branch: $branch"
fi

# get fasthost and git repository
if [ 'x__local__' != "x$branch" ]; then
    fasthost=$(get_fast_host)
    if [ "$fasthost" == "gitee.com" ]; then
        gitrepo="https://gitee.com/tboox/xmake.git"
        gitrepo_raw="https://gitee.com/tboox/xmake/raw/master"
    elif [ "$fasthost" == "gitlab.com" ]; then
        gitrepo="https://gitlab.com/tboox/xmake.git"
        gitrepo_raw="https://gitlab.com/tboox/xmake/-/raw/master"
    else
        gitrepo="https://github.com/xmake-io/xmake.git"
        #gitrepo_raw="https://github.com/xmake-io/xmake/raw/master"
        gitrepo_raw="https://cdn.jsdelivr.net/gh/xmake-io/xmake"
    fi
fi

if [ "$1" = "__uninstall__" ]
then
    # uninstall
    makefile=$(remote_get_content https://github.com/xmake-io/xmake/raw/master/makefile)
    while which xmake >/dev/null 2>&1
    do<commit_msg>improve get.sh<commit_after>#!/usr/bin/env bash

# xmake getter
# usage: bash <(curl -s <my location>) [branch|__local__|__run__] [commit/__install_only__]

set -o pipefail

# has sudo?
if [ 0 -ne "$(id -u)" ]; then
    if sudo --version >/dev/null 2>&1
    then
        sudoprefix=sudo
    else
        sudoprefix=
    fi
else
    export XMAKE_ROOT=y
    sudoprefix=
fi

# make tmpdir
if [ -z "$TMPDIR" ]; then
    tmpdir=/tmp/.xmake_getter$$
else
    tmpdir=$TMPDIR/.xmake_getter$$
fi
if [ -d $tmpdir ]; then
    rm -rf $tmpdir
fi

remote_get_content() {
    if curl --version >/dev/null 2>&1
    then
        curl -fSL "$1"
    elif wget --version >/dev/null 2>&1
    then
        wget "$1" -O -
    fi
}

get_host_speed() {
    if [ `uname` == "Darwin" ]; then
        ping -c 1 -t 1 $1 2>/dev/null | egrep -o 'time=\d+' | egrep -o "\d+" || echo "65535"
    else
        ping -c 1 -W 1 $1 2>/dev/null | egrep -o 'time=\d+' | egrep -o "\d+" || echo "65535"
    fi
}

get_fast_host() {
    speed_gitee=$(get_host_speed "gitee.com")
    speed_gitlab=$(get_host_speed "gitlab.com")
    speed_github=$(get_host_speed "github.com")
    if [ $speed_gitee -le $speed_gitlab ]; then
        if [ $speed_gitee -le $speed_github ]; then
            echo "gitee.com" 
        else
            echo "github.com"
        fi
    else
        if [ $speed_gitlab -le $speed_github ]; then
            echo "gitlab.com"
        else
            echo "github.com"
        fi
    fi
}

# get branch
branch=__run__
if [ x != "x$1" ]; then
    brancharr=($1)
    if [ ${#brancharr[@]} -eq 1 ]
    then
        branch=${brancharr[0]}
    fi
    echo "Branch: $branch"
fi

# get fasthost and git repository
if [ 'x__local__' != "x$branch" ]; then
    fasthost=$(get_fast_host)
    if [ "$fasthost" == "gitee.com" ]; then
        gitrepo="https://gitee.com/tboox/xmake.git"
        gitrepo_raw="https://gitee.com/tboox/xmake/raw/master"
    elif [ "$fasthost" == "gitlab.com" ]; then
        gitrepo="https://gitlab.com/tboox/xmake.git"
        gitrepo_raw="https://gitlab.com/tboox/xmake/-/raw/master"
    else
        gitrepo="https://github.com/xmake-io/xmake.git"
        #gitrepo_raw="https://github.com/xmake-io/xmake/raw/master"
        gitrepo_raw="https://cdn.jsdelivr.net/gh/xmake-io/xmake"
    fi
fi

if [ "$1" = "__uninstall__" ]
then
    # uninstall
    makefile=$(remote_get_content $gitrepo_raw/makefile)
    while which xmake >/dev/null 2>&1
    do
{'fraction_non_alphanumeric': 0.15924344112263575, 'fraction_numerical': 0.013829570876550743, 'mean_word_length': 2.2940388479571334, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 15, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '602103', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1925, 'n_words': 418}