#!/usr/bin/env bash
set -euo pipefail
source "$stpRoot/lib/log.sh"
source "$stpRoot/lib/utils.sh"

readonly easyEffectsAppId="com.github.wwmm.easyeffects"
readonly easyEffectsApplicationDirectory="$HOME/.var/app/com.github.wwmm.easyeffects"
readonly easyEffectsConfig="$easyEffectsApplicationDirectory/config/easyeffects/db/easyeffectsrc"
readonly easyEffectsArchive="$stpRoot/secrets/easyEffectsConfig.zip"

easyEffectsIsInstalled() {
    util::isFlatpakInstalled "$easyEffectsAppId"
}

easyEffectsConfigExists() {
    [[ -f "$easyEffectsConfig" ]]
}

restoreConfig() {
    log::info "Restaurando configuración desde secrets/easyEffectsConfig.zip..."
    mkdir -p "$easyEffectsApplicationDirectory"
    unzip -qo "$easyEffectsArchive" -d "$easyEffectsApplicationDirectory"
    log::ok "Configuración restaurada en: $easyEffectsApplicationDirectory"
}

if ! easyEffectsIsInstalled; then
    log::info "EasyEffects no instalado aún, salteando restauración de config"
    exit 0
fi

if [[ ! -f "$easyEffectsArchive" ]]; then
    log::info "Sin respaldo de configuración (secrets/easyEffectsConfig.zip), salteando"
    exit 0
fi

if easyEffectsConfigExists; then
    log::info "Configuración de EasyEffects ya existe, salteando restauración"
    exit 0
fi

restoreConfig
