aboutsummarylogtreecommitdiffstats
path: root/kakaotalk
blob: 09c97cf1c511ddc7bebe7fd4e230bdf4051bfbc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# Copyright (c) 2023-2025 Ho Kim (ho.kim@ulagbulag.io). All rights reserved.
# Use of this source code is governed by The Unlicense license that can be
# found in the LICENSE file.

# Prehibit errors
set -e -o pipefail

###########################################################
#   Install Checker                                       #
###########################################################

function _check_wine() {
    WINE_EXE="$1"

    ls "${WINE_EXE}" >/dev/null 2>/dev/null
}

###########################################################
#   Installer                                             #
###########################################################

function _install_wine() {
    SRC_URL='https://app-pc.kakaocdn.net/talk/win32/KakaoTalk_Setup.exe'
    DST_FILE="$(mktemp)"
    WINE_EXE="$1"

    # Mock home directory
    HOME_ORIGIN="${HOME}"
    HOME_PATCH="${WINEPREFIX}/tmp"
    export HOME="${HOME_PATCH}"

    echo 'Installing Kakaotalk...'
    curl -s "${SRC_URL}" -o "${DST_FILE}"
    wineboot
    wine "${DST_FILE}" /S
    rm "${DST_FILE}"

    # Remove tmp home directory
    rm -rf "${HOME_PATCH}"

    # Restore home directory
    export HOME="${HOME_ORIGIN}"

    # Link the wine home directory to the wild
    HOME_WINE="$(
        find "${WINEPREFIX}/drive_c/users" \
            -mindepth 1 -maxdepth 1 -type d -not -name 'Public'
    )"
    mv "${HOME_WINE}" "${HOME_WINE}-bak" || true
    ln -sf "${HOME}" "${HOME_WINE}"

    # Create a desktop shortcut
    if [ -d "${HOME}/Desktop/" ]; then
        ICON_SRC='/usr/share/applications/kakaotalk.desktop'
        ICON_DST="${HOME}/Desktop/kakaotalk.desktop"

        if [ -f "${ICON_SRC}" ] && [ ! -f "${ICON_DST}" ]; then
            mkdir -p "$(dirname "${ICON_DST}")"
            cp "${ICON_SRC}" "${ICON_DST}"
            chmod u+x "${ICON_DST}"

            # Mark as trusted
            if which gio >/dev/null 2>/dev/null; then
                gio set -t string "${ICON_DST}" metadata::xfce-exe-checksum "$(sha256sum "${ICON_SRC}" | awk '{print $1}')"
            fi
        fi
    fi
}

###########################################################
#   Incremental Patches                                   #
###########################################################

function _patch_wine() {
    # Patch #5: Install CJK fonts if not exists
    # Issue: https://github.com/ulagbulag/kakaotalk/issues/5
    if ! cat "${WINEPREFIX}/winetricks.log" 2>/dev/null | grep -Posq '^cjkfonts$'; then
        winetricks --optout --unattended corefonts cjkfonts
    fi

    # Patch #5: Detect and apply IME-dependent environment variables
    # Issue: https://github.com/ulagbulag/kakaotalk/issues/5
    if ! echo "x${XMODIFIERS}" | grep -Posq '^x\*?@im='; then
        # Probe current IME (alphabetical order)
        im=''
        if pgrep fcitx >/dev/null; then
            im='fcitx'
        elif pgrep ibus >/dev/null; then
            im='ibus'
        elif pgrep kime >/dev/null; then
            im='kime'
        elif pgrep nimf >/dev/null; then
            im='nimf'
        fi

        # Apply
        if [ "x${im}" != 'x' ]; then
            export XMODIFIERS="@im=${im}"
        fi
    fi
}

###########################################################
#   Executor                                              #
###########################################################

function _exec_wine() {
    WINE_EXE="$1"

    echo 'Executing Kakaotalk...'
    exec wine "${WINE_EXE}"
}

###########################################################
#   Main Function                                         #
###########################################################

# Define a main function
function main() {
    # Configure environment variables
    export LANG='ko_KR.UTF-8'
    export LC_ALL='ko_KR.UTF-8'
    export WINEARCH='win64'
    export WINEPREFIX="${HOME}/.local/share/kakaotalk"

    # Install
    WINE_EXE="${WINEPREFIX}/drive_c/Program Files (x86)/Kakao/KakaoTalk/KakaoTalk.exe"
    if ! _check_wine "${WINE_EXE}"; then
        _install_wine "${WINE_EXE}"
    fi

    # Patch
    _patch_wine

    # Exec
    _exec_wine "${WINE_EXE}"
}

# Execute main function
main "$@"