---
- name: Set nvim/lvim
  hosts: localhost
  vars:
    uid: "{{ lookup('env','USER') }}"
    LV_BRANCH: release-1.3/neovim-0.9
    tabby_token: "{{ lookup('ansible.builtin.pipe','pass show tabby-lab.sunet.se/tabby-token') }}"
  connection: local
  become: false
  tasks:
    - name: Have nvim allready installed
      stat:
        path: /usr/local/bin/nvim
      register: have_nvim
    - name: Get neovim
      ansible.builtin.command:
        chdir: /tmp
        cmd: wget https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz
        creates: /tmp/nvim-linux64.tar.gz
      when: not have_nvim.stat.exists
    - name: Unpack neovim
      ansible.builtin.command:
        chdir: /tmp
        cmd: tar xf nvim-linux64.tar.gz
        creates: /tmp/nvim-linux64
      when: not have_nvim.stat.exists
    - name: Install neovim
      become: true
      ansible.builtin.command:
        chdir: /tmp/nvim-linux64
        cmd: cp -a bin/ lib/ share/  /usr/local/
        creates: [/usr/local/bin/nvim]
      when: not have_nvim.stat.exists
    - name: Install neovim man pages
      become: true
      ansible.builtin.command:
        chdir: /tmp/nvim-linux64
        cmd: cp -a man/  /usr/local/share/
        creates: [/usr/local/share/man/man1/nvim.1]
      when: not have_nvim.stat.exists
    - name: Remove neovim tar ball
      ansible.builtin.command:
        chdir: /tmp
        cmd: rm nvim-linux64.tar.gz
        removes: /tmp/nvim-linux64.tar.gz
      when: not have_nvim.stat.exists
    - name: Remove neovim install files
      ansible.builtin.command:
        chdir: /tmp
        cmd: rm -rf nvim-linux64/
        removes: /tmp/nvim-linux64
      when: not have_nvim.stat.exists
    - name: Get lunarvim
      ansible.builtin.command:
        chdir: /tmp
        cmd: wget https://raw.githubusercontent.com/LunarVim/LunarVim/release-1.3/neovim-0.9/utils/installer/install.sh
        creates: /tmp/install.sh
      when: not have_nvim.stat.exists
    - name: Install lunarvim
      ansible.builtin.command:
        cmd: bash /tmp/install.sh
        creates: /home/{{ uid }}/.local/bin/lvim
      when: not have_nvim.stat.exists
    - name: Remove lunarvim install script
      ansible.builtin.command:
        cmd: rm /tmp/install.sh
        removes: /tmp/install.sh
      when: not have_nvim.stat.exists
    - name: Make my lvim real vim
      become: true
      ansible.builtin.file:
        src: /home/{{ uid }}/.local/bin/lvim
        dest: /usr/local/bin/vim
        state: link
      when: not have_nvim.stat.exists
    - name: Install lunarvim config
      ansible.builtin.template:
        src: ../templates/config.lua.j2
        dest: /home/{{ uid }}/.config/lvim/config.lua
        owner: '{{ uid }}'
        group: '{{ uid }}'
        mode: '0644'
    - name: Install tabby config
      ansible.builtin.template:
        src: ../templates/config.toml.j2
        dest: /home/{{ uid }}/.tabby-client/agent/config.toml
        owner: '{{ uid }}'
        group: '{{ uid }}'
        mode: '0644'
    - name: Link up yapf
      ansible.builtin.file:
        src: /usr/bin/yapf3
        dest: /usr/local/bin/yapf
        state: link
      become: true
    - name: Install stylua
      ansible.builtin.command:
        cmd: cargo install stylua
        creates: /home/{{ uid }}/.cargo/bin/stylua
