--- 
- name: mf main.yml
  hosts: localhost
  gather_facts: True
  vars: 
    install_dir: "{{ ansible_env.HOME }}/.config/mf"
    wifi_ifname: "wlp170s0"

  tasks:
   - name: apt update cache
     apt: 
       update_cache: True
       cache_valid_time: 1800 #30m
     become: True

   - name: apt upgrade
     apt: 
       name: "*" 
       state: latest
       update_cache: True
       cache_valid_time: 1800 #30m
     become: True

   - name: apt install tools
     apt: 
       update_cache: True
       cache_valid_time: 1800 #30m
       state: latest
       pkg:
       # system
       - pciutils
       - gdisk
       - psmisc
       - unzip
       - strace
       # wm 
       - sway
       - grim
       - slurp
       - imv
       - wl-clipboard
       - pipewire-audio
       # cli
       - kitty
       - tmux
       - htop
       - vim
       - git
       - git-lfs
       - magic-wormhole
       - file
       - tree
       - shellcheck
       # network
       - curl
       - dnsutils
       - mtr-tiny
       - firmware-iwlwifi
       - wpasupplicant
       # browser
       - firefox-esr
       # yubikey
       - gnupg2 
       - gnupg-agent
       - scdaemon
       - pcscd
       - pinentry-gnome3
       # recording
       - obs-studio 
       - xdg-desktop-portal
       - xdg-desktop-portal-wlr
     become: True

   - name: home directory dirs
     file:
       path: "{{ ansible_env.HOME }}/{{ item }}"
       state: directory
     loop:
       - ".ssh"
       - ".config"
       - ".config/sway"
       - ".config/kitty"
       - ".local"
       - ".local/bin"
       - ".gnupg"
       - ".mozilla"
       - ".mozilla/firefox"
       - ".mozilla/firefox/{{ ansible_env.USER }}"
       - "git"

   - name: binary installs (archives)
     unarchive:
       src: "{{ item.src }}"
       dest: "{{ item.dest }}"
       creates: "{{ item.creates }}"
       extra_opts: "{{ item.extra_opts | default([]) }}"
       include: "{{ item.include | default([]) }}"
       remote_src: True
     loop: 
     - src: "https://github.com/charmbracelet/gum/releases/download/v0.12.0/gum_0.12.0_Linux_x86_64.tar.gz"
       dest: "{{ ansible_env.HOME }}/.local/bin"
       creates: "{{ ansible_env.HOME }}/.local/bin/gum"
       include: 
         - "gum"
     - src: "https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz"
       dest: "{{ ansible_env.HOME }}/.local"
       creates: "{{ ansible_env.HOME }}/.local/bin/nvim"
       extra_opts: 
         - "--strip-components=1"
     - src: "https://go.dev/dl/go1.22.0.linux-amd64.tar.gz"
       dest: "{{ ansible_env.HOME }}/.local/"
       creates: "{{ ansible_env.HOME }}/.local/go/bin/go"
     - src: "https://github.com/casey/just/releases/download/1.24.0/just-1.24.0-x86_64-unknown-linux-musl.tar.gz"
       dest: "{{ ansible_env.HOME }}/.local/bin"
       creates: "{{ ansible_env.HOME }}/.local/bin/just"

   - name: binary installs
     get_url:
       url: "{{ item.src }}"
       dest: "{{ item.dest }}"
     loop: 
     - src: "https://dl.min.io/client/mc/release/linux-amd64/mc"
       dest: "{{ ansible_env.HOME }}/.local/bin/mc"

   - name: binary installs - chmod +x
     file:
       path: "{{ item.dest }}"
       state: touch
       mode: a+x
     loop: 
     - dest: "{{ ansible_env.HOME }}/.local/bin/mc"

   - name: install nvm
     shell: >
       curl -o - https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
     environment:
       PROFILE: "/dev/null"
     args:
       executable: "/bin/bash"
       chdir: "{{ ansible_env.HOME }}"
       creates: "{{ ansible_env.HOME }}/.nvm/nvm.sh"

   - name: install nvm
     shell: >
       . {{ ansible_env.HOME }}/.nvm/nvm.sh && nvm install --lts && nvm use --lts
     args:
       executable: "/bin/bash"
       chdir: "{{ ansible_env.HOME }}"
       creates: "{{ ansible_env.HOME }}.nvm/versions/"

   - name: git repos
     git:
       repo: "https://github.com/nvim-lua/kickstart.nvim"
       dest: "{{ ansible_env.HOME }}/.config/nvim"
     ignore_errors: True # local modifcations


   - name: home directory symlinks
     file:
       src: "{{ install_dir }}/files/{{ item.src }}"
       dest: "{{ ansible_env.HOME }}/{{ item.dest }}"
       force: True
       state: "link"
     loop:
      - src: "tmux.conf"
        dest: ".tmux.conf"
      - src: "ssh_config"
        dest: ".ssh/config"
      - src: "fonts"
        dest: ".fonts"
      - src: "gpg-agent.conf"
        dest: ".gnupg/gpg-agent.conf"
      - src: "sway_config"
        dest: ".config/sway/config"
      - src: "sway_status.sh"
        dest: ".config/sway/status.sh"
      - src: "kitty_kitty.conf"
        dest: ".config/kitty/kitty.conf"
      - src: "kitty_theme.conf"
        dest: ".config/kitty/theme.conf"
      - src: "bashrc"
        dest: ".bashrc"
      - src: "gitconfig"
        dest: ".gitconfig"
      - src: "firefox_profiles.ini"
        dest: ".mozilla/firefox/profiles.ini"
      - src: "firefox_user.js"
        dest: ".mozilla/firefox/{{ ansible_env.USER }}/user.js"
      - src: "nvim_kickstarter_init.lua"
        dest: ".config/nvim/init.lua"
      # all bashrc_d files
      - src: "bashrc_d"
        dest: ".bashrc.d"

   - name: wifi - config
     copy:
       src: "files/wpa_supplicant.conf"
       dest: "/etc/wpa_supplicant/wpa_supplicant-{{ wifi_ifname }}.conf"
     become: True

   - name: wifi - systemd
     systemd:
       name: "wpa_supplicant@{{ wifi_ifname }}.service"
       enabled: True
     become: True

   - name: alternatives - editor
     alternatives:
       name: editor
       link: /usr/bin/editor
       path: /usr/bin/vim.basic
     become: True

       #- name: reboot

