- name: Record (initial) disk space used
  shell: df -B1 --output=used / | tail -1
  register: df1


- name: 'Install package: awstats'
  package:
    name: awstats
    state: present

# - name: 'Install package: pwauth (when: apache_installed is defined)'
#   package:
#     name:
#       - pwauth    # Auto-installs apache2-bin and libapache2-mod-authnz-external
#       #- apache2-utils    # Unneeded?  (In any case, already installed by Apache itself.)
#       #- openssl    # Unneeded?  (In any case, already installed by most every Linux distro.)
#     state: present
#   when: apache_installed is defined
#
# - name: "Run 'a2enmod cgi' to enable cgi execution via Apache (when: apache_installed is defined)"
#   command: a2enmod cgi
#   when: apache_installed is defined
#
# - name: 'Create directory... mkdir {{ apache_log_dir }}, recursively chown {{ apache_user }}:{{ apache_user }}, with chmod u+rw,g+r,g-w,o-rwx (when: apache_installed is defined)'
#   file:
#     state: directory
#     recurse: yes
#     path: "{{ apache_log_dir }}"    # /var/log/apache2 on debuntu
#     owner: "{{ apache_user }}"      # www-data on debuntu
#     group: "{{ apache_user }}"
#     mode: u+rw,g+r,g-w,o-rwx    # '0750' turned on too many x bits
#     #force: yes
#   when: apache_installed is defined

- name: Create 2 directories... mkdir {{ awstats_data_dir }} (intermediate summary storage) and /usr/lib/cgi-bin/awstats, recursively chown {{ apache_user }}:{{ apache_user }}, with chmod u+rw,g+r,g-w,o-rwx
  file:
    state: directory
    recurse: yes
    path: "{{ item }}"
    owner: "{{ apache_user }}"    # USED FOR NGINX TOO: 'www-data' on debuntu
    group: "{{ apache_user }}"
    mode: u+rw,g+r,g-w,o-rwx    # '0750' turned on too many x bits
    #force: yes
  with_items:
    - "{{ awstats_data_dir }}"    # /library/awstats
    - /usr/lib/cgi-bin/awstats    # create backward compatible path for awstats

# - name: 'Install /etc/{{ apache_conf_dir }}/awstats.conf from template (when: apache_installed is defined)'
#   template:
#     src: apache-awstats.conf
#     dest: "/etc/{{ apache_conf_dir }}/awstats.conf"    # apache2/sites-available on debuntu
#   when: apache_installed is defined
#
# - name: "Install /etc/logrotate.d/apache2 from template, to ensure logrotate doesn't make logs unreadable (when: apache_installed is defined)"
#   template:
#     src: logrotate.d.apache2
#     dest: /etc/logrotate.d/apache2
#   when: apache_installed is defined

- name: Does /etc/awstats/awstats.conf exist?
  stat:
    path: /etc/awstats/awstats.conf
  register: awstats

- name: If so, move it aside to /etc/awstats/awstats.conf.dist
  command: mv /etc/awstats/awstats.conf /etc/awstats/awstats.conf.dist
  when: awstats.stat.islnk is defined and not awstats.stat.islnk

- name: Symlink /usr/lib/cgi-bin/awstats/awstats.pl -> /usr/lib/cgi-bin/awstats.pl so old Apache links to awstats will work after change to NGINX
  file:
    src: /usr/lib/cgi-bin/awstats.pl
    path: /usr/lib/cgi-bin/awstats/awstats.pl
    state: link

- name: Install /etc/awstats/awstats.schoolserver.conf from template
  template:
    src: awstats.schoolserver.conf.j2
    dest: /etc/awstats/awstats.schoolserver.conf

- name: Symlink /etc/awstats/awstats.conf -> /etc/awstats/awstats.schoolserver.conf for access by IP address
  file:
    src: /etc/awstats/awstats.schoolserver.conf
    path: /etc/awstats/awstats.conf
    state: link

# - name: On first enabling of AWStats, summarize httpd logs up to now (OS's other than debuntu)
#   shell: /bin/perl /usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=schoolserver -update
#   when: awstats_enabled and not is_debuntu

- name: "Summarize logs up to now: /usr/bin/perl /usr/lib/cgi-bin/awstats.pl -config=schoolserver -update"
  command: /usr/bin/perl /usr/lib/cgi-bin/awstats.pl -config=schoolserver -update

- name: Install /etc/nginx/cgi-bin.php from template
  template:
    src: cgi-bin.php
    dest: /etc/nginx/


# RECORD AWStats AS INSTALLED

- name: Record (final) disk space used
  shell: df -B1 --output=used / | tail -1
  register: df2

- name: Add 'awstats_disk_usage = {{ df2.stdout|int - df1.stdout|int }}' to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"    # /etc/iiab/iiab.ini
    section: awstats
    option: awstats_disk_usage
    value: "{{ df2.stdout|int - df1.stdout|int }}"

- name: "Set 'awstats_installed: True'"
  set_fact:
    awstats_installed: True

- name: "Add 'awstats_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^awstats_installed'
    line: 'awstats_installed: True'