# Buggy code
def keyboard_detect():
    global internalid, usbid, chromeswap, system_type
    internal_kbname = ""
    usb_kbname = ""
    print()
    print("Looking for keyboards...")
    print()
    result = subprocess.check_output(
        'xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | grep -o -P "(?<=↳).*(?=id\=)";exit 0',
        shell=True,
    ).decode("utf-8")
    if result != "":
        internal_kbname = result.strip()
    internalid = subprocess.check_output(
        'xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | cut -d "=" -f 2- | awk \'{print $1}\' | tail -1;exit 0',
        shell=True,
    ).decode("utf-8")
    print("Internal Keyboard\nName: " + internal_kbname + "\nID: " + internalid)

    result = subprocess.check_output(
        'udevadm info -e | grep -o -P "(?<=by-id/usb-).*(?=-event-kbd)" | head -1;exit 0',
        shell=True,
    ).decode("utf-8")
    if result != "":
        usb_kbname = result.strip()

    # Loop the following to ensure the id is picked up after 5-10 tries
    usbid = ""
    usbcount = 0
    while usbid == "":
        usbid = subprocess.check_output(
            'udevadm info -e | stdbuf -oL grep -o -P "(?<=event-kbd /dev/input/by-path/pci-0000:00:).*(?=.0-usb)";exit 0',
            shell=True,
        ).decode("utf-8")
        if usbid == "":
            usbcount += 1
            # print('usbid not found '+ str(usbcount))
            if usbcount == 5:
                usbid = "0"
        time.sleep(1)
    print("\nUSB Keyboard\n" + "Name: " + usb_kbname + "\nID: " + usbid)

    if system_type == "1":
        system_type = "windows"
    elif system_type == "2":
        system_type = "chromebook"
    elif system_type == "3":
        system_type = "mac"

    if system_type == "windows" or system_type == "mac":
        subprocess.check_output("/bin/bash -c ./mac_wordwise.sh", shell=True).decode(
            "utf-8"
        )
        cmdgui = '"/usr/bin/setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.mac.onelvl $DISPLAY"'
        # subprocess.check_output('echo "1" > /sys/module/hid_apple/parameters/swap_opt_cmd', shell=True).decode('utf-8')
    elif system_type == "chromebook":
        subprocess.check_output("/bin/bash -c ./chromebook.sh", shell=True).decode(
            "utf-8"
        )
        cmdgui = '"setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.chromebook.gui $DISPLAY"'

    # password = getpass("Please enter your password to complete the keyswap: ")
    # proc = Popen("echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd".split(), stdin=PIPE, stdout=PIPE, stderr=PIPE)
    # proc.communicate(password.encode())

    if swap_behavior == 1:
        print("Setting up " + system_type + " keyswap as a service.")
        print("You can disable and remove the service by using the following commands.")
        print("systemctl --user stop keyswap")
        print("systemctl --user disable keyswap")
        print("rm -rf ~/.config/autostart/keyswap.sh")
        print("rm -rf ~/.config/xactive.sh")
        keyswapcmd = (
            '/bin/bash -c "./keyswap_service.sh 1 0 '
            + system_type
            + " "
            + str(internalid).strip()
            + " "
            + str(usbid).strip()
            + " "
            + str(chromeswap)
            + '"'
        )
        print(keyswapcmd)
        subprocess.check_output(keyswapcmd, shell=True).decode("utf-8")
    else:
        print(
            "Setting up "
            + system_type
            + " keyswap inside your profiles ~/.Xsession file."
        )
        print(
            "You can modify or remove the file if you want you want to remove the modification."
        )
        keyswapcmd = '/bin/bash -c "./keyswap_service.sh 0 ' + cmdgui + '"'
        subprocess.check_output(keyswapcmd, shell=True).decode("utf-8")

    print(
        "Please run this command in the terminal if you are using a Windows or Macbook."
    )
    print("Your keymapping will not work right on Apple keyboards without it.")
    print("echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd")


# Traceback (subprocess.CalledProcessError)
K!nt◎
- F!x the dɑmn kɐyb◎ɑrd. -

Press Enter to begin...

What type of system are you using?
1) Windows
2) Chromebook
3) Mac
3

Would you like to swap Command back to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)
Note: For a more mac like experience &amp; less issues with terminal based interactions y is recommended.
y

Looking for keyboards...

Internal Keyboard
Name: AT Translated Set 2 keyboard
ID: 17


USB Keyboard
Name: Microsoft_Microsoft_Nano_Transceiver_1.1
ID: 14
14
14

Setting up mac keyswap as a service.
You can disable and remove the service by using the following commands.
systemctl --user stop keyswap
systemctl --user disable keyswap
rm -rf ~/.config/autostart/keyswap.sh
rm -rf ~/.config/xactive.sh
/bin/bash -c "./keyswap_service.sh 1 0 mac 17 14
14
14 0"
+ swapbehavior=1
+ noswapcmd=0
+ systemtype=mac
+ internalid=17
+ usbid=14
+ chromeswap=
+ [[ 1 == \1 ]]
++ whoami
+ swapcmd='\/bin\/bash\ \/home\/bdu\/.config\/xactive.sh\ mac\ 17\ 14\ '
+ mkdir -p /home/bdu/.config/systemd/user
+ cp ./system-config/keyswap.service /home/bdu/.config/systemd/user/keyswap.service
+ cp ./system-config/keyswap.sh /home/bdu/.config/autostart/keyswap.sh
+ cp ./system-config/xactive.sh /home/bdu/.config/xactive.sh
++ whoami
+ sed -i 's/{username}/bdu/g' /home/bdu/.config/systemd/user/keyswap.service
+ sed -i 's/ExecStart=/ExecStart=\/bin\/bash\ \/home\/bdu\/.config\/xactive.sh\ mac\ 17\ 14\ /g' /home/bdu/.config/systemd/user/keyswap.service
+ systemctl --user enable keyswap
+ systemctl --user start keyswap
/bin/bash: line 1: 14: command not found
/bin/bash: line 2: 14: command not found
Traceback (most recent call last):
File "./install.py", line 162, in <module>
keyboard_detect()
File "./install.py", line 82, in keyboard_detect
subprocess.check_output(keyswapcmd, shell=True).decode('utf-8')
File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
**kwargs).stdout
File "/usr/lib/python3.6/subprocess.py", line 438, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '/bin/bash -c "./keyswap_service.sh 1 0 mac 17 14
14
14 0"' returned non-zero exit status 127.

# Fixed code
def keyboard_detect():
    global internalid, usbid, chromeswap, system_type
    internal_kbname = ""
    usb_kbname = ""

    # If chromebook
    if system_type == "2":
        print()
        print("Looking for keyboards...")
        print()
        result = subprocess.check_output(
            'xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | grep -o -P "(?<=↳).*(?=id\=)";exit 0',
            shell=True,
        ).decode("utf-8")
        if result != "":
            internal_kbname = result.strip()
        internalid = subprocess.check_output(
            'xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | cut -d "=" -f 2- | awk \'{print $1}\' | tail -1;exit 0',
            shell=True,
        ).decode("utf-8")
        print("Internal Keyboard\nName: " + internal_kbname + "\nID: " + internalid)

        result = subprocess.check_output(
            'udevadm info -e | grep -o -P "(?<=by-id/usb-).*(?=-event-kbd)" | head -1;exit 0',
            shell=True,
        ).decode("utf-8")
        if result != "":
            usb_kbname = result.strip()

        # Loop the following to ensure the id is picked up after 5-10 tries
        usbid = ""
        usbcount = 0
        while usbid == "":
            usbid = subprocess.check_output(
                'udevadm info -e | stdbuf -oL grep -o -P "(?<=event-kbd /dev/input/by-path/pci-0000:00:).*(?=.0-usb) | head -n 1";exit 0',
                shell=True,
            ).decode("utf-8")
            if usbid == "":
                usbcount += 1
                # print('usbid not found '+ str(usbcount))
                if usbcount == 5:
                    usbid = "0"
            time.sleep(1)
        print("\nUSB Keyboard\n" + "Name: " + usb_kbname + "\nID: " + usbid)

    if system_type == "1":
        system_type = "windows"
    elif system_type == "2":
        system_type = "chromebook"
    elif system_type == "3":
        system_type = "mac"

    if system_type == "windows" or system_type == "mac":
        subprocess.check_output("/bin/bash -c ./mac_wordwise.sh", shell=True).decode(
            "utf-8"
        )
        cmdgui = '"/usr/bin/setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.mac.onelvl $DISPLAY"'
        # subprocess.check_output('echo "1" > /sys/module/hid_apple/parameters/swap_opt_cmd', shell=True).decode('utf-8')
    elif system_type == "chromebook":
        subprocess.check_output("/bin/bash -c ./chromebook.sh", shell=True).decode(
            "utf-8"
        )
        cmdgui = '"setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.chromebook.gui $DISPLAY"'

    # password = getpass("Please enter your password to complete the keyswap: ")
    # proc = Popen("echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd".split(), stdin=PIPE, stdout=PIPE, stderr=PIPE)
    # proc.communicate(password.encode())

    if swap_behavior == 1:
        print("Setting up " + system_type + " keyswap as a service.")
        print("You can disable and remove the service by using the following commands.")
        print("systemctl --user stop keyswap")
        print("systemctl --user disable keyswap")
        print("rm -rf ~/.config/autostart/keyswap.sh")
        print("rm -rf ~/.config/xactive.sh")
        keyswapcmd = (
            '/bin/bash -c "./keyswap_service.sh 1 0 '
            + system_type
            + " "
            + str(internalid).strip()
            + " "
            + str(usbid).strip()
            + " "
            + str(chromeswap)
            + '"'
        )
        print(keyswapcmd)
        subprocess.check_output(keyswapcmd, shell=True).decode("utf-8")
    else:
        print(
            "Setting up "
            + system_type
            + " keyswap inside your profiles ~/.Xsession file."
        )
        print(
            "You can modify or remove the file if you want you want to remove the modification."
        )
        keyswapcmd = '/bin/bash -c "./keyswap_service.sh 0 ' + cmdgui + '"'
        subprocess.check_output(keyswapcmd, shell=True).decode("utf-8")

    print(
        "Please run this command in the terminal if you are using a Windows or Macbook."
    )
    print("Your keymapping will not work right on Apple keyboards without it.")
    print("echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd")