#!/bin/bash

BOTTLE="%BOTTLE%"
USER_FOLDER=$HOME
disclaimer="/opt/kylin-wine/app/${BOTTLE}/Disclaimers-${BOTTLE}.txt"
export WINEPREFIX="$HOME/.kylinwine/${BOTTLE}"
export WINEARCH="%ARCH%"
WINE_BIN="%WINEBIN%"

%ENVIRONMENT%
AUTH="%AUTH%"

is_change_arch()
{
    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() {
    BOTTLE_NEW_USER="${WINEPREFIX}/drive_c/users/kylin"
    BOTTLE_OLD_USER="${WINEPREFIX}/drive_c/users/crossover"
    if [ -d "${BOTTLE_OLD_USER}" ]; then 
        DEF_USER="crossover";
        if [ -L ${BOTTLE_NEW_USER} ] && [ "$(readlink ${BOTTLE_NEW_USER})" = "${BOTTLE_OLD_USER}" ]; then
            :
        else
            rm -rf ${BOTTLE_NEW_USER}
            ln -s ${BOTTLE_OLD_USER} ${BOTTLE_NEW_USER}
        fi
    elif [ -d ${BOTTLE_OLD_USER} ]; 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
            rm -rf ${WINEPREFIX}/drive_c/users/$USER
            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 "# 初始化环境..."
        "/opt/kylin-wine/wine8/bin/kylin-wine" 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
        bottle_init
        show_disclaimers
        echo $AUTH > "${WINEPREFIX}/.disclaimer"
    else
        OLD_AUTH=$(cat "${WINEPREFIX}/.auth")
        if [ "$AUTH" != "$OLD_AUTH" ]; then
            bottle_init
        fi
	      IS_DISCLAIMER=$(cat "${WINEPREFIX}/.disclaimer")
        if [ "$AUTH" != "$IS_DISCLAIMER" ]; then
            show_disclaimers
            echo $AUTH > "${WINEPREFIX}/.disclaimer"
        fi

    fi

}


show_disclaimers()
{
    title="${BOTTLE} (win32)"
    problem=$(cat $disclaimer)
    cancel="拒绝"
    ok="接受"
    zenity --question \
	    --title="${title}" \
	    --text="${problem}" \
	    --cancel-label="${cancel}" \
	    --ok-label="${ok}" \
	    --width=500 \
	    --height=200 \
	    --window-icon="info" \
      --icon-name="info"
#            --window-icon="/usr/share/icons/hicolor/48x48/apps/kylin-wine-E282_WeChat.0.png" \

    if [ "$?" != "0" ]; then exit 1; fi
}


is_init
cd ~/.kylinwine/
cmd="\"%WINEBIN%\" start /d \"%EXECPATH%\" \"%EXECNAME%\" %PARAM%" 
if command -v phybin &> /dev/null; then
    phybin -- "$cmd"
elif command -v exagear &> /dev/null; then
    exagear -- "$cmd"
else
    eval "$cmd"
fi
