# Ansible playbook to add in the willow site and interface
# to a GS3 base installation, along with its supporting
# dependencies

# See greenstone3-svn-base-playbook.yml for details on how to run
---

- name:       Setting up variables for The Willow installation
  hosts:      localhost
  connection: local

  vars_files:
    - vars/default-core.yml
    ####
    # Review and edit as needed the variables in the following file
    ####
    # - vars/default-thewillow.yml # Review and edit as needed    

  tasks:
    - name: Setting Greenstone3 username
      set_fact: gsdl3_user="{{ ansible_user | default(ansible_env.USER,true) | default('greenstone',true) }}"
    - debug: msg="gsdl3_user = {{ gsdl3_user }}"


- name:       Installing The Willow interface and site, and supporting dependencies
  hosts:      localhost
  connection: local
  become_user: "{{ gsdl3_user }}"

  vars_files:
    - vars/default-core.yml
    
  tasks:
  - name: Downloading and unpacking self-contained command-line tools for The Willow
    ansible.builtin.shell:
      chdir:     "{{ gsdl3srchome }}/ext-cli"
      cmd:       "./get-selfcontained-{{ item }}.sh"
    register:  result
    changed_when:
      - '"Untarred" in result.stdout'      
    with_items:
      - rclone

  #- name: Debian/Ubuntu command-line tools
  #  include_tasks: thewillow-installation-debian-task.yml
  #  when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

  #- name: CentOS/Red Hat command-line tools
  #  include_tasks: thewillow-installation-centos-task.yml
  #  when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'

  #- debug: msg="gsdl3_user = {{ gsdl3_user }}, USER={{ ansible_env.USER }}"
  #- name: Subversion checkout of Greenstone3
  #  ansible.builtin.subversion:
  #    repo: https://svn.greenstone.org/main/trunk/greenstone3
  #    checkout: yes
  #    update: no
  #    dest: "{{ gsdl3srchome }}"


  # The next two steps are equivalent to sourcing gs3-setup.sh
  # For more details, see:
  #   https://stackoverflow.com/questions/60209185/ansible-environment-variables-from-env-file
  #
  - name: Source Greenstone3's gs3-setup.sh
    ansible.builtin.shell:
      executable: /bin/bash
      chdir:      "{{ gsdl3srchome }}"
      # RHEL8.5 introduces a weirdly environemnt variable BASH_FUNC_which%% set to a multi-line JSON value
      # => use 'sed' to delete this form 'env' output
      cmd:        . ./gs3-setup.sh 1>/dev/null 2>&1 && env | sed '/^BASH_FUNC_which%%=() {.*$/,/^}$/d'
    register:   env_file_result

  #- debug: var=env_file_result.stdout_lines

  - name: Parse Greenstone3 environment variables
    set_fact:
      env_vars: "{{ ('{' + env_file_result.stdout_lines | map('regex_replace', '([^=]*)=(.*)', '\"\\1\": \"\\2\"') | join(',') + '}') | from_json }}"

  #- debug: var=env_vars

  # !!!!
  # For each extension .... get it, and run cascade make (or equivalent)
  # !!!!

  # structured-image

  - name: Svn checking out extensions for The Willow
    ansible.builtin.shell:
      chdir:     "{{ gsdl3srchome }}/ext"
      cmd:       "./get-extension.sh {{ item }}"
    register:  result
    changed_when:
      - '"A    " | string in result.stdout'      
    with_items:
      - structured-image 

  - name: Compiling and Installing the structured-image extension
    ansible.builtin.shell:
      chdir: "{{ gsdl3srchome }}/ext/structured-image"
      cmd: |
         ./PREPARE-PYTHON3.sh
         cd packages
         ./COMPILE-AND-INSTALL.sh
         cd ..
#          
#         cd src
#         ./PREPARE.sh
#         ./COMPILE.sh
#         # Generated Warnings are OK
#         ./INSTALL.sh
    environment: "{{ env_vars }}"


#  - name: Compiling and Installing the structured-image extension
#    ansible.builtin.command:
#      chdir: "{{ gsdl3srchome }}/ext/structured-image"
#      cmd: ./CASCADE-COMPILE-AND-INSTALL.sh
#    environment: "{{ env_vars }}"


  - name: Svn checkout of The Willow iterface and site
    ansible.builtin.command:
      chdir: "{{ gsdl3srchome }}/web"
      cmd: ./get-installation.sh thewillow
    register:  result
    changed_when:
      - '"A    " | string in result.stdout'      
    environment: "{{ env_vars }}"

  - debug: msg="Additional steps needed{{ ':' }} (1) update resources/web/servlet.xml.in to interface=thewillow site=thewillow; (2) add in service key for Google Vision API <collect>/etc/google-sa-credentials-key.json"



