- name: Copy file with owner and permissions
  hosts: "{{ all | default('all', true) }}"
  become: true
  gather_facts: true
  vars:
    permissions: "0755"
    owner: "root"
    setupfiles_root_dir: "{{ lookup('env', 'SETUPFILES_ROOT_DIR') }}"
    build_dir: "{{ lookup('env', 'BUILD_DIR') }}"
    home_dir: "{{ lookup('env', 'BUILD_DIR') }}/home"
    deb_files_dir: "{{ setupfiles_root_dir }}/deb"
    deb_files_pattern: "*{{ ansible_architecture }}.deb"
    users:
      - { name: "kriss", home: "/home/kriss" }
      - { name: "root", home: "/root" }
    dirs:
      # - {src: "{{ build_dir }}/bin/", dest: "/usr/local/setupfiles/bin/"}
      # - {src: "{{ build_dir }}/scripts/", dest: "/usr/local/setupfiles/scripts/"}
      - {src: "{{ build_dir }}/usr", dest: "/usr"}
      # - {src: "{{ build_dir }}/cron.d/", dest: "/etc/cron.d/"}
      # EDIT: ssh dir is now in {{ build_dir }}/etc/ssh
      - {src: "{{ build_dir }}/etc", dest: "/etc"}
      # - {src: "{{ build_dir }}/ssh/sshd_config.d/", dest: "/etc/ssh/sshd_config.d/"}
      # - {src: "{{ build_dir }}/profile.d/", dest: "/etc/profile.d/"}
    templates:
      - {src: "{{ build_dir }}/etc/profile.d/sf00-ansible-vars.sh", dest: "/etc/profile.d/sf00-ansible-vars.sh"}
      - {src: "{{ build_dir }}/etc/profile.d/sf01-profile-env.sh", dest: "/etc/profile.d/sf01-profile-env.sh"}
      - {src: "{{ build_dir }}/etc/profile.d/sf02-profile.sh", dest: "/etc/profile.d/sf02-profile.sh"}
      # - {src: "{{ build_dir }}/ssh/banner", dest: "/etc/ssh/banner"}
  tasks:

    # NOTE: Debug: Print version info
    - name: "Print debug info"
      debug:
        msg: 
          - "Starting deployment of SSH"
          - "Kernel: {{ ansible_kernel }}"
          - "Architecture: {{ ansible_architecture }}"
          - "OS: {{ ansible_os_family }}"
          - "OS Version: {{ ansible_distribution_major_version }}"
          - "Ansible inventory host: {{ ansible_host }}"
          - "Incoming version: {{ semaphore_vars.task_details.incoming_version }}"

    # NOTE: Delete setupfiles root dir if it exists
    - name: Remove setupfiles root dir if exists
      ansible.builtin.file:
        path: "{{ setupfiles_root_dir }}"
        state: absent

    # NOTE: Re-create setupfiles root dir
    - name: Re-create setupfiles root dir
      ansible.builtin.file:
        path: "{{ setupfiles_root_dir }}"
        owner: "{{ owner }}"
        group: "{{ owner }}"
        mode: "{{ permissions }}"
        state: directory

    # NOTE: Write vars info to file
    - name: "Write vars info to file"
      ansible.builtin.copy:
        content: "{{ vars | to_nice_yaml }}"
        dest: "{{ setupfiles_root_dir }}/sf-info.yml"

    # NOTE: Ensure directories exist
    - name: Ensure directories exist
      ansible.builtin.file:
        path: "{{ item.dest }}"
        state: directory
        owner: "{{ owner }}"
        group: "{{ owner }}"
        mode: "{{ permissions }}"
      with_items: "{{ dirs }}"

    # NOTE: Copy over `dirs` from build to destination host
    - name: Copy over from build to destination host
      ansible.builtin.copy:
        src: "{{ item.src }}/"
        dest: "{{ item.dest }}/"
        owner: "{{ owner }}"
        group: "{{ owner }}"
        mode: "{{ permissions }}"
      with_items: "{{ dirs }}"

      # NOTE: Copy over templates from build to destination host
    - name: Template profile env from build to destination host
      ansible.builtin.template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: "{{ permissions }}"
      with_items: "{{ templates }}"

    # NOTE: Copy {{ home_dir }} to home directories of users
    - name: Copy home directory files to users' home directories
      ansible.builtin.copy:
        src: "{{ home_dir }}/"
        dest: "{{ item.home }}/"
        owner: "{{ item.name }}"
        group: "{{ item.name }}"
      with_items: "{{ users }}"

    # NOTE: Find applicable .deb files
    - name: Find applicable .deb files
      ansible.builtin.find:
        paths: "{{ deb_files_dir }}"
        patterns: "{{ deb_files_pattern }}"
        recurse: yes
      register: found_deb_files
      failed_when: False
    # NOTE: Install all *.deb files under $SETUPFILES_ROOT_DIR/deb
    - name: Install all .deb files
      ansible.builtin.apt:
        deb: "{{ item }}"
        state: present
      with_items: "{{ found_deb_files.files | map(attribute='path') | list }}"
      when: found_deb_files.matched > 0

    - name: Deployment complete
      debug:
        msg: "[OK] Deployment complete. Please check the logs for any errors."

    # # NOTE: Copy files
    # - name: Copy files
    #   ansible.builtin.copy:
    #     src: "{{ item.src }}"
    #     dest: "{{ item.dest }}"
    #     owner: "{{ owner }}"
    #     group: "{{ owner }}"
    #     mode: "{{ permissions }}"
    #   with_items: "{{ dirs }}"