#!/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
}

# 生成initrd
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-theme
# 备份原cartoon主题部分文件，以便恢复cartoon主题
# 备份Login背景图片
move_file /usr/share/cartoon-artwork/login-bg.png $BASE_DIR/cartoon_backup/cartoon-artwork/login-bg.png
# 备份用户头像图片
move_file /usr/share/pixmaps/faces $BASE_DIR/cartoon_backup/avatar/
# 备份默认用户头像配置文件
move_file /var/lib/AccountsService/users/loongson $BASE_DIR/cartoon_backup/avatar/

# 添加锁屏背景
copy_file $BASE_DIR/backgrounds/loongnix /usr/share/backgrounds/
copy_file $BASE_DIR/backgrounds/debain-loongnix.xml /usr/share/gnome-background-properties/
# 配置默认桌面背景和锁屏登陆背景
copy_file $BASE_DIR/backgrounds/26_loongnix-default-settings.gschema.override /usr/share/glib-2.0/schemas/
copy_file $BASE_DIR/backgrounds/loongnix-theme /usr/share/desktop-base/
# 修改登陆背景
copy_file $BASE_DIR/cartoon-artwork/login-bg.png /usr/share/cartoon-artwork/
# 用户头像添加
copy_file $BASE_DIR/backgrounds/faces /usr/share/pixmaps/
# 默认用户头像修改
copy_file $BASE_DIR/backgrounds/faces/loongson /var/lib/AccountsService/users/
# 修改系统默认头像
copy_file $BASE_DIR/backgrounds/faces/face1.png /etc/skel/.face

# 复制Loongnix主题到系统主题目录
copy_file $BASE_DIR/Loongnix /usr/share/themes/
# 复制Loongnix-Icon主题到系统Icon主题目录
copy_file $BASE_DIR/Loongnix-Icon /usr/share/icons/
# 生成主题缓存
if which gtk-update-icon-cache >/dev/null 2>&1 ; then
    gtk-update-icon-cache -f -t /usr/share/icons/Loongnix-Icon/.
fi

# 复制loongnix开机动画主题到系统开机动画主题目录
copy_file $BASE_DIR/plymouth/themes/loongnix /usr/share/plymouth/themes/

# 复制panel样式到系统panel样式
copy_file $BASE_DIR/panel-layout/loongnix.layout /usr/share/cartoon-panel/layouts/

# 当前用户
curuser=$(who | grep -w 'tty7' | awk '{print $1}')
# 修改当前用户默认头像
copy_file $BASE_DIR/backgrounds/faces/face1.png /home/$curuser/.face
chown $curuser:$curuser /home/$curuser/.face
chmod 755 /home/$curuser/.face
echo "当前用户名：$curuser"
# 切换GTK主题
su -c 'gsettings set org.cartoon.interface gtk-theme "Loongnix"' $curuser
# 切换ICON主题
su -c 'gsettings set org.cartoon.interface icon-theme "Loongnix-Icon"' $curuser
# 切换marco主题
su -c 'gsettings set org.cartoon.Marco.general theme "Loongnix"' $curuser

# 切换cartoon-panel样式
su -c 'gsettings set org.cartoon.panel default-layout "loongnix"' $curuser
su -c 'cartoon-panel --reset' $curuser

# 修改默认主题使其生效
glib-compile-schemas /usr/share/glib-2.0/schemas/

# 切换plymouth主题
plymouth-set-default-theme loongnix
# Array of available initrd creation commands
update_initrd

