#!/bin/bash
# ─── Empaqueta binarios amd64 en ZIP para Arch Linux ─────────────────────────
# Uso: ./package-zip-arch.sh <version>
# Genera: GR-player-RC1-v<version>_amd64_arch.zip  (en el directorio padre)
set -e

VERSION="${1:?Uso: $0 <version>  ej: $0 0.4.6}"
ZNAME="GR-player-RC1-v${VERSION}_amd64_arch"
ZDIR="/tmp/${ZNAME}"
DEST="$(dirname "$0")/../${ZNAME}.zip"

cd "$(dirname "$0")"

# ── Verificar binarios ────────────────────────────────────────────────────────
BINS=(
    radio-player
    comercial-scheduler
    playlist-refill
    gr-visor
    gr-botonera
    gr-buscador
    gr-parrilla
    gr-pautaje
    gr-playlist
    gr-record
    gr-reportes
)
for bin in "${BINS[@]}"; do
    if [ ! -f "target/release/$bin" ]; then
        echo "ERROR: binario no encontrado: target/release/$bin"
        echo "       Ejecuta 'cargo build --release' primero."
        exit 1
    fi
done

# Assets en el directorio padre (fuera de git/)
ASSETS_DIR="../target/data/panel/Time"
if [ ! -d "$ASSETS_DIR" ]; then
    echo "ERROR: carpeta 'target/data/panel/Time' no encontrada en el directorio padre."
    exit 1
fi

# ── Armar estructura del ZIP ──────────────────────────────────────────────────
rm -rf "$ZDIR"
mkdir -p "$ZDIR/target/release"
mkdir -p "$ZDIR/target/data/panel"

for bin in "${BINS[@]}"; do
    cp "target/release/$bin" "$ZDIR/target/release/$bin"
done

cp -r "$ASSETS_DIR" "$ZDIR/target/data/panel/Time"
cp install-arch.sh "$ZDIR/install.sh"
cp uninstall.sh    "$ZDIR/uninstall.sh"
chmod +x "$ZDIR/install.sh" "$ZDIR/uninstall.sh"

# ── Crear ZIP ─────────────────────────────────────────────────────────────────
rm -f "$DEST"
(cd /tmp && zip -r "$OLDPWD/$DEST" "$ZNAME")
rm -rf "$ZDIR"

echo ""
echo "  ZIP generado: $(realpath "$DEST")"
echo "  Versión: $VERSION  |  Arch Linux amd64"
