#!/bin/bash
#
# Apply upgrade workarounds
set -euo pipefail

function apply_patch {
    local patch_dir=$1
    local patch_id=$2
    local temp_dir=''

    temp_dir=$( mktemp -d )

    curl -4 --retry 5 https://review.opendev.org/changes/${patch_id}/revisions/current/patch?download | \
        base64 -d > ${temp_dir}/patch.txt

    if [[ ${?} -ne 0 ]] ; then
        echo "Failed to download patch https://review.opendev.org/#/c/${patch_id}/"
        exit 1
    fi

    if sudo patch --dry-run --reverse --force -d ${patch_dir} -p1 < ${temp_dir}/patch.txt >/dev/null 2>&1
    then
        echo "Patch ${patch_id} already applied, skipping"
    else
        sudo patch -Ns  -d ${patch_dir} -p1 < ${temp_dir}/patch.txt
    fi

    sudo rm -Rf ${temp_dir}

}

{% macro render_workarounds(workaround_var, workaround_name) -%}
{% if workaround_name in item -%}
{% for bugs in workaround_var -%}
{% for key, value in bugs.items() -%}
echo {{ key }}
{% if value.patch -%}
apply_patch {{ value.basedir }} {{ value.id }}
{% else -%}
{{ value.command }}
{% endif -%}
################################################################################
{% endfor -%}
{% endfor -%}
{% endif -%}
{% endmacro -%}

{{ render_workarounds(pre_ffu_undercloud_upgrade_workarounds|default([]),'pre_ffu_undercloud_upgrade_workarounds') }}
{# finish pre_ffu_undercloud_upgrade_workarounds #}
{{ render_workarounds(post_ffu_undercloud_upgrade_workarounds|default([]),'post_ffu_undercloud_upgrade_workarounds') }}
{# finish post_ffu_undercloud_upgrade_workarounds #}
{{ render_workarounds(pre_ffu_overcloud_prepare_workarounds|default([]),'pre_ffu_overcloud_prepare_workarounds') }}
{# finish pre_ffu_overcloud_prepare_workarounds #}
{{ render_workarounds(post_ffu_overcloud_prepare_workarounds|default([]),'post_ffu_overcloud_prepare_workarounds') }}
{# finish post_ffu_overcloud_prepare_workarounds #}
{{ render_workarounds(pre_ffu_overcloud_run_workarounds|default([]),'pre_ffu_overcloud_run_workarounds') }}
{# finish pre_ffu_overcloud_run_workarounds #}
{{ render_workarounds(post_ffu_overcloud_run_workarounds|default([]),'post_ffu_overcloud_run_workarounds') }}
{# finish post_ffu_overcloud_run_workarounds #}
{{ render_workarounds(pre_ffu_overcloud_upgrade_workarounds|default([]),'pre_ffu_overcloud_upgrade_workarounds') }}
{# finish pre_ffu_overcloud_upgrade_workarounds #}
{{ render_workarounds(post_ffu_overcloud_upgrade_workarounds|default([]),'post_ffu_overcloud_upgrade_workarounds') }}
{# finish post_ffu_overcloud_upgrade_workarounds #}
{{ render_workarounds(pre_ffu_overcloud_converge_workarounds|default([]),'pre_ffu_overcloud_converge_workarounds') }}
{# finish pre_ffu_overcloud_converge_workarounds #}
{{ render_workarounds(post_ffu_overcloud_converge_workarounds|default([]),'post_ffu_overcloud_converge_workarounds') }}
{# finish post_ffu_overcloud_converge_workarounds #}
{{ render_workarounds(pre_ffu_overcloud_ceph_workarounds|default([]),'pre_ffu_overcloud_ceph_workarounds') }}
{# finish pre_ffu_overcloud_ceph_workarounds #}
{{ render_workarounds(post_ffu_overcloud_ceph_workarounds|default([]),'post_ffu_overcloud_ceph_workarounds') }}
{# finish post_ffu_overcloud_ceph_workarounds #}
