# 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 directly 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 to check-out and compile up Greenstone3
  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 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 distribution specific required command-line tools
  hosts:      localhost
  connection: local
  become:     true

  tasks:
  #- debug: msg="ansible_ditribution = {{ ansible_distribution }}"
  #
  - name: Debian/Ubuntu command-line tools
    include_tasks: greenstone3-svn-base-debian-task.yml
    when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

  - name: CentOS/Red Hat command-line tools
    include_tasks: greenstone3-svn-base-centos-task.yml
    when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'

- name: Checking out Greenstone3 code from svn, configure and installing base system
  hosts:      localhost
  connection: local
  become_user: "{{ gsdl3_user }}"
  become:     true
  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
      - perl-with-cpan
      - python3

  # 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

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

  - name: Setting ports in build.properties
    ansible.builtin.lineinfile:
      path:   "{{ gsdl3srchome }}/build.properties"
      regexp: '^{{ 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: 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 }}"


