# Ansible playbook to add in the intermuse site and interface
# to a GS3 base installation

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

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

  vars_files:
    - vars/default-core.yml
    ####
    # Review and edit as needed the variables in the following file
    ####
    # - vars/default-intermuse.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 InterMusE interface and site
  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 InterMusE
    ansible.builtin.shell:
      chdir:     "{{ gsdl3srchome }}/ext-cli"
      cmd:       "./get-selfcontained-{{ item }}.sh"
    register:  result
    changed_when:
      - '"Untarred" in result.stdout'      
    with_items:
      - cmake
      - mvn
      - nodejs
      - rclone

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

  #- name: CentOS/Red Hat command-line tools
  #  include_tasks: intermuse-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 SETUP.bash
  # For more details, see:
  #   https://stackoverflow.com/questions/60209185/ansible-environment-variables-from-env-file
  #
  - name: Source Greenstone3's SETUP.bash
    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:        . ./SETUP.bash 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 .... run get it, and casecade make
  # !!!!

  # iiif-servlet  structured-image jena-fuseki

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

#  - name: Compiling and Installing the iiif-servlet extension
#    ansible.builtin.shell:
#      chdir: "{{ gsdl3srchome }}/ext/iiif-servlet"
#      cmd: |
#         ./PREPARE-GSDL-AND-COMPILE-CORE.sh
#         ./PREPARE-CANTALOUPE.sh
#         ./COMPILE-CANTALOUPE.sh
#         ./INSTALL-CANTALOUPE-WAR.sh
#    environment: "{{ env_vars }}"

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

  - name: Compiling and Installing the jena-fuseki extension
    ansible.builtin.command:
      chdir: "{{ gsdl3srchome }}/ext/jena-fuseki"
      cmd: ./CASCADE-MAKE.sh
    environment: "{{ env_vars }}"

  - name: Creating the jena-fuseki extension dataset /greenstone
    ansible.builtin.command:
      chdir: "{{ gsdl3srchome }}/ext/jena-fuseki"      
      stdin:             "y"
      stdin_add_newline: true
      cmd: ./CREATE-GREENSTONE-DATASET.sh
    register: result      
    environment: "{{ env_vars }}"

  # - debug: var=result.stdout_lines

#  - 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 InterMusE iterface and site
    ansible.builtin.command:
      chdir: "{{ gsdl3srchome }}/web"
      cmd: ./get-installation.sh intermuse
    register:  result
    changed_when:
      - '"A    " | string in result.stdout'      
    environment: "{{ env_vars }}"



  #- name: Generate build.properties for Greenstone3
  #  ansible.builtin.command:
  #    chdir: "{{ gsdl3srchome }}"
  #    cmd:   ant
  #  environment: "{{ env_vars }}"

  #- name: Prepare Greenstone3
  #  ansible.builtin.command:
  #    chdir:             "{{ gsdl3srchome }}"
  #    stdin:             "y"
  #    stdin_add_newline: true
  #    cmd:               ant prepare
  #  environment: "{{ env_vars }}"

  #- name: Compile and Install Greenstone3 (command)
  #  ansible.builtin.command:
  #    chdir: "{{ gsdl3srchome }}"
  #    cmd:   ant install
  #  environment: "{{ env_vars }}"

#  - name: Compile and Install Greenstone3 (shell, piped to /dev/tty)
#    ansible.builtin.shell:
#      chdir: "{{ gsdl3srchome }}"
#      cmd:   ant install | tee /dev/tty
#    environment: "{{ env_vars }}"


