export FISHROS_URL=http://134.175.71.90/install/

SUDO=''
if [ "$(id -u)" -ne 0 ];then
    SUDO='sudo'
fi

# 上次运行被中断时 /tmp/fishinstall 会残留且属主是 root, 普通用户再下载会因权限不足静默失败,
# 结果要么没有菜单直接退出, 要么跑的是旧脚本, 所以每次先清掉
$SUDO rm -rf /tmp/fishinstall/
mkdir -p /tmp/fishinstall/tools/translation/assets
# 与 install.py 里其它下载保持一致不校验证书: 老系统证书库过旧、树莓派等开机时钟不准时, 服务器跳转到 https 也能下载
wget -q -T 15 -t 3 --no-check-certificate $FISHROS_URL/install.py -O /tmp/fishinstall/install.py
source /etc/profile

if [ -s /tmp/fishinstall/install.py ]; then
    $SUDO apt install python3-distro python3-yaml -y
    # sudo 默认会清掉环境变量, 显式把 FISHROS_URL 传进去, 否则后续工具一律从默认地址下载(自建服务器测试时会拿到旧版)
    $SUDO env FISHROS_URL="$FISHROS_URL" python3 /tmp/fishinstall/install.py
else
    echo "下载一键安装脚本失败: ${FISHROS_URL}install.py"
    echo "请检查网络后重试(能否打开 ${FISHROS_URL}install.py)"
fi
$SUDO rm -rf /tmp/fishinstall/

if [ -f fishros ]; then
    $SUDO rm fishros
fi

# 初始假设默认是 Bash
shell_name='bash'
if shopt -u lastpipe 2> /dev/null; then
    # 当前 shell 是 Bash
    :
else
    # 当前 shell 是 Zsh 或其他 shell
    if test -n "$ZSH_VERSION"; then
        shell_name='zsh'
    else
        # 当前使用的 shell 不是 Bash 或 Zsh
        shell_name=''
    fi
fi

# 根据 shell 名称加载相应的配置文件
if [ "$shell_name" = "bash" ]; then
    if [ -f ~/.bashrc ]; then
        source ~/.bashrc
    fi
elif [ "$shell_name" = "zsh" ]; then
    if [ -f ~/.zshrc ]; then
        source ~/.zshrc
    fi
fi
