---
- name: Create directories
  file:
    path: '~/.local/{{ item }}'
    state: directory
    mode: 0755
  loop:
    - bin
    - libexec

- name: Synchronize bin
  synchronize:
    src: bin/
    dest: ~/.local/bin/

- name: Synchronize libexec
  synchronize:
    src: libexec/
    dest: ~/.local/libexec/
    recursive: yes
    delete: no

- name: Copy templates to ~/.local/libexec/wayland
  template:
    src: '{{ item }}.j2'
    dest: '~/.local/libexec/wayland/{{ item }}'
    force: yes
    mode: 0755
  loop:
    - brightness
    - bukumenu
    - clipboard
    - dismiss_notify
    - launch_notify
    - mangamenu
    - musicmenu
    - screenshot
    - searchmenu
    - sway_touchpad
    - volumecontrol

- name: Copy templates to ~/.local/libexec
  template:
    src: genfontimage.j2
    dest: ~/.local/libexec/genfontimage
    force: yes
    mode: 0755

# The readlinkf function was moved to an external file
# https://github.com/monesonn/doasedit/commit/40d5b8c92cc3e533ddadbda0464a89450534360a
- name: Install doasedit + readlinkf_posix
  tags: [ curl, doasedit ]
  get_url:
    url: 'https://github.com/monesonn/doasedit/raw/main/{{ item }}'
    dest: '~/.local/bin/{{ item }}'
    mode: 0755
    force: yes
  loop:
    - doasedit
    - readlinkf_posix

- name: Install tmpsms
  tags: [ curl, tmpsms ]
  get_url:
    url: https://github.com/sdushantha/tmpsms/raw/master/tmpsms
    dest: ~/.local/bin/tmpsms
    mode: 0755
    force: yes

- name: Install tmpmail
  tags: [ curl, tmpmail ]
  get_url:
    url: https://github.com/sdushantha/tmpmail/raw/master/tmpmail
    dest: ~/.local/bin/tmpmail
    mode: 0755
    force: yes

- name: Install ani-cli
  tags: [ curl, ani-cli ]
  get_url:
    url: https://github.com/pystardust/ani-cli/raw/master/ani-cli
    dest: ~/.local/bin/ani-cli
    mode: 0755
    force: yes

- name: Install ytfzf
  tags: [ curl, ytfzf ]
  get_url:
    url: https://github.com/pystardust/ytfzf/raw/master/ytfzf
    dest: ~/.local/bin/ytfzf
    mode: 0755
    force: yes

- name: Install tuxi
  tags: [ curl, tuxi ]
  get_url:
    url: https://raw.githubusercontent.com/Bugswriter/tuxi/main/tuxi
    dest: ~/.local/bin/tuxi
    mode: 0755
    force: yes

- name: Install git-filter-repo
  tags: [ curl, git-filter-repo ]
  get_url:
    url: https://github.com/newren/git-filter-repo/raw/main/git-filter-repo
    dest: ~/.local/bin/git-filter-repo
    mode: 0755
    force: yes

- name: Check whether the Lua binary is installed
  file:
    path: '{{ lua_binary }}'
    state: file
  check_mode: yes
  failed_when: no
  register: lua_binary_check

- name: Symlink lua binary
  file:
    src: '{{ lua_binary }}'
    dest: ~/.local/bin/lua
    state: link
  when: lua_binary_check.state == 'file'
