#!/bin/sh
# wine-sg-redirect start|configure|clear: keep the Wine the running wineserver uses until it
# restarts, as Windows keeps the old binaries until the restart (wine-sg 7840).
#
# An update that changes the server protocol cannot talk to the running server. Its files land
# when the package is unpacked, the server restarts only later (or at the next boot), and every
# program started in between died with "version mismatch" -- the lock screen among them, so
# Win+L showed an empty screen for minutes (David's machine, protocol 877 -> 880).
#   start      (preinst, before the new files land) keep the running tree as hard links in
#              $KEPT and name it in $REDIRECT; Wine's loader then starts every program from it
#   configure  (postinst) the new Wine can talk to the running server (same protocol, or a
#              test client gets no "version mismatch"): new programs start from it at once
#   clear      (when the server starts: the sg-wineserver drop-in; on removal) back to the
#              installed Wine
set -u
TREE=${WINE_SG_TREE:-/opt/wine-sg}
KEPT=${WINE_SG_KEPT:-/opt/wine-sg-running}
REDIRECT=${WINE_SG_REDIRECT_FILE:-/run/wine-sg/redirect}
SG_PREFIX=${WINE_SG_MACHINE_PREFIX:-/var/lib/stained-glass/prefix}
REBOOT=${WINE_SG_REBOOT_FILE:-/run/reboot-required}

# Debian's "restart required" (sg-session's restart notice shows it): only when the running
# Wine is kept, never when the new one takes over at once
reboot_required() {
    [ -e "$REBOOT" ] || printf '%s\n' "*** System restart required: an update to the Windows programs' support (Wine) ***" > "$REBOOT"
    grep -qx wine-sg "$REBOOT.pkgs" 2>/dev/null || echo wine-sg >> "$REBOOT.pkgs"
}

# a wineserver runs from DIR: its executable is DIR/bin/wineserver -- by path, also when an update
# has replaced the file since ("(deleted)"; an update the server could still talk to), or by
# device and inode, whatever the path (the kept tree's hard link to the running server's file)
server_from() {
    _want=$(stat -L -c %d:%i "$1/bin/wineserver" 2>/dev/null) || _want=none
    for _p in /proc/[0-9]*; do
        case "$(readlink "$_p/exe" 2>/dev/null)" in
        "$1/bin/wineserver"|"$1/bin/wineserver (deleted)") return 0 ;;
        esac
        [ "$(stat -L -c %d:%i "$_p/exe" 2>/dev/null)" = "$_want" ] && return 0
    done
    return 1
}

clear_kept() {
    rm -f "$REDIRECT"
    rm -rf "$KEPT"
}

# the installed Wine talks to the running server: same protocol number, or else ask it
compatible() {
    _new=$(cat "$TREE/share/wine/sg-server-protocol" 2>/dev/null)
    _old=$(cat "$KEPT/share/wine/sg-server-protocol" 2>/dev/null)
    if [ -n "$_new" ] && [ -n "$_old" ]; then
        [ "$_new" = "$_old" ]
        return
    fi
    # an older tree has no number: a client of the new Wine on the machine's prefix
    [ -d "$SG_PREFIX" ] || return 1
    _owner=$(stat -c %U "$SG_PREFIX" 2>/dev/null) || return 1
    _out=$(cd / && timeout 60 runuser -u "$_owner" -- env -i PATH=/usr/bin:/bin HOME=/nonexistent \
        WINEPREFIX="$SG_PREFIX" WINEDEBUG=-all SG_WINE_REDIRECT=0 WINEDLLOVERRIDES=winemenubuilder.exe=d \
        SG_WINSTATION='__wineservice_winstation\Default' "$TREE/bin/wine" cmd /c exit 2>&1 </dev/null)
    case "$_out" in *"version mismatch"*) return 1 ;; esac
    return 0
}

case "${1:-}" in
start)
    # a restart is already pending (the redirect goes when the server starts, and /run at boot):
    # the running server is the kept tree's, keep it
    [ -e "$REDIRECT" ] && [ -x "$KEPT/bin/wine" ] && exit 0
    clear_kept
    server_from "$TREE" || exit 0
    if ! cp -al "$TREE" "$KEPT.new" || ! mv "$KEPT.new" "$KEPT"; then
        rm -rf "$KEPT.new"
        exit 0
    fi
    mkdir -p "$(dirname "$REDIRECT")"
    chmod 755 "$(dirname "$REDIRECT")"
    printf '%s\n' "$KEPT" > "$REDIRECT.new" && chmod 644 "$REDIRECT.new" && mv "$REDIRECT.new" "$REDIRECT"
    ;;
configure)
    if [ ! -e "$REDIRECT" ]; then
        rm -rf "$KEPT"
        exit 0
    fi
    # no server left from before (it restarted already), or the new Wine talks to it
    if { ! server_from "$KEPT" && ! server_from "$TREE"; } || compatible; then
        clear_kept
    else
        echo "wine-sg: Windows programs start from the running Wine until the Windows system restarts"
        reboot_required
    fi
    ;;
clear)
    clear_kept
    ;;
*)
    echo "usage: $0 start|configure|clear" >&2
    exit 2
    ;;
esac
exit 0
