#!/usr/bin/env python3
import subprocess
import sys

# defining keys & strings to be used
key = "org.gnome.settings-daemon.plugins.media-keys custom-keybindings"
subkey1 = key.replace(" ", ".")[:-1]+":"
item_s = "/"+key.replace(" ", "/").replace(".", "/")+"/"
firstname = "custom"
# get the current list of custom shortcuts
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
array_str = get("gsettings get "+key)
# in case the array was empty, remove the annotation hints
command_result = array_str.lstrip("@as")
current = eval(command_result)
# make sure the additional keybinding mention is no duplicate
n = 1
while True:
    new = item_s+firstname+str(n)+"/"
    if new in current:
        n = n+1
    else:
        break
# add the new keybinding to the list
current.append(new)
# create the shortcut, set the name, command and shortcut key
cmd0 = 'gsettings set '+key+' "'+str(current)+'"'
cmd1 = 'gsettings set '+subkey1+new+" name '"+sys.argv[1]+"'"
cmd2 = 'gsettings set '+subkey1+new+" command '"+sys.argv[2]+"'"
cmd3 = 'gsettings set '+subkey1+new+" binding '"+sys.argv[3]+"'"

import time

# gsettings/dconf may not be fully up yet this early in an autostart-
# triggered login (same class of race hit with Kerberos/network-drives
# elsewhere in this project) -- a call can silently fail with nothing
# printed anywhere, and that one shortcut just never gets registered.
# Retry with backoff and actually report a final failure instead of
# swallowing it, so a login-log grep for "failed" would catch it.
for cmd in [cmd0, cmd1, cmd2, cmd3]:
    for attempt in range(5):
        if subprocess.call(["/bin/bash", "-c", cmd]) == 0:
            break
        time.sleep(1)
    else:
        print(f"set_gnome_keyboard_shortcut: failed after 5 attempts: {cmd}", file=sys.stderr)

