#!/bin/bash
##
## Copyright (C) 2015-2020 Robin Schneider <ypid@riseup.net>
## Copyright (C) 2017-2020 DebOps https://debops.org/
## SPDX-License-Identifier: GPL-3.0-only

set -o nounset -o pipefail -o errexit

## TODO: Migrate more parts from ./debops-optimize-old

## Script is expected to be run from the root of the DebOps monorepo.
cd "$(git rev-parse --show-toplevel)" || exit

## Testing:
# perl -i -p0e '
# ' ansible/roles/prometheus_exporter/templates/etc/ansible/facts.d/prometheus_exporter.fact.j2
# exit 0

git ls-files -z "$(git rev-parse --show-toplevel)" | xargs --null -I '{}' find './{}' -type f -print0 | \
    xargs --null perl -i -p0e '
        s~
            ## Use Ansible 2.8 default shortcut: True or False

            ## Line: .*True
            \(?\n?True\n?

            ## Line: if (ansible_local|d() and ansible_local.pki|d() and
            \s+if\s*\((?<var_ref1>ansible_local)\s*\|\s*d(?:efault)?\(\)\s+and\s+(?<var_ref2>\k<var_ref1>\.\w+)\s*\|\s*d(?:efault)?\(\)\s+and\n?
            ## var_ref1: ansible_local
            ## var_ref2: ansible_local.pki

            ## Line: ansible_local.pki.enabled|d()|bool)
            \s+(?<expression>\k<var_ref2>\.\w+(?:\s*\|\s*d(?:efault)?\(\))?)\)\n?
            ## expression: ansible_local.pki.enabled|d()|bool

            ## Line: else False
            \s+else\s+False\)?(?:\s*\|\s*bool)?
         ~$+{expression}~xg;

        s~
            ## Use Ansible 2.8 default shortcut: True or False (no final default in expression)

            ## Line: .*True
            \(?\n?True\n?

            ## Line: if (ansible_local|d() and ansible_local.pki|d() and
            \s+if\s*\((?<var_ref1>ansible_local)\s*\|\s*d(?:efault)?\(\)\s+and\s+(?<var_ref2>\k<var_ref1>\.\w+)\s*\|\s*d(?:efault)?\(\)\s+and\n?
            ## var_ref1: ansible_local
            ## var_ref2: ansible_local.pki

            ## Line: ansible_local.pki.enabled|bool)
            \s+(?<expression>\k<var_ref2>\.\w+?)\s*\|\s*bool\)\n?
            ## expression: ansible_local.pki.enabled|d()|bool

            ## Line: else False
            \s+else\s+False\)?(?:\s*\|\s*bool)?
        ~$+{expression}|d()|bool~xg;

        s~
            ## Use Ansible 2.8 default shortcut: Thing if defined else fallback

            ## Line: .*ansible_local.core.domain
            \(?\n?(?<var_ref3>(?<var_ref2>(?<var_ref1>ansible_local)\.\w+)\.\w+)\n?
            ## var_ref1: ansible_local
            ## var_ref2: ansible_local.core
            ## var_ref3: ansible_local.core.domain

            ## Line: if (ansible_local|d() and ansible_local.core|d() and
            \s+if\s+\(\k<var_ref1>\s*\|\s*d(?:efault)?\(\)\s+and\s+\k<var_ref2>\s*\|\s*d(?:efault)?\(\)\s+and\n?

            ## Line: ansible_local.core.domain|d())
            \s+\k<var_ref3>(?:\s*\|\s*d(?:efault)?\(\))?\)\n?

            ## Line: else ansible_domain
            \s+else\s+\(?(?<else_exp>[^\n]+?)\)?\s+(?<end_marker>-?}}|-?%})
            ## else_exp: ansible_domain
        ~$+{var_ref3}|d($+{else_exp}) $+{end_marker}~xg;

        s~
            ## Use Ansible 2.8 default shortcut: More generic catch-all as last resort.

            ## Line: ansible_local|d() and ansible_local.pki|d() and
            (?<var_ref1>ansible_local)\s*\|\s*d(?:efault)?\(\)\s+and\s+(?<var_ref2>\k<var_ref1>\.\w+)\s*\|\s*d(?:efault)?\(\)\s+and\n?
            ## var_ref1: ansible_local
            ## var_ref2: ansible_local.pki

            ## Line: ansible_local.pki.enabled|d()|bool
            \s+(?<expression>\k<var_ref2>\.\w+\s*\|\s*d(?:efault)?\(\))
            ## expression: ansible_local.pki.enabled|d()
         ~$+{expression}~xg;
    '


## TODO: I need to bring yaml4rst back to speed again.
exit

for role_dir in ./ansible/roles/*; do
    role_full_name="$(basename "$role_dir")"
    # role_name="${role_full_name#*.}"
    # role_owner="${role_full_name%.*}"
    # is_ansible_role=true

    echo "Role: ${role_full_name}"
    echo "Defaults file: ${role_dir}/defaults/main.yml"

    if command -v yaml4rst
    then
        test -e "${role_dir}/defaults/main.yml" && yaml4rst --config-kv "ansible_full_role_name=${role_full_name}" "${role_dir}/defaults/main.yml" -i
    fi
done
