{% from '.github/workflows/version-info.py.j2' import version_info %}
{% macro _build_body(settings) %}
cat > temp_script.py <<EOF
from ${python_path} import ${func_name}
if __name__ == '__main__':
    ${func_name}()
EOF
if [[ "${ACTIONS_RUNNER_DEBUG}" == 'true' ]]; then
    cat temp_script.py
fi
cat > version-info.txt <<EOF
{{ version_info(settings) | trim }}
EOF
if [[ "${ACTIONS_RUNNER_DEBUG}" == 'true' ]]; then
    cat version-info.txt
fi
echo "Building executable for '${script_name}'..."
{% if settings.pyinstaller.vcpkg.enabled %}
openssl_args=()
if [[ -n "${OPENSSL_DIR:-}" ]]; then
    openssl_args=(--add-binary "${OPENSSL_DIR}/bin/*.dll;.")
fi
{% endif %}
{{ 'uv run' if settings.package_manager == 'uv' else 'poetry run --' }} pyinstaller --log-level ERROR --onefile -n "${script_name}" --optimize 1 \
{% for mod in settings.pyinstaller.hidden_imports %}
  --hidden-import {{ mod }} \
{% endfor %}
{% for pkg in settings.pyinstaller.collect_submodules %}
  --collect-submodules {{ pkg }} \
{% endfor %}
{% for pkg in settings.pyinstaller.collect_data %}
  --collect-data {{ pkg }} \
{% endfor %}
{% for pkg in settings.pyinstaller.copy_metadata %}
  --copy-metadata {{ pkg }} \
{% endfor %}
  -y --version-file version-info.txt \
{% if settings.pyinstaller.extra_args %}
  {{ settings.pyinstaller.extra_args | join(" ") }} \
{% endif %}
{% if settings.pyinstaller.vcpkg.enabled %}
  "${openssl_args[@]}" \
{% endif %}
  temp_script.py
if [[ "${ACTIONS_RUNNER_DEBUG}" == 'true' ]]; then
    cat "${script_name}.spec"
fi
{% endmacro %}
{% macro build(settings) %}
    {% set single = settings.pyinstaller.include_only | length == 1 %}
project_name=$(tomlq -r '.project.urls.repository|split("/")[-1]' pyproject.toml)
vers="$(tomlq -r '.project.version|split(".")|map(tonumber)|@csv' pyproject.toml),0"
version=$(tomlq -r .project.version pyproject.toml)
    {% if single %}
script_name='{{ settings.pyinstaller.include_only[0] }}'
IFS=$' ' read -r python_path func_name < <(tomlq -r '.project.scripts."'"${script_name}"'"|split(":")|join(" ")' pyproject.toml)
func_name="${func_name#"${func_name%%[![:space:]]*}"}"
func_name="${func_name%"${func_name##*[![:space:]]}"}"
{{ _build_body(settings) }}
    {% else %}
        {% if settings.pyinstaller.include_only | length > 1 %}
include_only=(
  {{ settings.pyinstaller.include_only | join("\n") | indent(2) | trim }}
)
        {% else %}
            {% if settings.pyinstaller.macos_exclusions %}
macos_exclusions=(
  {{ settings.pyinstaller.macos_exclusions | join("\n") | indent(2) | trim }}
)
            {% endif %}
            {% if settings.pyinstaller.windows_exclusions %}
windows_exclusions=(
  {{ settings.pyinstaller.windows_exclusions | join("\n") | indent(2) | trim }}
)
            {% endif %}
        {% endif %}
while IFS=$' ' read -r script_name python_path func_name; do
        {% if settings.pyinstaller.include_only | length > 1 %}
    if ! printf '%s\0' "${include_only[@]}" | grep -Fqxz -- "${script_name}"; then
        echo "Skipping ${script_name} (not in include_only)."
        continue
    fi
        {% else %}
            {% if settings.pyinstaller.windows_exclusions %}
    if [[ "${{ '{{' }} matrix.os }}" == windows* ]] &&
        printf '%s\0' "${windows_exclusions[@]}" | grep -Fqxz -- "${script_name}"; then
        echo "Skipping ${script_name} on Windows."
        continue
    fi
            {% endif %}
            {% if settings.pyinstaller.macos_exclusions %}
    if [[ "${{ '{{' }} matrix.os }}" == macos* ]] &&
        printf '%s\0' "${macos_exclusions[@]}" | grep -Fqxz -- "${script_name}"; then
        echo "Skipping ${script_name} on macOS."
        continue
    fi
            {% endif %}
        {% endif %}
    func_name="${func_name#"${func_name%%[![:space:]]*}"}"
    func_name="${func_name%"${func_name##*[![:space:]]}"}"
    {{ _build_body(settings) | shell_indent(4) | trim }}
done < <(tomlq -r '.project.scripts|to_entries[]|"\(.key) \(.value|split(":")|join(" "))"' pyproject.toml)
    {% endif %}
{% endmacro %}
