Files
amber-pm/src/usr/bin/apm
2025-10-28 08:32:24 +00:00

329 lines
11 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
VERSION=1.0.10
# 获取脚本名称用于帮助信息
SCRIPT_NAME=$(basename "$0")
PATH_PREFIX=/var/lib/apm/apm/files/ace-env/
log.warn() { echo -e "[\e[33mWARN\e[0m]: \e[1m$*\e[0m"; }
log.error() { echo -e "[\e[31mERROR\e[0m]: \e[1m$*\e[0m"; }
log.info() { echo -e "[\e[96mINFO\e[0m]: \e[1m$*\e[0m"; }
log.debug() { echo -e "[\e[32mDEBUG\e[0m]: \e[1m$*\e[0m"; }
# 帮助信息函数
show_help() {
cat <<EOF
APM - Amber Package Manager ${VERSION}
Usage:
$SCRIPT_NAME [COMMAND] [OPTIONS] [PACKAGES...]
Commands:
install 安装软件包
remove 卸载软件包
update 更新软件包信息
list 查看可用软件包信息
search 搜索软件包
download 下载包
clean 清除缓存软件包
autoremove 自动移除不需要的包
full-upgrade 完全升级软件包
run <package> 运行指定软件包的可执行文件
ssaudit <path> 使用 ssaudit 进行本地软件安装,详情见 spark-store
debug 显示调试系统信息并进入调试环境
amber 彩蛋功能
xmp360 彩蛋功能
bronya 彩蛋功能
EOF
}
apm_exec(){
# 递归读取info文件并构建lowerdir
local lowerdirs=()
local current_dir="${PATH_PREFIX}/var/lib/apm/${coredir}" # 当前目录开始
local next_info_file=""
while : ; do
# 构建info文件的路径
next_info_file="${current_dir}/info"
# echo "${current_dir}/info"
# 检查info文件是否存在
if [[ ! -f "$next_info_file" ]]; then
# log.debug "No more info files found, stopping recursion."
break
fi
# 读取info文件的每一行并构建lowerdir
while IFS= read -r basedir; do
[[ -z "$basedir" ]] && continue # 跳过空行
# 检查ace-env目录是否存在
if [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env" ]]; then
lowerdirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env")
# 如果ace-env不存在检查core目录
elif [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/core" ]]; then
lowerdirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/core")
else
log.warn "Neither ace-env nor core directory found for base: $basedir"
fi
done < "$next_info_file"
# 尝试获取下一个依赖信息的路径
local next_basedir=$(tail -n 1 "$next_info_file")
if [[ -z "$next_basedir" || ! -d "${PATH_PREFIX}/var/lib/apm/${next_basedir}" ]]; then
log.debug "No further dependencies found, ending recursion."
break
fi
# 更新当前目录,递归处理下一个依赖
current_dir="${PATH_PREFIX}/var/lib/apm/${next_basedir}"
done
# 检查是否找到了有效的lowerdir
if [[ ${#lowerdirs[@]} -eq 0 ]]; then
log.error "No valid lower directories found for package: $coredir"
return 1
fi
# 将lowerdirs数组用冒号连接起来
local lowerdir=$(IFS=:; echo "${lowerdirs[*]}")
# 创建挂载点目录
mkdir -p "/tmp/apm/${coredir}"
# 使用fuse-overlayfs挂载
log.debug "Mounting with lowerdir: $lowerdir"
fuse-overlayfs -o lowerdir="$lowerdir",upperdir="${PATH_PREFIX}/var/lib/apm/${coredir}/files/core/",workdir="${PATH_PREFIX}/var/lib/apm/${coredir}/files/work/" "/tmp/apm/${coredir}"
# 执行命令
chrootEnvPath="/tmp/apm/${coredir}" /var/lib/apm/apm/files/ace-run "$@"
# 卸载
umount "/tmp/apm/${coredir}"
}
# 调试信息函数
debug_info() {
log.debug "======= APM Debug Information ======="
log.debug "User: $(whoami)"
log.debug "Hostname: $(hostname)"
log.debug "OS: $(lsb_release -ds 2>/dev/null || uname -om)"
log.debug "Kernel: $(uname -sr)"
log.debug "Bash Version: ${BASH_VERSION}"
log.debug "APT Version: $(apt --version | head -n1)"
log.debug "APM APT Version: $(amber-pm-debug apt --version | head -n1)"
log.debug "====================================="
amber-pm-debug "$@"
}
# 彩蛋函数
amber_egg() {
cat <<'EOF'
____ ____
/ __ )____ __________ ____ / __ )__ ______ ____ __ __
/ __ / __ `/ ___/ __ \/ __ \ / __ / / / / __ \/ __ \/ / / /
/ /_/ / /_/ / / / /_/ / / / / / /_/ / /_/ / / / / / / / /_/ /
/_____/\__,_/_/ \____/_/ /_/ /_____/\__,_/_/ /_/_/ /_/\__, /
/____/
Amber Package Manager - Sparkling with magic! 安柏包管理器 - blingbling
💎 Another target tracked down by Outrider Amber! 侦察骑士,发现目标!
EOF
}
bronya_egg() {
cat <<'EOF'
_ __ ____ _ ____ __
| | / /__ _/ / /____ ______(_)__ / __/_ _____ / /____ __ _
| |/ / _ `/ / '_/ // / __/ / -_) _\ \/ // (_-</ __/ -_) ' \
|___/\_,_/_/_/\_\\_, /_/ /_/\__/ /___/\_, /___/\__/\__/_/_/_/
/ / ___ ___ __/___/____/ / /___/
/ /__/ _ `/ // / _ \/ __/ _ \
/____/\_,_/\_,_/_//_/\__/_//_/
Valkyrie 系统启动 - 重装小兔Fire
💎 感谢 Anysets 为 AmberCE 和 AmberPM 的 Arch 架构支持提供帮助~
EOF
}
xmp360_egg() {
cat <<'EOF'
################################################################################
################################################################################
################################################################################
##################### ####################
####################. %###################
##########= ####################
############################ #=========*#########
############# .% .*****= :######
############################### =+ =#######* -#####
##### %: *######## :#####
######################% # ########* +#####
########: # %#####
########################## .# ######
##################* -* ######
##################= =###%: +- *###* ######
##################. # %: :* # -######
##################. % *###= % -- #### % %######
########################- ####+ ########################### .####. #########
######################### ############################% ##########
###########################=::+################################%-:-*############
################################################################################
哇——————袄 - 撞大运咯!
💎 感谢 潇湘·秀 为 APM 的 RPM 架构支持提供帮助~
EOF
}
apm-nvidia-toggle(){
# APM 基础路径
APM_BASE="${PATH_PREFIX}/var/lib/apm"
# 检查基础目录是否存在
if [[ ! -d "$APM_BASE" ]]; then
echo "错误: 目录 $APM_BASE 不存在"
exit 1
fi
# 遍历 /var/lib/apm 下的所有目录
for dir in "$APM_BASE"/*/; do
# 移除末尾的斜杠得到纯目录名
dir="${dir%/}"
# 提取目录名(不包括完整路径)
dirname=$(basename "$dir")
# 检查目标文件是否存在
target_file="${APM_BASE}/${dirname}/files/ace-env"
if [[ -e "$target_file" ]]; then
# 将目录传递给 amber-pm-configure-nvidia
amber-pm-configure-nvidia "$target_file"
fi
done
}
# 主命令处理
case "$1" in
install|full-upgrade|upgrade|reinstall)
command=$1
shift
amber-pm-debug aptss "$command" "$@"
exit_code=$?
if [ $exit_code -eq 0 ]; then
log.info "Operation successful"
else
log.error "Error: Operation failed"
exit $exit_code
fi
amber-pm-debug amber-pm-dstore-patch
apm-nvidia-toggle
amber-pm-gxde-desktop-fix
;;
download|search|policy|list|update|clean)
command=$1
shift
amber-pm-debug aptss "$command" "$@"
exit_code=$?
if [ $exit_code -eq 0 ]; then
log.info "Operation successful"
else
log.error "Error: Operation failed"
exit $exit_code
fi
;;
remove|autoremove|purge|autopurge)
# 特殊APT命令移除第一个参数后传递其余参数
command=$1
shift
amber-pm-debug aptss "$command" "$@"
exit_code=$?
if [ $exit_code -eq 0 ]; then
log.info "Operation successful"
else
log.error "Error: Operation failed"
exit $exit_code
fi
amber-pm-debug amber-pm-dstore-patch
amber-pm-gxde-desktop-fix
;;
run)
# 运行包命令:第二个参数必须是包名
if [ -z "$2" ]; then
log.error "Package name required for 'run' command"
show_help
exit 1
fi
# 检查包是否已安装
pkg="$2"
shift 2 # 移除 'run' 和包名
if ! ls "${PATH_PREFIX}/var/lib/apm/$pkg" >/dev/null 2>&1; then
# 如果带前缀的目录不存在,尝试不带前缀的目录
if ls "/var/lib/apm/$pkg" >/dev/null 2>&1; then
# 如果不带前缀的目录存在,清空 PATH_PREFIX
PATH_PREFIX=""
else
# 如果两个目录都不存在,报错退出
log.error "Package not installed: $pkg"
exit 1
fi
fi
coredir=$pkg
# 检测是否有额外命令参数
if [ $# -gt 0 ]; then
# 有额外参数:执行用户提供的命令
log.info "Running user command: $*"
apm_exec "$@"
else
# 没有额外参数:提示
log.info "Usage: $SCRIPT_NAME run $pkg [EXEC_PATH]"
exit 1
fi
;;
debug)
debug_info
;;
ssaudit)
amber-pm-debug ssaudit "$@" --native
exit_code=$?
if [ $exit_code -eq 0 ]; then
log.info "Operation successful"
else
log.error "Error: Operation failed"
exit $exit_code
fi
amber-pm-debug amber-pm-dstore-patch
amber-pm-gxde-desktop-fix
;;
-h|--help)
show_help
;;
amber)
amber_egg
;;
xmp360)
xmp360_egg
;;
bronya)
bronya_egg
;;
*)
show_help
;;
esac