#!/bin/bash
# archive-kimi-session.sh
# Quick wrapper to archive the most recent Kimi Code session to Obsidian.
#
# Usage:
#   ./archive-kimi-session.sh [topic]
#
# Examples:
#   ./archive-kimi-session.sh hermes-mcp-oauth-fix
#   ./archive-kimi-session.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PYTHON_SCRIPT="$SCRIPT_DIR/kimi-session-to-obsidian.py"
SESSIONS_DIR="$HOME/.kimi/sessions"

# Find the most recently modified session directory
if [ ! -d "$SESSIONS_DIR" ]; then
    echo "Error: Kimi sessions directory not found at $SESSIONS_DIR"
    exit 1
fi

LATEST_SESSION=$(ls -t "$SESSIONS_DIR" | head -n 1)

if [ -z "$LATEST_SESSION" ]; then
    echo "Error: No Kimi sessions found in $SESSIONS_DIR"
    exit 1
fi

TOPIC="${1:-$LATEST_SESSION}"

echo "Archiving session: $LATEST_SESSION"
echo "Topic: $TOPIC"

python3 "$PYTHON_SCRIPT" "$LATEST_SESSION" "$TOPIC"
