# Ansible playbook to install required Unix command-line packages, and
# then configure, compile and install a base (basic) Greenstone3
# system, running tomcat over http, and accessed directly,
# e.g. http://localhost:8383/greenstone3/library


# Playbook currently designed to be run on computer where the
# Greenstone installation is to occur (i.e., localhost)

# To run this playbook to svn checkout Greenstone3 and compile it up:
#
#     ansible-playbook -l localhost -u $USER ./greenstone3-svn-base-playbook.yml

# Ideally have the playbook install and setup Greenstone3 for the user 'greenstone'

---

- name:       Installing required command-line tools
  hosts:      localhost
  connection: local

  ####
  # Review and edit as needed the variables in the following file
  ####
  vars_files:
    - vars/default-core.yml
    
  tasks:
    - name: Setting gsdl3srchome
      set_fact: gsdl3srchome="{{ gsdl3srchome }}"
    - debug: msg="gsdl3srchome = {{ hostvars['localhost']['gsdl3srchome'] }}"
    
#    - name: Setting gsdl3port
#      set_fact: gsdl3port="{{ gsdl3port }}"
#    - debug: msg="gsdl3port = {{ hostvars['localhost']['gsdl3port'] }}"

#    - name: Setting gsdl3_buildproperties_ports
#      set_fact: gsdl3_buildproperties_ports="{{ gsdl3_buildproperties_ports }}"
#    - debug: msg="gsdl3_buildproperties_ports = {{ hostvars['localhost']['gsdl3_buildproperties_ports'] }}"

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


- name: Installing required command-line tools
  hosts:      localhost
  connection: local
  become:     true

  tasks:
    - name: Changing Ansible to use 'aptitude' as package installation manager
      apt: name=aptitude update_cache=yes state=latest force_apt_get=yes

    - name: Ensure that existing packages are up-to-date (within last 30 mins)
      apt: update_cache=yes state=latest force_apt_get=yes cache_valid_time=1800

    - name: Install prerequisites
      apt: name={{ item }} update_cache=yes state=latest
      loop: [
        'subversion', 'build-essential', 'wget',

        # As well as using 'unzip', the greenstone3 compile sequence currently uses
        # 'zip' in a handful of places, such as:
        #     <gsdl3srchome>/gli/makejar.sh
        'zip', 'unzip',

        # Choosing to install the latest, rather than work with the one included
        # as an Greenstone3 extension, as that latter is getting a bit old
        'imagemagick',
        
        # The following needed for WebSwing on a headless server
        'libxrender1', 'libxtst6', 'libxi6', 'xvfb',

        # The following are nice to have in general
        'plocate', 'emacs-nox' 
      ]

- name: Checking out Greenstone3 code from svn, configure and installing base system
  hosts:      localhost
  connection: local
  become_user: "{{ gsdl3_user }}"
  vars_files:
    - vars/default-core.yml
    
  tasks:
  - 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 }}"

  - name: Downloading and unpacking self-contained command-line tools for Greenstone3
    ansible.builtin.shell:
      chdir:     "{{ gsdl3srchome }}/ext-cli"
      # cmd:       "./get-selfcontained-jdk.sh && ./get-selfcontained-ant.sh"
      cmd:       "./get-selfcontained-{{ item }}.sh"
    register:  result
    changed_when:
      - '"Untarred" in result.stdout'      
    with_items:
      - jdk
      - ant

  # 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 }}"      
      cmd:        . ./SETUP.bash 1>/dev/null 2>&1 && env
    register:   env_file_result

#  - name: Source Greenstone3's SETUP.bash  using newer ./SETUP-ENV.sh (Ultimately new appoach not needed)
#    ansible.builtin.command: ./SETUP-ENV.sh
#    args:
#      chdir:      "{{ gsdl3srchome }}"      
#    register:   env_file_result

#  - name: Deprecated 'source' Greenstone3's SETUP.bash (command)
#    ansible.builtin.command:
#      # executable: /bin/bash
#      chdir:      "{{ gsdl3srchome }}"      
#      cmd:        ./SETUP-ENV.sh
#    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 }}"

#  - name: Display environment variables
#    command: env
#    environment: "{{ env_vars }}"

   #- debug: var=env_vars


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

#  - name: Deprecated Setting localhost.port.http in build.properties (only changes 1 value)
#    ansible.builtin.lineinfile:
#      path: "{{ gsdl3srchome }}/build.properties"
#      regexp: '^localhost.port.http='
#      line: localhost.port.http={{ gsdl3_buildproperties_port }}

  - name: Setting ports in build.properties
    ansible.builtin.lineinfile:
      path:   "{{ gsdl3srchome }}/build.properties"
      regexp: '^{{ item }}='
      #line:   "{{ item }}={{ hostvars['localhost']['gsdl3_buildproperties_ports'][item] }}"
      line:   "{{ item }}={{ gsdl3_buildproperties_ports[item] }}"      
    with_items:
      - "localhost.port.http"
      - "tomcat.shutdown.port"
      - "tomcat.ajp.port"
      - "derby.server.port"
      - "tomcat.port.https"

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

#  - name: Prepare Greenstone3 (A more complicated approach using 'expect')
#    ansible.builtin.expect:
#      chdir:       "{{ gsdl3srchome }}"
#      command:     ant prepare
#      timeout:     null
#      responses:
#        "\\[input\\]\\s+\\(y, n\\)": "y"
#    environment: "{{ env_vars }}"

#  - name: Prepare Greenstone3 II (untested, and even more complicated, requiring 'expect' to be installed)
#    ansible.builtin.shell: |
#      cd {{ gsdl3srchome }}
#      . ./SETUP.bash
#      spawn ant prepare
#      expect "[input]      (y, n):"
#      send "y\n"
#
#      exit 0
#    args:
#      executable: /usr/bin/expect


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

#  - name: Compile and Install Greenstone3 (shell)
#    ansible.builtin.shell:
#      chdir: "{{ gsdl3srchome }}"
#      cmd:   . ./SETUP.bash && ant install
    
