---
- name: Create hawser config dir
  file:
    path: /etc/hawser
    state: directory
    owner: root
    group: root
    mode: '0700'

- name: Check if token file exists
  ansible.builtin.stat:
    path: /etc/hawser/token
  register: token_file

- name: Deploy hawser config
  when: not token_file.stat.exists
  block:
    - name: Generate token
      ansible.builtin.command: openssl rand -hex 32
      register: hawser_token
      changed_when: false
    - name: Save token
      copy:
        content: "{{ hawser_token.stdout }}"
        dest: /etc/hawser/token
        mode: '0600'

    - name: Save config
      template:
        src: templates/hawser-config
        dest: /etc/hawser/config
        owner: root
        group: root
        mode: '0600'
  
- name: Install hawser binary
  copy:
    src: bin/hawser
    dest: /usr/local/bin/hawser
    mode: '0755'

- name: Setup Hawser service
  template:
    src: bin/hawser.service
    dest: /etc/systemd/system/hawser.service
    mode: '0644'

- name: Enable Hawser
  ansible.builtin.systemd:
    name: hawser
    state: started
    enabled: yes

