#!/bin/bash

#
WPS_CONFIG_FILE='{{ smartgears_instance_path }}/webapps/{{ dataminer_wps_name }}/config/wps_config.xml'

# Fix the hostname
{% if hostname is defined %}
sed -Ei 's/hostname=\".*\"\ hostport/hostname="{{ hostname }}" hostport/' "$WPS_CONFIG_FILE"
{% else %}
sed -Ei 's/hostname=\".*\"\ hostport/hostname="{{ smartgears_hostname }}" hostport/' "$WPS_CONFIG_FILE"
{% endif %}

# Set the port. It is different from the tomcat one if there is a reverse proxy before the service.
# https has the precedence if it is available
# TODO: change back to http and https_port when https://support.d4science.org/issues/13024 if fixed and the new dataminer deployed
{% if setup_nginx %}
{% if https_port is defined %}
sed -Ei 's/hostport=\".*\"\ includeDataInputsInResponse/hostport="{{ http_port | default('80') }}" includeDataInputsInResponse/' "$WPS_CONFIG_FILE"
sed -Ei 's/protocol=\".*\"\ hostname/protocol="http" hostname/' "$WPS_CONFIG_FILE"
{% else %}
sed -Ei 's/hostport=\".*\"\ includeDataInputsInResponse/hostport="{{ http_port | default('80') }}" includeDataInputsInResponse/' "$WPS_CONFIG_FILE"
{% endif %}
{% else %}
sed -Ei 's/hostport=\".*\"\ includeDataInputsInResponse/hostport="{{ item.http_port }}" includeDataInputsInResponse/' "$WPS_CONFIG_FILE"
{% endif %}

exit 0