#!/bin/bash


BOTTLE="7z"
USER_FOLDER=$HOME
export WINEPREFIX="$HOME/.kylinwine/${BOTTLE}"
export WINEARCH="win64"
WINE_BIN="/opt/kylin-wine/wine8/bin/kylin-wine"
AUTH="1765178414"
CODE=0



is_change_arch()
{
    NEW_ARCH=$WINEARCH
    if [ "wow64" == "$WINEARCH" ]; then
        NEW_ARCH="win64"
    fi
    if [ -d "$WINEPREFIX" ] && [ -d "$WINEPREFIX/drive_c/windows/syswow64" ]; then
        OLD_ARCH="win64"
    else
        OLD_ARCH="win32"
    fi
    if [ $OLD_ARCH != $WINEARCH ]; then
        echo 1
    fi
    echo 0
}

recon_cxoffice_symlink() {
    if [ -d "${WINEPREFIX}/drive_c/users/crossover" ]; then DEF_USER="crossover";
    elif [ -d "${WINEPREFIX}/drive_c/users/kylin" ]; then DEF_USER="kylin";
    else return; fi

    rm -rf "${WINEPREFIX}/drive_c/users/${DEF_USER}/Desktop"
    if [ -d $USER_FOLDER/Desktop ]; then
        ln -s $USER_FOLDER/Desktop "${WINEPREFIX}/drive_c/users/${DEF_USER}/Desktop"
    elif [ -d $USER_FOLDER/桌面 ]; then
        ln -s $USER_FOLDER/桌面 "${WINEPREFIX}/drive_c/users/${DEF_USER}/Desktop"
    fi
    rm -rf "${WINEPREFIX}/drive_c/users/${DEF_USER}/Documents"
    if [ -d $USER_FOLDER/Documents ]; then
        ln -s $USER_FOLDER/Documents "${WINEPREFIX}/drive_c/users/${DEF_USER}/Documents"
    elif [ -d $USER_FOLDER/文档 ]; then
        ln -s $USER_FOLDER/文档 "${WINEPREFIX}/drive_c/users/${DEF_USER}/Documents"
    fi
    rm -rf "${WINEPREFIX}/drive_c/users/${DEF_USER}/Downloads"
    if [ -d $USER_FOLDER/Downloads ]; then
        ln -s $USER_FOLDER/Downloads "${WINEPREFIX}/drive_c/users/${DEF_USER}/Downloads"
    elif [ -d $USER_FOLDER/下载 ]; then
        ln -s $USER_FOLDER/下载 "${WINEPREFIX}/drive_c/users/${DEF_USER}/Downloads"
    fi
    rm -rf "${WINEPREFIX}/drive_c/users/${DEF_USER}/Music"
    if [ -d $USER_FOLDER/Music ]; then
        ln -s $USER_FOLDER/Music "${WINEPREFIX}/drive_c/users/${DEF_USER}/Music"
    elif [ -d $USER_FOLDER/音乐 ]; then
        ln -s $USER_FOLDER/音乐 "${WINEPREFIX}/drive_c/users/${DEF_USER}/Music"
    fi
    rm -rf "${WINEPREFIX}/drive_c/users/${DEF_USER}/Pictures"
    if [ -d $USER_FOLDER/Pictures ]; then
        ln -s $USER_FOLDER/Pictures "${WINEPREFIX}/drive_c/users/${DEF_USER}/Pictures"
    elif [ -d $USER_FOLDER/图片 ]; then
        ln -s $USER_FOLDER/图片 "${WINEPREFIX}/drive_c/users/${DEF_USER}/Pictures"
    fi
    rm -rf "${WINEPREFIX}/drive_c/users/${DEF_USER}/Videos"
    if [ -d $USER_FOLDER/Videos ]; then
        ln -s $USER_FOLDER/Videos "${WINEPREFIX}/drive_c/users/${DEF_USER}/Videos"
    elif [ -d $USER_FOLDER/视频 ]; then
        ln -s $USER_FOLDER/视频 "${WINEPREFIX}/drive_c/users/${DEF_USER}/Videos"
    fi

    if [ $(echo "$WINE_BIN"| grep "/wine9/")  ];then
        if [ "${DEF_USER}" != "$USER" ]; then
            ln -s ${WINEPREFIX}/drive_c/users/${DEF_USER} ${WINEPREFIX}/drive_c/users/$USER
        fi
    fi
}

bottle_init()
{
    (
        echo 1
        echo "# 解压文件..."
        mkdir -p "$HOME/.kylinwine/"
        if [ -d "${WINEPREFIX}_dir" ]; then
            rm -rf "${WINEPREFIX}_dir/"
        fi
        ulimit -n 65535
        unsquashfs -no-xattrs -d "${WINEPREFIX}_dir" "/opt/kylin-wine/app/${BOTTLE}/${BOTTLE}.sqfs" >/dev/null 2>&1
        echo $AUTH > "${WINEPREFIX}_dir/.auth"

        echo 30
        echo "# 安装文件..."
        if [ "$(is_change_arch)" == "1" ]; then
            rsync -a --delete "${WINEPREFIX}_dir/" "${WINEPREFIX}/" >/dev/null 2>&1
        else
            rsync -a "${WINEPREFIX}_dir/" "${WINEPREFIX}/" >/dev/null 2>&1
        fi

        echo 60
        echo "# 初始化环境..."
        $WINE_BIN wineboot -u >/dev/null 2>&1
        pkexec /usr/bin/wine-mount "${WINEPREFIX}/" >/dev/null 2>&1
        recon_cxoffice_symlink >/dev/null 2>&1

        echo 98 
        echo "# 清理缓存..."
        rm -rf "${WINEPREFIX}_dir/"

        echo 99
        echo "# 安装完成"
        sleep 1

        echo 100
        echo ""
    ) | zenity --progress \
        --title="正在初始化..." \
        --no-cancel \
        --text="" \
        --percentage=0 \
        --height=100 \
        --width=300 \
        --auto-close

}


is_init()
{
    if [ ! -d "$WINEPREFIX" ] || [ ! -f  "${WINEPREFIX}/.auth" ]
    then
        echo 1
    else
        OLD_AUTH=$(cat "${WINEPREFIX}/.auth")
        if [ "$AUTH" != "$OLD_AUTH" ]; then
            echo 1
        else
            echo 0
        fi
    fi

}

reset() 
{
    if [ -d "$WINEPREFIX" ]; then
        rm -rf "$WINEPREFIX"
    fi
    bottle_init
}

TYPE=$1
shift
case $TYPE in
    init)
        ret=$(is_init)
        if [ $ret == "1" ]; then
            bottle_init
            CODE=1
        fi
        ;;
    check)
        ret=$(is_init)
        if [ $ret == "1" ]; then
            CODE=1
        fi
        ;;
    reset)
        reset
        ;;
    *) 
        echo "未知选项"
        ;;
esac
exit $CODE
