---

- block:
  - name: Set linuxptp_prefix for legacy compute
    set_fact:
      linuxptp_prefix: legacy
    when: zoox_compute_version is version('3.2', '<=')

  - name: Set linuxptp_prefix for C3.5
    set_fact:
      linuxptp_prefix: c3.5
    when: zoox_compute_version is version('3.5', '==')

  - name: Set linuxptp_prefix for C3.6 (VH6B)
    set_fact:
      linuxptp_prefix: c3.6/vh6b
    when: zoox_compute_version is version('3.6', '>=') and time_source == "EMU"

  - name: Set linuxptp_prefix for C3.6 (C2RV4)
    set_fact:
      linuxptp_prefix: c3.6/c2rv4
    when: zoox_compute_version is version('3.6', '>=') and time_source == "GPS"

  - name: Configure linuxptp options
    copy:
      src: "{{ linuxptp_prefix }}/ptp"
      dest: "/etc/default/ptp"
      owner: root
      group: root
      mode: '644'

  - name: Configure ptp4l
    copy:
      src: "{{ linuxptp_prefix }}/ptp4l.conf"
      dest: "/etc/linuxptp/ptp4l.conf"
      owner: root
      group: root
      mode: '644'
    notify:
      - "Restart ptp4l"

  - name: Configure ptp4l daemon
    copy:
      src: "{{ linuxptp_prefix }}/ptp4l.init"
      dest: "/etc/init/ptp4l.conf"
      owner: root
      group: root
      mode: '644'
    notify:
      - "Restart ptp4l"

  - name: Configure ptp4l_conti
    block:
      - name: Copy ptp4l conti config
        copy:
          src: "{{ linuxptp_prefix }}/ptp4l_conti.conf"
          dest: "/etc/linuxptp/ptp4l_conti.conf"
          owner: root
          group: root
          mode: '644'
        when: zoox_compute_version is version('3.5', '<=')
        notify: "Restart ptp4l_conti"
      - name: Copy ptp4l conti daemon
        copy:
          src: "{{ linuxptp_prefix }}/ptp4l_conti.init"
          dest: "/etc/init/ptp4l_conti.conf"
          owner: root
          group: root
          mode: '644'
        when: zoox_compute_version is version('3.5', '<=')
        notify: "Restart ptp4l_conti"
      - name: Stat ptp4l conti config
        stat:
          path: "/etc/init/ptp4l_conti.conf"
        register: ptp4l_conti_stat
      - name: Stop ptp4l conti service for C3.6+
        service:
          name: "ptp4l_conti"
          state: stopped
        when: zoox_compute_version is version('3.6', '>=') and ptp4l_conti_stat.stat.exists
      - name: Remove ptp4l conti init file for C3.6+
        file:
          dest: "/etc/init/ptp4l_conti.conf"
          state: absent
        when: zoox_compute_version is version('3.6', '>=')

  - block:
    - name: Configure ptp4l_client
      copy:
        src: "{{ linuxptp_prefix }}/ptp4l_client.conf"
        dest: "/etc/linuxptp/ptp4l_client.conf"
        owner: root
        group: root
        mode: '644'
      notify:
        - "Restart ptp4l_client"
    - name: Configure ptp4l_client daemon
      copy:
        src: "{{ linuxptp_prefix }}/ptp4l_client.init"
        dest: "/etc/init/ptp4l_client.conf"
        owner: root
        group: root
        mode: '644'
      notify:
        - "Restart ptp4l_client"
    - name: Stat ptp4l server
      stat:
        path: "/etc/init/ptp4l_server.conf"
      register: ptp4l_server_stat
    - name: Stop ptp4l server when using EMU
      service:
        name: "ptp4l_server"
        state: stopped
      when: ptp4l_server_stat.stat.exists
    - name: Remove ptp4l server conf when using EMU
      file:
        dest: "/etc/linuxptp/ptp4l_server.conf"
        state: absent
    - name: Remove ptp4l server daemon when using EMU
      file:
        dest: "/etc/init/ptp4l_server.conf"
        state: absent
    when: zoox_compute_version is version('3.6', '>=') and time_source == "EMU"

  - block:
    - name: Configure ptp4l_server
      copy:
        src: "{{ linuxptp_prefix }}/ptp4l_server.conf"
        dest: "/etc/linuxptp/ptp4l_server.conf"
        owner: root
        group: root
        mode: '644'
      notify:
        - "Restart ptp4l_server"
    - name: Configure ptp4l_server daemon
      copy:
        src: "{{ linuxptp_prefix }}/ptp4l_server.init"
        dest: "/etc/init/ptp4l_server.conf"
        owner: root
        group: root
        mode: '644'
      notify:
        - "Restart ptp4l_server"
    - name: Stat ptp4l client
      stat:
        path: "/etc/init/ptp4l_client.conf"
      register: ptp4l_client_stat
    - name: Stop ptp4l client when using GPS
      service:
        name: "ptp4l_client"
        state: stopped
      when: ptp4l_client_stat.stat.exists
    - name: Remove ptp4l client conf when using GPS
      file:
        dest: "/etc/linuxptp/ptp4l_client.conf"
        state: absent
    - name: Remove ptp4l client daemon when using GPS
      file:
        dest: "/etc/init/ptp4l_client.conf"
        state: absent
    when: zoox_compute_version is version('3.6', '>=') and time_source == "GPS"

  - name: Configure phc2sys daemon
    block:
      - name: Configure phc2sys
        copy:
          src: "{{ linuxptp_prefix }}/phc2sys.init"
          dest: "/etc/init/phc2sys.conf"
          owner: root
          group: root
          mode: '644'
        notify:
          - "Restart phc2sys"
      - name: Configure phc2sys daemon
        copy:
          src: "{{ linuxptp_prefix }}/phc2sys.conf"
          dest: "/etc/linuxptp/phc2sys.conf"
          owner: root
          group: root
          mode: '644'
        notify:
          - "Restart phc2sys"

  become: yes
