#!/bin/bash

export PATH=$PATH:/sbin

create_directory() {
    local dir_path="$1"
    # 检查文件夹是否存在
    if [ ! -d "$dir_path" ]; then
        mkdir -p "$dir_path"
    fi
}

move_file() {
    source_path=$1
    target_path=$2
    # 检查源文件或目录是否存在
    if [ ! -e "$source_path" ]; then
        echo "$source_path not exist, no move file."
        return
    fi
    # 分离目标路径的目录部分
    if [ -f "$target_path" ]; then
        echo this is a file
        target_dir=$(dirname "$target_path")
    else
        target_dir=$target_path
    fi
    
    # 检查目标目录是否存在，如果不存在则创建
    if [ ! -d "$target_dir" ]; then
        mkdir -p "$target_dir"
        echo "$target_dir not exist, have created the directory."
    fi
    
    # 执行mv操作
    mv $source_path $target_path
    echo $source_path move to $target_path
}

copy_file() {
    # 功能：复制文件或目录到指定位置，自动创建目标目录
    # 参数：$1 源文件或目录路径，$2 目标路径
    local source_path=$1
    local target_path=$2

    # 检查源文件或目录是否存在
    if [ ! -e "$source_path" ]; then
        echo "$source_path not exist, no copy action taken."
        return 1
    fi

    # 确保目标路径以 / 结尾，如果不是目录则取其目录部分
    local last_char=$(echo "$target_path" | awk '{sub(/ *$/,""); print substr($0,length)}')
    if [ "$last_char" == "/" ]; then
        target_dir=$target_path
    else
        if [ -d "$target_path" ]; then
            target_dir=$target_path
        else
            target_dir=$(dirname "$target_path")
        fi
    fi
    echo "target_dir: $target_dir"

    # 创建目标目录（如果需要）
    if [ ! -d "$target_dir" ]; then
        mkdir -p "$target_dir" && echo "Directory $target_dir not found, created."
    fi

    # 复制操作，根据源路径是否为目录决定是否使用-r
    if [ -d "$source_path" ]; then
        cp -rpf "$source_path" "$target_path"
    else
        cp -pf "$source_path" "$target_path"
    fi

    # 检查复制操作是否成功
    if [ $? -eq 0 ]; then
        echo "$source_path copied to $target_path successfully."
        return 0
    else
        echo "Error occurred while copying $source_path to $target_path."
        return 1
    fi
}

update_initrd() {
    VKERNEL=$(uname -r)
    # Array of available initrd creation commands
    commands="update-initramfs dracut mkinitrd"

    for cmd in $commands; do
        if [ -n "$cmd" ] && command -v "$cmd" >/dev/null 2>&1; then
            case "$cmd" in
                update-initramfs)
                    "$cmd" -u -k "$VKERNEL"
                    ;;
                dracut)
                    "$cmd" -f "/boot/initrd.img-$VKERNEL" "$VKERNEL"
                    ;;
                *)
                    # For any other initrd tool
                    "$cmd" -f "/boot/initrd.img-$VKERNEL" "$VKERNEL"
                    ;;
            esac
            return
        fi
    done

    # If none of the commands are found, print a message
    echo "No suitable initrd creation command found. Initrd not updated."
}

# 软件安装路径
BASE_DIR=/usr/share/loongnix-kde-theme

# sddm配置文件路径
SDDM_CONFIG_FILE="/etc/sddm.conf.d/kde_settings.conf"

# icon 主题安装
copy_file $BASE_DIR/icons/Loongnix /usr/share/icons/
copy_file $BASE_DIR/icons/Flat-Remix-Blue /usr/share/icons/
if which update-icon-caches >/dev/null 2>&1 ; then
    update-icon-caches /usr/share/icons/Loongnix /usr/share/icons/Flat-Remix-Blue
fi

# sddm 主题安装
copy_file $BASE_DIR/sddm/Loongnix /usr/share/sddm/themes/
# plasma主题安装
copy_file $BASE_DIR/plasma/desktoptheme/Loongnix /usr/share/plasma/desktoptheme/
copy_file $BASE_DIR/plasma/look-and-feel/Loongnix /usr/share/plasma/look-and-feel/
# 修改sddm配置文件
if [ -f "$SDDM_CONFIG_FILE" ]; then 
    # 备份原文件
    copy_file $SDDM_CONFIG_FILE $BASE_DIR/backaup/
    kwriteconfig5 --file $SDDM_CONFIG_FILE --group Theme --key Current Loongnix
    kwriteconfig5 --file $SDDM_CONFIG_FILE --group General --key Theme Loongnix
else
    copy_file $BASE_DIR/etc/sddm.conf.d/kde_settings.conf /etc/sddm.conf.d/
fi

# 备份原头像文件
move_file /usr/share/kpackage/kcms/kcm_users/contents/img $BASE_DIR/backaup/kcm_users/
move_file /usr/share/kpackage/kcms/kcm_users/contents/ui/UserDetailsPage.qml $BASE_DIR/backaup/kcm_users/
copy_file $BASE_DIR/kpackage/kcms/kcm_users/contents/img /usr/share/kpackage/kcms/kcm_users/contents/
copy_file $BASE_DIR/kpackage/kcms/kcm_users/contents/ui/UserDetailsPage.qml /usr/share/kpackage/kcms/kcm_users/contents/ui/

# 备份原设置界面logo
move_file /usr/share/kpackage/genericqml/org.kde.systemsettings.sidebar/contents/images/plasma-logo.svg $BASE_DIR/backaup/genericqml/
move_file /usr/share/kpackage/genericqml/org.kde.systemsettings.sidebar/contents/ui/introPage.qml $BASE_DIR/backaup/genericqml/
copy_file $BASE_DIR/kpackage/genericqml/org.kde.systemsettings.sidebar /usr/share/kpackage/genericqml/

# 修改默认壁纸主题
copy_file $BASE_DIR/wallpapers/Loongnix /usr/share/wallpapers/

# 修改头像，设置界面的logo
copy_file $BASE_DIR/kpackage/kcms/kcm_users/contents/img/face1.png /etc/skel/.face
# 当前用户
curuser=$(who | grep -w 'tty7' | awk '{print $1}')
# 修改当前用户的头像
copy_file $BASE_DIR/kpackage/kcms/kcm_users/contents/img/face1.png /home/$curuser/.face
chown $curuser:$curuser /home/$curuser/.face
chmod 755 /home/$curuser/.face
# 修改当前用户的全局设置
if [ ! -f "/home/$curuser/.config/kdeglobals" ]; then
    echo "/home/$curuser/.config/kdeglobals is not exist"
    copy_file $BASE_DIR/kdeglobals /home/$curuser/.config/
    chown $curuser:$curuser /home/$curuser/.config/kdeglobals
    chmod 755 /home/$curuser/.config/kdeglobals
else
    # 修改当前用户的LookAndFeelPackage主题
    kwriteconfig5 --file /home/$curuser/.config/kdeglobals --group KDE --key LookAndFeelPackage Loongnix
    # 修改当前用户的图标
    kwriteconfig5 --file /home/$curuser/.config/kdeglobals --group Icons --key Theme Loongnix
fi

# 修改当前用户的欢迎屏幕
if [ ! -f "/home/$curuser/.config/ksplashrc" ]; then
    echo "/home/$curuser/.config/ksplashrc is not exist"
    copy_file $BASE_DIR/ksplashrc /home/$curuser/.config/
    chown $curuser:$curuser /home/$curuser/.config/ksplashrc
    chmod 755 /home/$curuser/.config/ksplashrc
else
    # 修改当前用户的欢迎屏幕
    kwriteconfig5 --file /home/$curuser/.config/ksplashrc --group KSplash --key Engine KSplashQML
    # 修改当前用户的欢迎屏幕
    kwriteconfig5 --file /home/$curuser/.config/ksplashrc --group KSplash --key Theme Loongnix
fi
# 修改当前用户的plasma主题
if [ ! -f "/home/$curuser/.config/plasmarc" ]; then
    echo "/home/$curuser/.config/plasmarc is not exist"
    copy_file $BASE_DIR/plasmarc /home/$curuser/.config/
    chown $curuser:$curuser /home/$curuser/.config/plasmarc
    chmod 755 /home/$curuser/.config/plasmarc
else
    # 修改当前用户的plasma主题
    kwriteconfig5 --file /home/$curuser/.config/plasmarc --group Theme --key name Loongnix
fi

# 修改当前用户的plasma主题
if [ ! -f "/home/$curuser/.config/gtk-3.0/settings.ini" ]; then
    echo "/home/$curuser/.config/gtk-3.0/settings.ini is not exist"
    copy_file $BASE_DIR/settings.ini /home/$curuser/.config/gtk-3.0/
    chown $curuser:$curuser /home/$curuser/.config/gtk-3.0/settings.ini
    chmod 755 /home/$curuser/.config/gtk-3.0/settings.ini
else
    # 修改当前用户的plasma主题
    kwriteconfig5 --file /home/$curuser/.config/gtk-3.0/settings.ini --group Settings --key gtk-icon-theme-name Loongnix
fi

copy_file $BASE_DIR/xsettingsd.conf /home/$curuser/.config/xsettingsd/

su -c 'lookandfeeltool -a Loongnix' $curuser
echo "
if [ -f ~/.change_wallpaper ]; then
    sh /usr/share/loongnix-kde-theme/changed_wallpaper.sh &
    rm -rf ~/.change_wallpaper
fi" >> /home/$curuser/.bashrc
touch /home/$curuser/.change_wallpaper
chown $curuser:$curuser /home/$curuser/.change_wallpaper

# 在桌面添加目录快捷方式
if [ -d "/home/$curuser/桌面" ]; then
    destop_dir="/home/$curuser/桌面"
elif [ -d "/home/$curuser/Desktop" ]; then
    destop_dir="/home/$curuser/Desktop"
fi

# 全局添加文件夹快捷方式
if [ ! -f "/usr/share/applications/org.kde.dolphin.desktop" ]; then
    copy_file $BASE_DIR/desktop/org.kde.dolphin.desktop /usr/share/applications/
fi

# 用户桌面添加文件夹快捷方式
if [ ! -f "$destop_dir/org.kde.dolphin.desktop" ]; then
    copy_file $BASE_DIR/desktop/org.kde.dolphin.desktop $destop_dir/
fi

# 全局添加回收站快捷方式
if [ ! -f "/usr/share/applications/trash_⁄.desktop" ]; then
    copy_file $BASE_DIR/desktop/trash_⁄.desktop /usr/share/applications/
fi
# 用户桌面添加回收站快捷方式
if [ ! -f "$destop_dir/trash_⁄.desktop" ]; then
    copy_file $BASE_DIR/desktop/trash_⁄.desktop $destop_dir/
fi

# 设置默认生效配置文件
# 全局设置
if [ ! -f "/etc/skel/.config/kdeglobals" ]; then
    echo "/etc/skel/.config/kdeglobals is not exist"
    copy_file $BASE_DIR/kdeglobals /etc/skel/.config/
else
    # 修改当前用户的LookAndFeelPackage主题
    kwriteconfig5 --file /etc/skel/.config/kdeglobals --group KDE --key LookAndFeelPackage Loongnix
    # 修改当前用户的图标
    kwriteconfig5 --file /etc/skel/.config/kdeglobals --group Icons --key Theme Loongnix
fi
if [ ! -f "/etc/skel/.config/ksplashrc" ]; then
    echo "/etc/skel/.config/ksplashrc is not exist"
    copy_file $BASE_DIR/ksplashrc /etc/skel/.config/
else
    # 修改当前用户的LookAndFeelPackage主题
    kwriteconfig5 --file /etc/skel/.config/ksplashrc --group KSplash --key Engine KSplashQML
    # 修改当前用户的图标
    kwriteconfig5 --file /etc/skel/.config/ksplashrc --group KSplash --key Theme Loongnix
fi
if [ ! -f "/etc/skel/.config/plasmarc" ]; then
    echo "/etc/skel/.config/plasmarc is not exist"
    copy_file $BASE_DIR/plasmarc /etc/skel/.config/
else
    # 修改当前用户的LookAndFeelPackage主题
    kwriteconfig5 --file /etc/skel/.config/plasmarc --group Theme --key name Loongnix
fi

# 复制loongnix开机动画主题到系统开机动画主题目录
copy_file $BASE_DIR/plymouth/themes/loongnix /usr/share/plymouth/themes/
# 切换plymouth主题
plymouth-set-default-theme loongnix
# Array of available initrd creation commands
update_initrd
