#!/bin/bash

set -eu

debPath="$1"
debName=${debPath##*/}
containerName=${debName%_*}
echo "111111111$containerName"

containerUser=$(id -u)
containerEnv="-e DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" -e QT_X11_NO_MITSHM=1"
containerImage="baseimage_v10"
excludeDirList=("/backup" "/cdrom" "/lost+found" "/sys" "/fs.old" "/usr" "/opt" "/proc" "/boot" "/etc" "/root" "/srv" "/var")
containerSharedDir=""
for each in /*; do
    # 过滤非目录和链接目录
    if [ ! -d "$each" ] || [ -L "$each" ]; then
        continue
    fi
    if [ $each == "/dev" ]; then
        containerSharedDir=$containerSharedDir" -v $each:$each"
    elif [ $each == "/home" ]; then
        containerSharedDir=$containerSharedDir" -v $each:$each"
    elif [ $each == "/media" ]; then
        containerSharedDir=$containerSharedDir" -v $each:$each"
    elif [ $each == "/mnt" ]; then
        containerSharedDir=$containerSharedDir" -v $each:$each"
    elif [ $each == "/tmp" ]; then
        containerSharedDir=$containerSharedDir" -v $each:$each"
    elif [ $each == "/data" ]; then
        containerSharedDir=$containerSharedDir" -v $each:$each"
    # 输入法需要
    elif [ $each == "/run" ]; then
        containerSharedDir=$containerSharedDir" -v $each:$each"
    # 必要目录挂载完毕，寻找是否有自建目录
    else
        flag=0
        for tmpItem in ${excludeDirList[@]}; do
            if [ $each == $tmpItem ]; then
                flag=1
                break
            else
                flag=0
                continue
            fi
        done
        if [ $flag -eq 0 ]; then
            echo "找到用户自建目录: $each"
            containerSharedDir=$containerSharedDir" -v $each:$each"
        fi
    fi
done

dbus-send --system --type=method_call --print-reply \
    --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentInspect \
    string:"$containerName"
if [ $? = 0 ]; then
    dbus-send --system --type=method_call --print-reply \
        --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentRm \
        string:"$containerName"
fi
dbus-send --system --type=method_call --print-reply \
    --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentCreate \
    string:"$containerUser" string:"$containerName" string:"$containerEnv" string:"$containerSharedDir" string:"$containerImage"

dbus-send --system --type=method_call --print-reply \
    --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentCp \
    string:"/fs.old/usr/bin/sudo" string:"$containerName:/usr/bin"

dbus-send --system --type=method_call --print-reply \
    --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentCp \
    string:"$1" string:"$containerName:/"

dbus-send --system --type=method_call --print-reply \
    --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentStart \
    string:"$containerName"

sudopassword=$(zenity --password --title "旧环境sudo密码")
tmp="$1"
tmp=${tmp##*/}
echo "###########$tmp"
# 安装软件包
# containerDebInstallCmd="echo "$sudopassword" | sudo -S dpkg -i /$tmp"
containerDebInstallCmd="echo "$sudopassword" | sudo -S apt install -y /$tmp"

dbus-send --system --type=method_call --print-reply --reply-timeout=300000\
    --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentExec \
    string:"$containerName" string:"$containerDebInstallCmd"

# # 运行 d-feet
Exec="/usr/bin/d-feet"
dbus-send --system --type=method_call --print-reply \
    --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentRun \
    string:"$containerName" string:"$Exec"

# # 运行wps
# Exec2="wps"
# dbus-send --system --type=method_call --print-reply \
#     --dest=com.kylin.dockerAgent /com/kylin/dockerAgent com.kylin.dockerAgent.dockerAgentRun \
#     string:"$containerName" string:"$Exec2"


