#!/bin/bash
#############################################################################
#                                                                           #
# This file is part of the "ubuntu" module of the otoolbox project.         #
#                                                                           #
# This script is open-source and intended for automation purposes.          #
# It is distributed in the hope that it will be useful, but WITHOUT         #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        #
# or FITNESS FOR A PARTICULAR PURPOSE.                                      #
#                                                                           #
# Use of this script is entirely at your own risk.                          #
#                                                                           #
# Copyright (c) The otoolbox contributors.                                  #
#############################################################################
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source "$SCRIPT_DIR/bulk-common"
cd "$WORKSPACE_ROOT" || { echo "Error: failed to change directory to $WORKSPACE_ROOT" >&2; exit 1; }


# ... rest of the code remains the same ...

# copy changes to the repo
echo "Current directory is: $CURRENT_DIR"
cd "$WORKDIR"

TEMPLATE_TMP_DIR="$WORKSPACE_ROOT/.tmp"
TEMPLATE_TMP_FILE="$TEMPLATE_TMP_DIR/gen_addon_readme.rst.jinja"

mkdir -p "$TEMPLATE_TMP_DIR"
{
    echo '{#'
    echo '   addon_name: the addon name from manifest'
    echo '   authors: authors from manifest'
    echo '   badges: badges ???'
    echo '   branch: branch ???'
    echo '   fragments: list of fragments file which is a fixed list'
    echo '   manifest: the addon manifest file'
    echo '   org_name: github organization name which is part of github path'
    echo '   repo_name: the repository name which is part of github path'
    echo '   development_status: development_status???'
    echo '   source_digest: source_digest???'
    echo $'   level3_underline: "~" if fragments_format == ".rst" else "-",'
    echo ''
    echo ''
    echo ''
    echo '   FRAGMENTS:'
    echo ''
    echo '   - DESCRIPTION'
    echo '   - CONTEXT'
    echo '   - INSTALL'
    echo '   - CONFIGURE'
    echo '   - USAGE'
    echo '   - ROADMAP'
    echo '   - DEVELOP'
    echo '   - CONTRIBUTORS'
    echo '   - CREDITS'
    echo '   - HISTORY'
    echo ''
    echo ''
    echo '#}'
    echo ''
    echo ''
    echo ''
    echo "{%- macro fragment(name, title, sub='=') %}"
    echo '{%- if name in fragments %}'
    echo '{{- title }}'
    echo '{{ sub * title|length }}'
    echo ''
    echo '{{ fragments[name] }}'
    echo '{% endif %}'
    echo '{%- endmacro -%}'
    echo ''
    echo ''
    echo "{{ fragment('DESCRIPTION', 'Description') }}"
    echo "{{ fragment('CONTEXT', 'Usecase') }}"
    echo "{{ fragment('INSTALL', 'Installation') }}"
    echo "{{ fragment('CONFIGURE', 'Configuration') }}"
    echo "{{ fragment('USAGE', 'Usage') }}"
    echo "{{ fragment('CONTRIBUTORS', 'Contributer') }}"
    echo "{{ fragment('CREDITS', 'Credits') }}"
    echo "{{ fragment('HISTORY', 'History') }}"
    echo "{{ fragment('DEVELOP', 'Development') }}"
} > "$TEMPLATE_TMP_FILE"

# install or update maintainer tools
# pipx \
#     install \
#     --force \
#     oca-maintainers-tools@git+https://github.com/OCA/maintainer-tools.git


#For each folder in moonsun (it must be part of a shielded project)
for dir in "$WORKDIR/$PUBLIC_ORGANIZATION"/*/; do
    if [ -d "$dir" ]; then
        project=$(basename "$dir")
        echo ""
        echo "===================================================================="
        echo "Repository: $project"
        echo "Path: $dir"
        echo "===================================================================="
        cd "$dir"


        for addon_dir in "$WORKDIR/$PUBLIC_ORGANIZATION/$project"/*; do
            if [ -d "$addon_dir" ]; then
                if [ -f "$addon_dir/__manifest__.py" ]; then
                    addon_name=$(basename "$addon_dir")
                    echo   "Processing addon: $addon_name"

                    ####################################################################
                    # Documents
                    # commit and push changes
                    # https://github.com/OCA/oca-addons-repo-template
                    doc_files=( \
                        "/readme/CONFIGURE" \
                        "/readme/CONTEXT" \
                        "/readme/CONTRIBUTORS" \
                        "/readme/CREDITS" \
                        "/readme/DESCRIPTION" \
                        "/readme/DEVELOP" \
                        "/readme/HISTORY" \
                        "/readme/INSTALL" \
                        "/readme/ROADMAP" \
                        "/readme/USAGE" \
                        "/README" \
                        "/doc/index"
                    )
                    doc_dirs=(\
                        "/readme" \
                        "/doc"
                    )
                    for doc_dir in "${doc_dirs[@]}"; do
                        mkdir -p "$addon_dir/$doc_dir"
                    done

                    for doc_file in "${doc_files[@]}";do
                        if [ -f "${addon_dir}/${doc_file}.md" ]; then
                            mv -f \
                                "${addon_dir}/${doc_file}.md" \
                                "${addon_dir}/${doc_file}.rst"
                        fi
                        touch "${addon_dir}/${doc_file}.rst"
                    done
                fi
            fi
        done

        #
        #  Precommit
        #
        pre-commit run -a > "$LOG_FILE" 2>&1

        #
        # Generage customer readme
        #
        if [ -f "$TEMPLATE_TMP_FILE" ]; then
            oca-gen-addon-readme \
                --addons-dir="$dir" \
                --branch=${ODOO_VERSION} \
                --org-name=${SHIELDED_ORGANIZATION} \
                --repo-name=$project \
                --if-source-changed \
                --keep-source-digest \
                --template-filename="$TEMPLATE_TMP_FILE"
        fi
    fi
done
