#!/usr/bin/env bash

sudo apt-get update -qq
sudo apt-get install -y -qq kanata

# Kanata Configuration
# https://jtroo.github.io/config.html

# /dev/input/by-path/platform-i8042-serio-0-event-kbd

# The keyboard-layout-editor now has export to VIA functionality but you must
# add matrix properties before export.
# See /space/code_repositories/hardware/keyboards/zmk-related/dell_D830/Dell_latitude_D620_keyboard_matrix.ods.
# and /space/code_repositories/hardware/keyboards/zmk-related/dell_D830/Dell Latitude D630 keyboard DPN 0DR160.ods
#
# https://github.com/konpuku/vil2kanata features a vil2kanata converter.
# See /space/code_repositories/keymaps/vil2kanata/README_eng.md
# $ node vil2kanata.js my_keyboard.vil --output my_keyboard.kbd

# Dell D830 link to keyboard-layout-editor
# https://editor.keyboard-tools.xyz/#share=NobwRAhgrgLgFgewE5gFxgMYE8YFNcB2YANGAEYQYDWGCANsmmAMQCMurZnJYBEAtriYARXHToACYQA4AzAAYJ8gHIAFAKwB2aTwII8AZyYAVOLglVcWMgghIAJhIBmCKAUcBLAhPjn7YyWEJA1wkD1wDCQQnCToIAAcYBHiDAB0CYQA2VkUAWik5RQh3CQBZABYFeQA6HgwDI3QAegAqdNMvCRz5dIAZDwBzOBgJKvSAJVwBqDikCXL5HoJS3HsPKH4JdUX0gCF6R00dgl246gkATmOO726JAEkYCDoPDD7B4dHFh6eXt4JJtNZvNvo9nq90is1hstqDfhCTgcJEdFGC-nszlRLnDwf8Wk10ukAAIuAgwXJOSjmEDpCTOBBkikCDx0LCoCQAcnGCBsSQ5AG5afTGQYYFg6Lh2R54RhBd5heSAO64D4wdndOV0gxIDDsqBIOgACmGMBSqCaTVJMAM1QGoogMFe1Vo-CaBiaSB5+gQTQAbrIAExNADSADEAPIeADCBAAjgBRKAXAOhpCsUpQdSsACKSHjAC8AIL2XqK6qK6JOAMASnpSH4DsNHIrTirHOrcoAvoSCCSGeTKRhqUKrUz+Cy2ZzubyEAKR-3cqLxZKJNLcZqFbllar2VUN9rdRJ9UaTWaLVabXano6MM6EK73Z6Z37AyGIw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  /dev/input/by-path/platform-i8042-serio-0-event-kbd

)

(defsrc
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [    ]    
  caps a    s    d    f    g    h    j    k    l    ;    '    ret
  lsft \ z    x    c    v    b    n    m    ,    .    /    rsft
  lctl lmet lalt           spc            ralt rmet rctl
)


(defsrc
;;      vold volu mute                        gui  kprt nlck pause   ins  home pgup
esc  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12     del  end  pgdn
  grv  1    2    3    4    5    6    7    8    9    0    -     =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [     ]
  caps a    s    d    f    g    h    j    k    l    ;    '     \    ret
  lsft \    z    x    c    v    b    n    m    ,    .    /               rsft
  lctl fn   lmet lalt           spc            ralt  rctl           u
                                                              left down rght
)

(deflayer qwerty
  grv  _    _    _    _    _    _    _    _    _    _    _    _    _
  _  _    @warrows    _    _    _    _    _    _    _    _    _    _    
  lctrl     @alctrl    @slsft    @dlalt    @flmet    _    _    @jrmet    @kralt    @lrsft    @;rctrl    _ _
  _ _ _    _    _    _    _    _    _    _    _    _    _
  _ _ _           @smartspace            _ _ _
)

(deflayer arrows
  _  _    _    _    _    _    _    _    _    _    _    _    _    _
  _  _    _    _    _    _    _    _    _    _    _    _    _    
  _     _    _    _    @flmet    _    left down up rght _ _ _
  _ _ _    _    _    _    _    _    _    _    _    _    _
  _ _ _           @smartspace            _ _ _
)

(deflayer colemak
  grv XX    XX    XX    XX    XX    XX    XX    XX    XX    XX    XX    XX    _
  tab         q       w       f       p       b       j       l       u       y       ;       [       ]    
  lctrl   @alctrl @rlsft      @slalt      @tlmet      g       m       @nrmet      @eralt      @irsft      @orctrl      '       ret
  lsft    XX z       x       c       d       v     k       h       ,       .       /       rsft
  XX XX XX           @smartspace            XX XX XX
)

(deflayer magic
  _  @clmk    @qwerty    _    _    _    _    _    _    _    _    _    _    _
  _  _    _    _    _    _    _    _    _    _    _    _    _    
  _     tab    A-tab    _    _    _    _    bspc  esc   _ ret    _  _
  _ _ _    _    _    _    _    _    _    _    _    _    _
  _ _ _           _            _ _ _
)

(defalias
  warrows (tap-hold 200 200 w (layer-toggle arrows))

  alctrl (tap-hold 200 200 a lctrl)
  slsft (tap-hold 200 200 s lsft)
  dlalt (tap-hold 200 200 d lalt)
  flmet (tap-hold 200 200 f lmet)

  jrmet (tap-hold 200 200 j rmet)
  kralt (tap-hold 200 200 k ralt)
  lrsft (tap-hold 200 200 l rsft)
  ;rctrl (tap-hold 200 200 ; rctrl)

  rlsft (tap-hold 200 200 r lsft)
  slalt (tap-hold 200 200 s lalt)
  tlmet (tap-hold 200 200 t lmet)
  nrmet (tap-hold 200 200 n rmet)
  eralt (tap-hold 200 200 e ralt)
  irsft (tap-hold 200 200 i rsft)
  orctrl (tap-hold 200 200 o rctrl)
  clmk (layer-switch colemak)
  qwerty (layer-switch qwerty)

  smartspace (tap-dance 200 (
      (tap-hold 300 300 spc (layer-toggle magic))
      (tap-hold 300 300 (one-shot 300 lalt) spc)
      a
      ))

  )
EOF
}
conf_print_kanata_kbd | tee ${DEST}/.config/kanata/kanata.kbd
