#!/usr/bin/env bash
# Regenerate Telegram session string via QR login.
# Reads TELEGRAM_API_ID / TELEGRAM_API_HASH from the project .env,
# then runs the generator from the telegram-mcp install.
set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
TELEGRAM_DIR="/projects/my-utils/telegram"

if [ ! -f "$PROJECT_ROOT/.env" ]; then
  echo "missing $PROJECT_ROOT/.env" >&2
  exit 1
fi

set -a
. "$PROJECT_ROOT/.env"
set +a

if [ -z "${TELEGRAM_API_ID:-}" ] || [ -z "${TELEGRAM_API_HASH:-}" ]; then
  echo "TELEGRAM_API_ID / TELEGRAM_API_HASH missing in $PROJECT_ROOT/.env" >&2
  exit 1
fi

cd "$TELEGRAM_DIR"
exec .venv/bin/python session_string_generator.py --qr "$@"
