2024-10-25 14:03:16 +08:00
#!/usr/bin/env bash
2024-10-25 13:47:51 +08:00
# ---------------------------------------------------------------
2024-10-27 20:56:34 +08:00
# File Name : installer.sh
2024-10-26 14:08:49 +08:00
# File Authors : GnixAij <gaojiaxing0220@gmail.com>
# | xuan <wick.dynex@qq.com>
# | ChatGPT <https://chatgpt.com>
2024-10-25 13:47:51 +08:00
# Contributors : Aoran Zeng <ccmywish@qq.com>
# |
# Created On : <2024-10-25>
2024-10-26 10:23:52 +08:00
# Last Modified : <2024-10-26>
2024-10-25 13:47:51 +08:00
#
2024-10-25 17:26:29 +08:00
# chsrc installer for Linux & macOS
2024-10-25 13:47:51 +08:00
# ---------------------------------------------------------------
binary_name = "chsrc"
2024-10-26 14:08:49 +08:00
# 用户指定的安装目录, 函数set_install_dir()将填充/校验该变量
userOpt_install_dir = ""
# 默认安装目录
default_install_dir_4root = "/usr/local/bin"
default_install_dir_4nonroot = " $HOME /.local/bin "
# 若用户指定的目录不存在则会创建该目录
tmp_created_install_dir = ""
userOpt_version = "pre"
userOpt_help = 0
userOpt_lang = "zh"
2024-10-25 13:47:51 +08:00
info( ) {
echo " [INFO] $* "
}
2024-10-26 14:08:49 +08:00
# 出现错误,直接强制退出
2024-10-25 13:47:51 +08:00
error( ) {
echo -e " [ERROR] $* " >& 2
exit 1
}
help( ) {
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
echo "chsrc-installer: 在类Unix操作系统上安装 chsrc"
2024-10-25 20:21:39 +08:00
echo
echo "使用: install.sh [选项]"
echo "选项:"
2024-10-26 14:08:49 +08:00
echo "-h 打印此帮助信息"
echo " -d <dir> 指定安装目录,默认为 $default_install_dir_4root 或 $default_install_dir_4nonroot "
echo "-v <version> 指定 chsrc 版本"
echo "-l <lang> 指定输出语言,支持 zh 和 en "
2024-10-25 20:21:39 +08:00
echo
else
echo "chsrc-installer: Install chsrc on any Unix-like OS"
echo
echo "Usage: install.sh [options]"
echo "Options:"
2024-10-26 14:08:49 +08:00
echo "-h Print this help information"
echo " -d <dir> Specify installation directory, default is $default_install_dir_4root OR $default_install_dir_4nonroot "
echo "-v <version> Specify chsrc version"
echo "-l <lang> Specify output language, supports zh and en"
2024-10-25 20:21:39 +08:00
echo
fi
2024-10-25 13:47:51 +08:00
}
2024-10-25 22:54:59 +08:00
2024-10-26 14:08:49 +08:00
#
# 1. 若用户指定了安装目录,则安装至那里
#
# 若安装目录不存在,该脚本会为用户创建
#
# 2. 若用户没有指定安装目录:
#
# 2.1 若 chsrc 已存在,自动安装到该位置
# 2.2 若 chsrc 不存在,安装到两个预定义的默认安装位置
#
set_install_dir( ) {
if [ -n " $userOpt_install_dir " ] ; then
2024-10-25 13:47:51 +08:00
# 扩展 ~ 符号
2024-10-26 14:08:49 +08:00
userOpt_install_dir = " ${ userOpt_install_dir /# \~ / $HOME } "
2024-10-25 15:28:49 +08:00
2024-10-26 14:08:49 +08:00
# 检查目录是否存在,如果不存在则创建该目录
if [ ! -d " $userOpt_install_dir " ] ; then
2024-10-25 20:21:39 +08:00
# 多种语言输出
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
echo " 目录 $userOpt_install_dir 不存在,正在创建... "
2024-10-25 20:21:39 +08:00
else
2024-10-26 14:08:49 +08:00
echo " Directory $userOpt_install_dir does not exist. Creating... "
2024-10-25 20:21:39 +08:00
fi
# 多语言输出
2024-10-26 14:08:49 +08:00
if ! mkdir -p " $userOpt_install_dir " ; then
if [ " $userOpt_lang " = "zh" ] ; then
2024-10-25 20:21:39 +08:00
echo "创建目录失败,请重试"
else
echo "Failed to create directory, please try again"
fi
exit 1
fi
2024-10-26 14:08:49 +08:00
tmp_created_install_dir = " $userOpt_install_dir " # 记录临时安装目录
2024-10-25 15:28:49 +08:00
fi
2024-10-25 20:21:39 +08:00
2024-10-25 13:47:51 +08:00
elif existing_path = $( command -v " $binary_name " 2>/dev/null) ; then
2024-10-25 20:21:39 +08:00
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
2024-10-25 20:21:39 +08:00
info " $binary_name 已安装,更新路径: ${ existing_path } "
else
info " $binary_name is already installed, updating path: ${ existing_path } "
fi
2024-10-26 14:08:49 +08:00
userOpt_install_dir = $( dirname " $existing_path " )
2024-10-25 13:47:51 +08:00
else
2024-10-26 14:08:49 +08:00
# 检查默认安装目录
if [ -d " $default_install_dir_4root " ] && [ -w " $default_install_dir_4root " ] ; then
userOpt_install_dir = " $default_install_dir_4root "
elif [ -d " $default_install_dir_4nonroot " ] && [ -w " $default_install_dir_4nonroot " ] ; then
userOpt_install_dir = " $default_install_dir_4nonroot "
2024-10-25 13:47:51 +08:00
else
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
error " 默认安装目录 $default_install_dir_4root 和 $default_install_dir_4nonroot 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它目录安装 "
2024-10-25 20:21:39 +08:00
else
2024-10-26 14:08:49 +08:00
error " Default install dir $default_install_dir_4root and $default_install_dir_4nonroot is not writable. Please run the script with sudo; or specify another dir using the -d option. "
2024-10-25 20:21:39 +08:00
fi
2024-10-25 13:47:51 +08:00
fi
fi
}
2024-10-25 22:54:59 +08:00
2024-10-25 13:47:51 +08:00
install( ) {
arch = " $( uname -m | tr '[:upper:]' '[:lower:]' ) "
case " $arch " in
x86_64) arch = "x64" ; ;
2024-10-25 15:29:40 +08:00
aarch64| arm64) arch = "aarch64" ; ;
2024-10-25 13:47:51 +08:00
riscv64) arch = "riscv64" ; ;
armv7*) arch = "armv7" ; ;
2024-10-25 22:54:59 +08:00
*)
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
2024-10-25 20:21:39 +08:00
error " 不支持的架构: ${ arch } "
else
error " Unsupported architecture: ${ arch } "
fi
; ;
2024-10-25 13:47:51 +08:00
esac
platform = " $( uname -s | awk '{print tolower($0)}' ) "
case " $platform " in
linux) platform = "linux" ; ;
darwin) platform = "macos" ; ;
2024-10-25 22:54:59 +08:00
*)
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
2024-10-25 22:54:59 +08:00
error " 不支持的平台: ${ platform } "
2024-10-25 20:21:39 +08:00
else
error " Unsupported platform: ${ platform } "
fi
; ;
2024-10-25 13:47:51 +08:00
esac
2024-10-26 14:08:49 +08:00
if [ [ ! " $userOpt_version " = ~ ^( pre| 0\. 1\. ( [ 4-9] ) ) $ ] ] ; then
2024-10-25 15:28:49 +08:00
# version 不符合条件,报错
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
2024-10-26 15:05:30 +08:00
error " 不支持的版本: ${ userOpt_version } ,版本号必须在 0.1.4 到 0.1.9 之间或为 'pre' "
2024-10-25 20:21:39 +08:00
else
2024-10-26 15:05:30 +08:00
error " Unsupported version: ${ userOpt_version } . Version number must be between 0.1.4 and 0.1.9 or 'pre' "
2024-10-25 20:21:39 +08:00
fi
2024-10-25 15:28:49 +08:00
fi
2024-10-26 15:05:30 +08:00
url = " https://gitee.com/RubyMetric/chsrc/releases/download/ ${ userOpt_version } / ${ binary_name } - ${ arch } - ${ platform } "
2024-10-25 13:47:51 +08:00
2024-10-26 14:08:49 +08:00
path_to_executable = " ${ userOpt_install_dir } / ${ binary_name } "
2024-10-25 22:54:59 +08:00
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
2024-10-26 15:05:30 +08:00
info " 下载 ${ binary_name } ( ${ arch } 架构, ${ platform } 平台, ${ userOpt_version } 版本) 到 ${ path_to_executable } "
2024-10-25 20:21:39 +08:00
else
2024-10-26 15:05:30 +08:00
info " Downloading ${ binary_name } ( ${ arch } architecture, ${ platform } platform, version ${ userOpt_version } ) to ${ path_to_executable } "
2024-10-25 20:21:39 +08:00
fi
2024-10-25 13:47:51 +08:00
if curl -sL " $url " -o " $path_to_executable " ; then
chmod +x " $path_to_executable "
2024-10-25 20:21:39 +08:00
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
info " 🎉 安装完成,版本: $userOpt_version ,路径: $path_to_executable "
2024-10-25 20:21:39 +08:00
else
info " 🎉 Installation completed, path: $path_to_executable "
fi
2024-10-25 13:47:51 +08:00
else
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
2024-10-25 20:21:39 +08:00
error " 下载失败,请检查您的网络连接和代理设置: ${ url } "
else
error " Download failed, please check your network connection and proxy settings: ${ url } "
fi
2024-10-25 13:47:51 +08:00
fi
}
2024-10-25 15:34:00 +08:00
cleanup( ) {
2024-10-26 14:08:49 +08:00
if [ -n " $tmp_created_install_dir " ] && [ -d " $tmp_created_install_dir " ] ; then
2024-10-25 20:21:39 +08:00
2024-10-26 14:08:49 +08:00
if [ " $userOpt_lang " = "zh" ] ; then
echo " 清理创建的目录: $tmp_created_install_dir "
2024-10-25 20:21:39 +08:00
else
2024-10-26 14:08:49 +08:00
echo " Cleaning up created directory: $tmp_created_install_dir "
2024-10-25 20:21:39 +08:00
fi
2024-10-26 14:08:49 +08:00
rm -rf " $tmp_created_install_dir "
2024-10-25 15:34:00 +08:00
fi
}
trap cleanup EXIT
2024-10-25 13:47:51 +08:00
2024-10-25 22:54:59 +08:00
2024-10-26 14:08:49 +08:00
# main
2024-10-25 20:21:39 +08:00
while getopts ":hd:v:l:" option; do
2024-10-25 13:47:51 +08:00
case $option in
h)
2024-10-26 14:08:49 +08:00
userOpt_help = 1
2024-10-25 13:47:51 +08:00
; ;
d)
2024-10-26 14:08:49 +08:00
userOpt_install_dir = ${ OPTARG }
2024-10-25 15:28:49 +08:00
; ;
v)
2024-10-26 14:08:49 +08:00
userOpt_version = ${ OPTARG }
2024-10-25 13:47:51 +08:00
; ;
2024-10-25 20:21:39 +08:00
l)
2024-10-26 14:08:49 +08:00
userOpt_lang = ${ OPTARG }
2024-10-25 20:21:39 +08:00
; ;
2024-10-25 13:47:51 +08:00
\? )
2024-10-25 17:26:29 +08:00
echo "无效的命令行选项,请使用 -h 查看帮助"
2024-10-25 13:47:51 +08:00
exit 1
; ;
esac
done
2024-10-26 14:08:49 +08:00
if [ [ " $userOpt_lang " != "zh" && " $userOpt_lang " != "en" ] ] ; then
error " 无效的语言选项: $userOpt_lang ,支持的选项为 zh 和 en "
2024-10-25 20:21:39 +08:00
fi
2024-10-26 14:08:49 +08:00
if [ " $userOpt_help " -eq 1 ] ; then
2024-10-25 20:21:39 +08:00
help
exit 0;
fi
2024-10-26 14:08:49 +08:00
set_install_dir
2024-10-25 22:54:59 +08:00
install