#!/usr/bin/env bash
# Excluye emails, caché, crash dumps y archivos de bloqueo.
# Replica lo que hace el export integrado de Thunderbird (Ajustes → Exportar perfil).
set -euo pipefail
stpRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source "$stpRoot/lib/log.sh"

readonly thunderbirdApplicationDirectory="$HOME/snap/thunderbird/common"
readonly thunderbirdProfileDirectory="$thunderbirdApplicationDirectory/.thunderbird"
readonly outputFile="$stpRoot/secrets/thunderbirdProfile.zip"

thunderbirdDataExists() {
    [[ -d "$thunderbirdProfileDirectory" ]]
}

if ! thunderbirdDataExists; then
    log::warn "Datos de Thunderbird Snap no encontrados en: $thunderbirdProfileDirectory"
    log::info "Asegurate de haber abierto Thunderbird al menos una vez antes de capturar"
    exit 0
fi

log::step "Capturando configuración de Thunderbird"
mkdir -p "$stpRoot/secrets"
rm -f "$outputFile"

(
    cd "$thunderbirdApplicationDirectory"
    find ".thunderbird" \
        | grep -Ev "/(Mail|ImapMail|News|cache|cache2|startupCache|minidumps|crashes|storage|Crash Reports)(/|$)" \
        | grep -Ev "\.(msf|sqlite-wal|sqlite-shm)$" \
        | grep -Ev "/(parent\.lock|lock)$" \
        | zip -q "$outputFile" -@
)

log::ok "Guardado: secrets/thunderbirdProfile.zip ($(du -sh "$outputFile" | cut -f1))"
log::info "Commiteá secrets/thunderbirdProfile.zip antes de hacer push"
