- name: Add dir {{ doc_root }}/local_content, where USB drive links can appear
  file:
    path: "{{ doc_root }}/local_content"
    state: directory
    owner: "{{ apache_user }}"
    group: "{{ iiab_admin_user }}"    # ISN'T "{{ apache_user }}" MORE APPROPRIATE?
    mode: 0775

- name: Install /etc/usbmount/mount.d/70-usb-library from template
  template:
    src: mount.d/70-usb-library
    dest: /etc/usbmount/mount.d/
    owner: root
    group: root
    mode: 0751
  when: usb_lib_enabled

- name: 'Install from template: /etc/udev/rules.d/usbmount.rules, /etc/systemd/system/usbmount@.service, /usr/bin/iiab-usb-lib-show-all-on, /usr/bin/iiab-usb-lib-show-all-off'
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "{{ item.mode }}"
  with_items:
    - { src: 'usbmount@.service.j2' , dest: '/etc/systemd/system/usbmount@.service', mode: '0644' }
    - { src: 'usbmount.rules.j2' , dest: '/etc/udev/rules.d/usbmount.rules', mode: '0644' }
    - { src: 'iiab-usb-lib-show-all-on' , dest: '/usr/bin/', mode: '0755' }
    - { src: 'iiab-usb-lib-show-all-off' , dest: '/usr/bin/', mode: '0755' }

- name: Enable exFAT and NTFS in /etc/usbmount/usbmount.conf
  lineinfile: 
    regexp: '^FILESYSTEMS.*'
    line: 'FILESYSTEMS="vfat ext2 ext3 ext4 hfsplus exfat fuseblk ntfs"'
    path: /etc/usbmount/usbmount.conf

- name: Install /etc/usbmount/umount.d/70-usb-library from template
  template:
    src: umount.d/70-usb-library
    dest: /etc/usbmount/umount.d
    owner: root
    group: root
    mode: 0751
  when: usb_lib_enabled

- name: Remove /etc/usbmount/mount.d/70-usb-library if not usb_lib_enabled
  file:
    path: /etc/usbmount/mount.d/70-usb-library
    state: absent
  when: not usb_lib_enabled

- name: Remove /etc/usbmount/umount.d/70-usb-library if not usb_lib_enabled
  file:
    path: /etc/usbmount/umount.d/70-usb-library
    state: absent
  when: not usb_lib_enabled

- name: Put variable in iiab.env that enables display of content at root of USB
  lineinfile:
    path: "{{ iiab_env_file }}"
    regexp: "^IIAB_USB_LIB_SHOW_ALL.*"
    line: "IIAB_USB_LIB_SHOW_ALL={{ iiab_usb_lib_show_all }}"

- name: Install /etc/{{ apache_config_dir }}/content_dir.conf from template
  template:
    src: content_dir.conf
    dest: "/etc/{{ apache_config_dir }}"
  when: usb_lib_enabled

- name: Create symlink content_dir.conf from sites-enabled to sites-available (debuntu)
  file:
    src: "/etc/{{ apache_config_dir }}/content_dir.conf"
    dest: /etc/apache2/sites-enabled/content_dir.conf
    state: link
  when: is_debuntu

- name: Remove symlink content_dir.conf from /etc/apache2/sites-enabled (debuntu)
  file:
    dest: /etc/apache2/sites-enabled/content_dir.conf
    state: absent
  when: is_debuntu and not usb_lib_enabled

- name: Remove content_dir.conf from /etc/{{ apache_config_dir }}
  file:
    name: "/etc/{{ apache_config_dir }}/content_dir.conf"
    state: absent
  when: not usb_lib_enabled

- name: Add 'usb-lib' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: usb-lib
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: usb-lib
    - option: description
      value: '"usb-lib automounts Teacher Content on USB drives to /library/www/html/local_content, so students can browse it almost immediately at http://box/usb"'
    - option: enabled
      value: "{{ usb_lib_enabled }}"
