#!/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; }

INSTALL_OCA_MAINTAINERS_TOOLS=0
ADD_DOCS=0

usage() {
    cat <<'EOF'
Usage: bulk-pre-commit [OPTIONS]

Options:
  -i, --install                         Install/update oca-maintainers-tools and others if needed
  -d, --add-docs                        Create/add docs scaffold for each addon
  -h, --help                            Show this help message
EOF
}

install_oca_maintainers_tools() {
    echo "Installing/updating oca-maintainers-tools..."
    if command -v pipx >/dev/null 2>&1; then
        pipx install --force "oca-maintainers-tools@git+https://github.com/OCA/maintainer-tools.git"
        return
    fi

    if command -v pip >/dev/null 2>&1; then
        pip install --upgrade "oca-maintainers-tools@git+https://github.com/OCA/maintainer-tools.git"
        return
    fi

    echo "Error: neither pipx nor pip is available for installing oca-maintainers-tools." >&2
    exit 1
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        -i|--install)
            INSTALL_OCA_MAINTAINERS_TOOLS=1
            ;;
        -d|--add-docs)
            ADD_DOCS=1
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            echo "Unknown option: $1" >&2
            usage
            exit 1
            ;;
    esac
    shift
done


# ... 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 '   - 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') }}"
} > "$TEMPLATE_TMP_FILE"

# install or update maintainer tools if requested
if [[ "$INSTALL_OCA_MAINTAINERS_TOOLS" -eq 1 ]]; then
    install_oca_maintainers_tools
fi


#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"

        # Gererate readme for each addon
        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"

                    if [[ "$ADD_DOCS" -eq 1 ]]; then
                        ####################################################################
                        # 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/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
                            touch "${addon_dir}/${doc_file}.md"
                        done
                    fi
                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 \
                --convert-fragments-to-markdown \
                --template-filename="$TEMPLATE_TMP_FILE"
        fi
    fi
done
