- name: copy cron job
  copy:
    src: "{{ role_path }}/templates/cron/csc-mirror"
    dest: /etc/cron.d/csc-mirror
    owner: root
    group: root
    mode: "0644"

- name: restart and enable cron
  systemd:
    name: cron
    state: restarted
    enabled: yes

- name: copy files for /home/mirror
  copy:
    src: "{{ role_path }}/templates/mirror/"
    dest: /home/mirror
    owner: mirror
    group: mirror
    mode: preserve

- name: copy files for /mirror/root
  copy: 
    src: "{{ role_path }}/templates/root/root/"
    dest: /mirror/root
    owner: root
    group: csc-mirror
    mode: preserve

- name: create merlin subdirectories
  file:
    path: "/mirror/merlin/{{ item.file }}"
    owner: "{{ item.owner }}"
    group: "{{ item.owner }}"
    mode: "0755"
    state: directory
  loop:
    - { file: dev, owner: root }
    - { file: bin, owner: root }
    - { file: run, owner: mirror }

- name: copy arthur and busybox into merlin
  copy:
    src: "{{ role_path }}/templates/root/merlin/{{ item.file }}"
    dest: "/mirror/merlin/{{ item.dest }}"
    owner: root
    group: root
    mode: "0755"
  loop:
    - { file: "arthur",   dest: "arthur" }
    - { file: "busybox",  dest: "bin/busybox" }

- name: create symlinks from busybox and arthur
  # noqa deprecated-command-syntax
  # need chdir to create relative symlinks
  command: 
    cmd: "ln -s {{ item.src }} {{ item.dest }}"
    chdir: "/mirror/merlin/bin"
    creates: "/mirror/merlin/bin/{{ item.dest }}"
    warn: no
  loop:
    - { src: busybox,   dest: cd }
    - { src: busybox,   dest: cp }
    - { src: busybox,   dest: ln }
    - { src: busybox,   dest: ls }
    - { src: busybox,   dest: rm }
    - { src: busybox,   dest: sh }
    - { src: busybox,   dest: sleep }
    - { src: ../arthur, dest: arthur }

  # https://tldp.org/LDP/lfs/LFS-BOOK-6.1.1-HTML/chapter06/devices.html
- name: create special files
  # /dev/(random|urandom) normally has "-m 0444"
  # donno why mirror has writable random and urandom
  command:
    cmd: "mknod -m 0666 /mirror/merlin/dev/{{ item.file }} c 1 {{ item.type }}"
    creates: "/mirror/merlin/dev/{{ item.file }}"
  loop:
    - { type: "3", file: "null" }
    - { type: "8", file: "random" }
    - { type: "9", file: "urandom" }

- name: create first merlin.sock symlink
  # noqa deprecated-command-syntax
  # need chdir to create relative symlinks
  command: 
    cmd: "ln -s run/merlin.sock merlin.sock"
    chdir: "/mirror/merlin"
    creates: "/mirror/merlin/merlin.sock"
    warn: no

- name: create second merlin.sock symlink
  file:
    src: "/mirror/merlin/merlin.sock"
    dest: "/home/mirror/merlin/merlin.sock"
    owner: root
    group: root
    state: link
    follow: no
    force: yes

- name: copy merlin.service
  copy:
    src: "{{ role_path }}/templates/mirror/merlin/merlin.service"
    dest: "/etc/systemd/system/merlin.service"
    owner: root
    group: root
    mode: "0644"

# - name: start merlin.service
#   # merlin is disabled on mirror
#   systemd:
#     name: merlin
#     state: started
#     # enabled: yes
