# wordpress-configure-playbook.yml
# Ansible playbook that integrates WordPress (PHP-based) into an
# existing reverse-proxied Greenstone3 installation.
# This playbook does step (c):
#
#   (c) uses 'wp' to configure the WordPress files
#
# End result:
#   https://{{ gsdl3servlet_external.domain }}/greenstone3/wordpress

---
- name: Configure WordPress within existing Apache/Greenstone3
  hosts: localhost
  connection: local
  gather_facts: true

  vars_files:
    - vars/default-core.yml
    - vars/default-wordpress.yml

  vars:
    site_name: "{{ apache2_revproxy_config.config_root_name }}"
    apache2_main_config_file: "/etc/apache2/sites-enabled/{{ site_name }}.conf"
    apache2_wordpress_topup_file: "sites-enabled/{{ site_name }}-wordpress-topup.conf"   # set absolute path if not in ServerRoot
    gsdl3_web_dir: "{{ gsdl3srchome }}/web"
    wordpress_dir: "{{ gsdl3_web_dir }}/wordpress"
    apachectl_bin: /usr/sbin/apache2ctl

  tasks:

    - name: Run WordPress configure/setup script (non-interactive)
      #become: true
      #become_user: www-data
      ansible.builtin.command:
        argv:
          - ./_02-RUN-ANSIBLExtra-WORDPRESS_CONFIGURE.sh
          - --gsdl3srchome
          - "{{ gsdl3srchome }}"
          - --wordpress-url
          - "{{ wordpress_url }}"
          - --wordpress-www-root
          - "{{ wordpress_www_root }}"
          - --wordpress-db
          - "{{ wordpress.db }}"
          - --wordpress-db-prefix
          - "{{ wordpress.table_prefix }}"
          - --wordpress-user
          - "{{ wordpress_db.user }}"
          - --wordpress-user-pw
          - "{{ wordpress_db.user_pw }}"
          - --wordpress-host
          - "{{ wordpress_db.host }}"
          - --wp-sitename-title
          - "{{ wordpress_site.title }}"
          - --wp-sitename-url
          - "{{ wordpress_url }}"
          - --wp-sitename-admin-un
          - "{{ wordpress_site.admin_un }}"
          - --wp-sitename-admin-pw
          - "{{ wordpress_site.admin_pw }}"
          - --wp-sitename-admin-email
          - "{{ wordpress_site.admin_email }}"
          #- --force
        #args:
        #  creates: "{{ wordpress_www_root }}/wp-config.php"   # skip if already configured
      register: wp_run


    - name: Show script output
      ansible.builtin.debug:
        msg: |
          rc={{ wp_run.rc }}
          stdout:
          {{ wp_run.stdout }}
          stderr:
          {{ wp_run.stderr }}
      
  handlers:
    - name: Reload Apache2
      become: true
      ansible.builtin.service:
        name: apache2
        state: reloaded

    - name: Restart Apache2
      become: true
      ansible.builtin.service:
        name: apache2
        state: restarted
