---
- name: installs and setups dotfiles and packages
  hosts: localhost
  tasks:
    - name: test loops
      debug:
        msg: "{{ ansible_os_family }}"
    - name: install packages on a debian host
      apt:
        name: "{{ item }}"
        state: latest
      loop:
        - vim-nox
        - git
        - stow
        - curl
        - ranger
        - tmux
      when: ansible_os_family == "Debian"
      become: yes
      become_user: root

    - name: install packages on redhat host
      dnf:
        name:
          - vim-enhanced
          - git
          - stow
          - curl
          - ranger
          - tmux
        state: latest
      when: ansible_os_family == "RedHat"
      become: yes
      become_user: root

    - name: copy old bash configs to ...bak
      copy:
        src: "/home/{{ansible_user_id}}/{{item}}"
        dest: "/home/{{ansible_user_id}}/{{item}}.bak"
      loop:
        - ".bashrc"
      when: ansible_user_id != "root"

    - name: delete old configs
      file:
        path: "/home/{{ansible_user_id}}/{{item}}"
        state: absent
      loop:
        - ".bashrc"
        - ".bash_profile"
      when: ansible_user_id != "root"

    - name: link the dotfiles to users home directory
      command:
        cmd: stow "{{ item }}"
      loop:
        - bash
        - git
        - ranger
        - shellenv
        - tmux
        - vim
