#!/bin/bash
# postinst script for kylin-wine-test
#
# see: dh_installdeb(1)

if [[ $(id -u) != 0 ]]; then
    folder=$USER
else
    for folder in $(ls /home/); do
        if test -d /home/$folder/.config/; then
            user_name=$(who | grep $folder)
            if [[ "$user_name" != "" ]]; then
                folder=${user_name%%$" "*}
                break
            fi
        fi
    done
fi

BOTTLE="%BOTTLE%"
USER_FOLDER="/home/$folder"
export WINEPREFIX="/home/$folder/.kylinwine/${BOTTLE}"
export WINEARCH="%ARCH%"
AUTH="%AUTH%"
WINE_BIN="%WINEBIN%"

mkdir -p $USER_FOLDER/.kylinwine
chown -R $folder:$folder $USER_FOLDER/.kylinwine

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_NEW_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
            ln -s ${WINEPREFIX}/drive_c/users/${DEF_USER} ${WINEPREFIX}/drive_c/users/$USER
        fi
    fi
}


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
}

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"
echo $AUTH > "${WINEPREFIX}_dir/.auth"

if [ "$(is_change_arch)" == "1" ] && [ -d "${WINEPREFIX}" ]; then
    rm -rf "${WINEPREFIX}"
    mv "${WINEPREFIX}_dir" "${WINEPREFIX}"
else
    rsync -a "${WINEPREFIX}_dir/" "${WINEPREFIX}/"
    if [ -d "${WINEPREFIX}/.cache" ]; then
        rm -rf "${WINEPREFIX}/.cache"
    fi
fi

recon_cxoffice_symlink

chown -R $folder:$folder ${WINEPREFIX}
kysec_set -rn exectl -v verified ${WINEPREFIX}

if [ -d "${WINEPREFIX}_dir/" ]; then
    rm -rf "${WINEPREFIX}_dir/"
fi

update-icon-caches /usr/share/icons/* >/dev/null|| true
update-desktop-database -q || true

exit 0
