#!/bin/bash


BOTTLE="%BOTTLE%"
USER_FOLDER=$HOME
export WINEPREFIX="$HOME/.kylinwine/${BOTTLE}"
export WINEARCH="%ARCH%"
WINE_BIN="%WINEBIN%"
AUTH="%AUTH%"
CODE=0
RUN_ARCH="$(uname -m)"
kylinwine_tips="/opt/kylin-wine/inst-translation/kylinwine_tips.${RUN_ARCH}"

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 != $NEW_ARCH ]; then
        echo 1
    else
        echo 0
    fi
}

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
}

show_tips()
{
    text=$1
    progress=$2

    if [ ! -x "$kylinwine_tips" ]; then return; fi
    $kylinwine_tips unpack --prefix=${WINEPREFIX} "$text" $progress >/dev/null &
}


bottle_init()
{
    if [ ! -x "$kylinwine_tips" ]; then
        (
            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
                if [ -d "${WINEPREFIX}/.cache" ]; then
                    rm -rf "${WINEPREFIX}/.cache"
                fi
            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/"
            sleep 1

            echo 100
            echo "# 安装完成"
        ) | zenity --progress \
                   --title="正在初始化..." \
                   --no-cancel \
                   --text="" \
                   --percentage=0 \
                   --height=100 \
                   --width=300 \
                   --auto-close
    else
        (
            show_tips "解压文件..." 1
            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"

            show_tips "安装文件..." 30
            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
                if [ -d "${WINEPREFIX}/.cache" ]; then
                    rm -rf "${WINEPREFIX}/.cache"
                fi
            fi

            show_tips "初始化环境..." 60
            $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

            show_tips "清理缓存..." 98
            rm -rf "${WINEPREFIX}_dir/"
            sleep 1

            show_tips "安装完成" 100
        )
    fi
}


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
