#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" cd "$ROOT_DIR" TOPIC="${1:-Filo Logico Contabilita}" MODE="${2:-both}" # daily|weekly|both DATE_TODAY="$(date +%F)" WEEK_TAG="$(date +%G-W%V)" NOW="$(date '+%Y-%m-%d %H:%M:%S')" TOPIC_SLUG="$(echo "$TOPIC" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//;s/-$//')" TOPIC_FILE="docs/ai/topics/${TOPIC_SLUG}.md" DAILY_FILE="docs/ai/summaries/daily/${DATE_TODAY}.md" WEEKLY_FILE="docs/ai/summaries/weekly/${WEEK_TAG}.md" mkdir -p "docs/ai/summaries/daily" "docs/ai/summaries/weekly" latest_checkpoint_raw="$(awk ' /^## CHECKPOINT / {line=NR} {lines[NR]=$0} END { if (line == 0) exit for (i=line; i<=NR; i++) print lines[i] } ' docs/ai/SESSION_HANDOFF.md 2>/dev/null || true)" latest_checkpoint="$(awk ' /^### File modificati/ {print "- File modificati: (omessi nel summary periodico)"; exit} NR<=40 {print} ' <<< "$latest_checkpoint_raw")" if [[ -z "$latest_checkpoint" ]]; then latest_checkpoint="(Nessun CHECKPOINT trovato in docs/ai/SESSION_HANDOFF.md)" elif [[ "$(printf '%s\n' "$latest_checkpoint_raw" | wc -l)" -gt 40 ]]; then latest_checkpoint+=$'\n\n... (estratto troncato, vedi SESSION_HANDOFF completo)' fi git_short_raw="$(git status --short 2>/dev/null || true)" if [[ -z "$git_short_raw" ]]; then git_short="(working tree pulita)" git_stats="M=0 A=0 D=0 R=0 ??=0" else git_short="$(printf '%s\n' "$git_short_raw" | awk 'NR<=120{print}')" if [[ "$(printf '%s\n' "$git_short_raw" | wc -l)" -gt 120 ]]; then git_short+=$'\n... (troncato)' fi cnt_m="$(printf '%s\n' "$git_short_raw" | grep -Ec '^[ MARC][MARC] ' || true)" cnt_a="$(printf '%s\n' "$git_short_raw" | grep -Ec '^A ' || true)" cnt_d="$(printf '%s\n' "$git_short_raw" | grep -Ec '^D ' || true)" cnt_r="$(printf '%s\n' "$git_short_raw" | grep -Ec '^R ' || true)" cnt_u="$(printf '%s\n' "$git_short_raw" | grep -Ec '^\?\? ' || true)" git_stats="M=${cnt_m} A=${cnt_a} D=${cnt_d} R=${cnt_r} ??=${cnt_u}" git_focus_raw="$(printf '%s\n' "$git_short_raw" | grep -E ' (app/|resources/|routes/|config/|database/|docs/ai/|scripts/ai/|\.vscode/|composer\.json|package\.json)' || true)" git_focus="$(printf '%s\n' "$git_focus_raw" | awk 'NR<=80{print}')" if [[ -z "$git_focus" ]]; then git_focus="$git_short" fi fi latest_two_hint="docs/ai/LATEST_2_CHATS_RECOVERY.md" write_summary() { local file_path="$1" local period_label="$2" cat > "$file_path" <