1#!/bin/bash
2
3#
4# Copyright (c) 2024, RT-Thread Development Team
5#
6# SPDX-License-Identifier: Apache-2.0
7#
8# Change Logs:
9# Date           Author       Notes
10# 2024-08-27     Supperthomas the first version
11# 2025-08-09     ThearchyHelios     Fix Darwin detection and URL
12#
13
14#这个脚本用于安装RT-Thread开发环境 请确保网络畅通
15
16# 设置环境变量 如果希望生效请在当前shell中执行source install.sh
17
18export RTT_ROOT=$(pwd)
19export RTT_CC=gcc
20
21echo "RTT_ROOT is set to: $RTT_ROOT"
22
23
24check_if_china_ip() {
25    # 默认情况下不使用gitee
26    use_gitee=false
27
28    # 尝试通过IP地址判断
29    ip=$(curl -s https://ifconfig.me/ip)
30    if [ -n "$ip" ]; then
31        location=$(curl -s http://www.ip-api.com/json/$ip | grep -o '"country":"China"')
32        if [ "$location" == '"country":"China"' ]; then
33            use_gitee=true
34            echo "Detected China IP. Using gitee."
35        else
36            echo "IP location is not in China."
37        fi
38    else
39        echo "Failed to retrieve IP address. Falling back to timezone check."
40
41        # 通过时区判断
42        if [ $(($(date +%z)/100)) -eq 8 ]; then
43            use_gitee=true
44            echo "Detected timezone UTC+8. Using gitee."
45        else
46            echo "Timezone is not UTC+8."
47        fi
48    fi
49
50    echo $use_gitee
51}
52
53
54# 检测操作系统类型和发行版
55detect_os() {
56    if command -v uname >/dev/null 2>&1; then
57        OS=$(uname -s)
58    else
59        if [ -f "/etc/os-release" ]; then
60            OS="Linux"
61        elif [ -f "/System/Library/CoreServices/SystemVersion.plist" ]; then
62            OS="Darwin"
63        elif [[ -d "/mnt/c/Windows" || -d "/c/Windows" ]]; then
64            OS="WSL"
65        else
66            OS="UNKNOWN"
67        fi
68    fi
69
70    if [ "$OS" == "Linux" ]; then
71        if [ -f /etc/os-release ]; then
72            . /etc/os-release
73            DISTRO=$ID
74            VERSION=$VERSION_ID
75        elif [ -f /etc/lsb-release ]; then
76            . /etc/lsb-release
77            DISTRO=$DISTRIB_ID
78            VERSION=$DISTRIB_RELEASE
79        else
80            DISTRO="UNKNOWN"
81            VERSION="UNKNOWN"
82        fi
83    fi
84
85    echo "Detected Operating System: $OS, Distribution: $DISTRO, Version: $VERSION"
86}
87
88# 修改的安装函数
89install_on_ubuntu() {
90    echo "Installing on Debian/Ubuntu..."
91    use_gitee=$(check_if_china_ip)
92
93    # 根据检测结果决定是否使用--gitee参数
94    if [ "$use_gitee" = true ]; then
95        wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_ubuntu.sh
96        chmod 777 install_ubuntu.sh
97        echo "Installing on China gitee..."
98        ./install_ubuntu.sh --gitee
99    else
100        wget https://raw.githubusercontent.com/RT-Thread/env/master/install_ubuntu.sh
101        chmod 777 install_ubuntu.sh
102        echo "Installing on no China..."
103        ./install_ubuntu.sh
104    fi
105    rm install_ubuntu.sh
106}
107
108
109install_on_fedora() {
110    echo "Installing on Fedora..."
111
112}
113
114install_on_centos() {
115    echo "Installing on CentOS/RHEL..."
116}
117
118install_on_arch() {
119    echo "Installing on Arch Linux..."
120}
121
122install_on_macos() {
123    echo "Installing on macOS..."
124    use_gitee=$(check_if_china_ip)
125
126    # 根据检测结果决定是否使用--gitee参数
127    if [ "$use_gitee" = true ]; then
128        wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_macos.sh
129        chmod 777 install_macos.sh
130        echo "Installing on China gitee..."
131        ./install_macos.sh --gitee
132    else
133        wget https://raw.githubusercontent.com/RT-Thread/env/master/install_macos.sh
134        chmod 777 install_macos.sh
135        echo "Installing on no China..."
136        ./install_macos.sh
137    fi
138    rm ./install_macos.sh
139}
140
141install_on_wsl() {
142    echo "Installing on Windows Subsystem for Linux (WSL)..."
143}
144
145install_on_windows() {
146    echo "Installing on Windows using PowerShell..."
147        use_gitee=$(check_if_china_ip)
148
149    # 根据检测结果决定是否使用--gitee参数
150    if [ "$use_gitee" = true ]; then
151        wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_windows.ps1
152        echo "Installing on China gitee..."
153        ./install_windows.ps1 --gitee
154    else
155        wget https://raw.githubusercontent.com/RT-Thread/env/master/install_windows.ps1
156        echo "Installing on no China..."
157        ./install_windows.ps1
158    fi
159    rm ./install_windows.ps1
160}
161
162install_on_opensuse() {
163    echo "Installing on openSUSE..."
164    use_gitee=$(check_if_china_ip)
165    if [ "$use_gitee" = true ]; then
166        wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_suse.sh
167        chmod 777 install_suse.sh
168        echo "Installing on China gitee..."
169        ./install_suse.sh --gitee
170    else
171        wget https://raw.githubusercontent.com/RT-Thread/env/master/install_suse.sh
172        chmod 777 install_suse.sh
173        echo "Installing on no China..."
174        ./install_suse.sh
175    fi
176    rm ./install_suse.sh
177}
178# 主函数
179main() {
180    detect_os
181    case "$OS" in
182        Linux)
183            case "$DISTRO" in
184                ubuntu|debian)
185                    install_on_ubuntu
186                    ;;
187                fedora)
188                    install_on_fedora
189                    ;;
190                centos|rhel)
191                    install_on_centos
192                    ;;
193                arch)
194                    install_on_arch
195                    ;;
196                *)
197                    echo "Unsupported Linux distribution: $DISTRO"
198                    exit 1
199                    ;;
200            esac
201            ;;
202        Darwin)
203            install_on_macos
204            ;;
205        WSL)
206            install_on_wsl
207            ;;
208        Windows)
209            install_on_windows
210            ;;
211        *)
212            echo "Unsupported Operating System: $OS"
213            exit 1
214            ;;
215    esac
216    echo "Installation completed!"
217}
218
219# 执行主函数
220main
221