#!/bin/bash # 检测是否安装了 KDE Plasma 桌面环境 # 通过检查系统中是否存在相关的 desktop 文件或关键程序 is_kde_plasma() { # 检查 KDE Plasma if [ -f /usr/share/xsessions/plasma.desktop ] || \ [ -f /usr/share/xsessions/plasma-xorg.desktop ] || \ [ -f /usr/share/xsessions/plasma-wayland.desktop ] || \ [ -f /usr/local/share/xsessions/plasma.desktop ] || \ [ -f /usr/local/share/xsessions/plasma-xorg.desktop ] || \ [ -f /usr/local/share/xsessions/plasma-wayland.desktop ] || \ command -v startplasma-x11 >/dev/null 2>&1 || \ command -v startplasma-wayland >/dev/null 2>&1 || \ command -v plasmashell >/dev/null 2>&1; then return 0 fi return 1 } # 确定目标目录 if grep -q "Kylin" /etc/os-release; then TARGET_BASE="/usr/share" APP_TARGET_DIR="$TARGET_BASE/applications" echo "检测到麒麟系统,使用目标目录: $TARGET_BASE" else TARGET_BASE="/usr/local/share" # 检测是否为 KDE Plasma 桌面环境 if is_kde_plasma; then APP_TARGET_DIR="$TARGET_BASE/applications/apm" echo "检测到 KDE Plasma 桌面环境,使用目标目录: $APP_TARGET_DIR" else APP_TARGET_DIR="$TARGET_BASE/applications" echo "使用标准目标目录: $APP_TARGET_DIR" fi fi function ensure_dir() { local dir="$1" # 检查目录是否为空 if [ -z "$dir" ]; then echo "错误: 目录路径不能为空" return 1 fi # 检查目录是否存在 if [ ! -d "$dir" ]; then echo "目录 '$dir' 不存在,正在创建..." if mkdir -p "$dir"; then echo "成功创建目录 '$dir'" return 0 else echo "错误: 无法创建目录 '$dir'" return 1 fi else return 0 fi } # 函数:检查目录并创建符号链接 process_directory() { local source_dir="$1" local target_dir="$2" local name="$3" if [ -d "$source_dir" ] && [ -n "$(ls -A "$source_dir")" ]; then ln -sv $source_dir/* "$target_dir" 2>/dev/null find "$target_dir" -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & else echo "$name directory is empty or does not exist, skipping..." fi } # 使用动态确定的目标目录 ensure_dir "$APP_TARGET_DIR/" ensure_dir "$TARGET_BASE/icons/" # 处理 applications 目录 process_directory "/var/lib/apm/apm/files/ace-env/amber-ce-tools/data-dir/applications/" \ "$APP_TARGET_DIR/" "Applications" # 处理 icons 目录 process_directory "/var/lib/apm/apm/files/ace-env/amber-ce-tools/data-dir/icons/" \ "$TARGET_BASE/icons/" "Icons" # 等待所有后台任务完成 wait # 迁移老链接(仅非麒麟系统需要) if ! grep -q "Kylin" /etc/os-release; then # 定义可能的链接位置 APM_SUBDIR="/usr/local/share/applications/apm" APP_ROOT_DIR="/usr/local/share/applications" # 根据当前目标目录,确定源目录 if [ "$APP_TARGET_DIR" = "$APM_SUBDIR" ]; then # 当前目标是 apm 子目录,需要检查根目录是否有链接需要迁移 SOURCE_DIR="$APP_ROOT_DIR" else # 当前目标是根目录,需要检查 apm 子目录是否有链接需要迁移 SOURCE_DIR="$APM_SUBDIR" fi # 检查源目录是否存在且与目标目录不同 if [ -d "$SOURCE_DIR" ] && [ "$SOURCE_DIR" != "$APP_TARGET_DIR" ]; then echo "检查并迁移老链接..." # 查找源目录中指向APM数据目录的符号链接 find "$SOURCE_DIR" -maxdepth 1 -type l 2>/dev/null | while read -r link; do target=$(readlink "$link") # 如果链接指向APM的数据目录 if [[ "$target" == /var/lib/apm/apm/files/ace-env/amber-ce-tools/data-dir/applications/* ]]; then filename=$(basename "$link") # 如果新位置没有同名文件,则移动 if [ ! -e "$APP_TARGET_DIR/$filename" ]; then echo "迁移老链接: $filename" mv -v "$link" "$APP_TARGET_DIR/" else echo "新位置已存在 $filename,删除老链接" rm -v "$link" fi fi done # 如果源目录是 apm 子目录且已空,尝试删除 if [ "$SOURCE_DIR" = "$APM_SUBDIR" ] && [ -d "$APM_SUBDIR" ]; then rmdir "$APM_SUBDIR" 2>/dev/null || true fi fi fi