#!/bin/bash

# Wrapper script to disable Discord's automatic updates

# The main binary files of the Discord .tar.gz are in lib64/discord.
# This wrapper script "disable-updates.sh" is renamed and installed in /usr/bin/discord and calls the main binary.
# So the .desktop file calls the wrapper script instead of the main binary.

# in the file $HOME/.config/discord/settings.json
# if the setting SKIP_HOST_UPDATE does not exist add it
# otherwise leave it untouched
CONFIGFILE="$HOME/.config/discord/settings.json"
EXE="$(dirname "$0")/../lib64/discord/discord"

if [[ -x "$EXE" ]]; then
    if [[ -d "$HOME/.config" && ! -f "$CONFIGFILE" ]]; then
        mkdir -p "$HOME/.config/discord"
        printf '%s\n' '{"SKIP_HOST_UPDATE": true}' > "$CONFIGFILE"
    elif [[ -f "$CONFIGFILE" ]]; then
        ! grep -qF SKIP_HOST_UPDATE "$CONFIGFILE" && \
          sed -i '/^{/s@$@\n  "SKIP_HOST_UPDATE": true,@' "$CONFIGFILE"
    fi
    exec "$EXE" "$@"
fi

