- name: Set user home directory
  ansible.builtin.set_fact:
    user_home: "{{ ansible_env.HOME if ansible_user_id == 'root' else lookup('env', 'HOME') }}"

- name: Create basic symlinks
  ansible.builtin.file:
    src: "{{ item.src | replace('~', user_home) }}"
    dest: "{{ item.dest | replace('~', user_home) }}"
    state: link
    force: true
    follow: false
  loop:
    - { src: "$DOTFILES_PATH/config/home-manager", dest: "~/.config/home-manager" }
    - { src: "$DOTFILES_PATH/vscode/settings.json", dest: "~/.config/Code/User/settings.json" }
    - { src: "$DOTFILES_PATH/config/ssh/config", dest: "~/.ssh/config" }
    - { src: "$DOTFILES_PATH/config/ssh/config.d", dest: "~/.ssh/config.d" }
    - { src: "$DOTFILES_PATH/config/starship.toml", dest: "~/.config/starship.toml" }

- name: Create gitconfig symlink
  ansible.builtin.file:
    src: "{{ gitconfig_mapping[hostname] | replace('~', user_home) }}"
    dest: "{{ user_home }}/.gitconfig"
    state: link
    force: true
    follow: false
  vars:
    gitconfig_mapping:
      mennos-desktop: "$DOTFILES_PATH/config/git/gitconfig.linux"
      mennos-laptop: "$DOTFILES_PATH/config/git/gitconfig.linux"
      mennos-server: "$DOTFILES_PATH/config/git/gitconfig.mennos-server"
      mennos-vm: "$DOTFILES_PATH/config/git/gitconfig.mennos-server"
      mennos-hobbypc: "$DOTFILES_PATH/config/git/gitconfig.linux"

- name: Create SSH authorized_keys symlink
  ansible.builtin.file:
    src: "{{ authorized_keys_mapping[hostname] | replace('~', user_home) }}"
    dest: "{{ user_home }}/.ssh/authorized_keys"
    state: link
    force: true
    follow: false
  vars:
    authorized_keys_mapping:
      mennos-desktop: "$DOTFILES_PATH/config/ssh/authorized_keys/mennos-desktop"
      mennos-laptop: "$DOTFILES_PATH/config/ssh/authorized_keys/mennos-laptop"
      mennos-server: "$DOTFILES_PATH/config/ssh/authorized_keys/mennos-server"
      mennos-vm: "$DOTFILES_PATH/config/ssh/authorized_keys/mennos-server"
      mennos-hobbypc: "$DOTFILES_PATH/config/ssh/authorized_keys/mennos-hobbypc"
