#!/usr/bin/env bash
# Собрать архив удалённого доступа для Fedora.
set -euo pipefail

OLD_MAC="$(cd "$(dirname "$0")/.." && pwd)"
STAGE="${TMPDIR:-/tmp}/fedora-remote-access-$$"
ARCHIVE="$OLD_MAC/deliverable/fedora-remote-access.tar.gz"
PKG="fedora-remote-access"

# Убедиться что есть pubkey
bash "$OLD_MAC/scripts/setup-remote-mac.sh" >/dev/null 2>&1 || true

cleanup() { rm -rf "$STAGE"; }
trap cleanup EXIT

rm -rf "$STAGE"
mkdir -p "$STAGE/$PKG"

cp "$OLD_MAC/scripts/setup-ssh-fedora.sh" "$STAGE/$PKG/"
cp "$OLD_MAC/scripts/setup-bt-pan-fedora.sh" "$STAGE/$PKG/"
cp "$OLD_MAC/REMOTE_ACCESS.md" "$STAGE/$PKG/"
[ -f "$OLD_MAC/deliverable/mac_pubkey.txt" ] && \
  cp "$OLD_MAC/deliverable/mac_pubkey.txt" "$STAGE/$PKG/"

cat >"$STAGE/$PKG/INSTALL.txt" <<'EOF'
Fedora — удалённый SSH с Mac (хотспот / BT PAN)
================================================

1. Распаковать:
   tar xzf fedora-remote-access.tar.gz
   cd fedora-remote-access

2. SSH (один раз, root):
   sudo bash setup-ssh-fedora.sh ВАШ_ЛОГИН

3a. Если Mac видит fedora.local и ping OK:
   На Mac: bash connect-fedora.sh

3b. Если ping FAIL (изоляция хотспота):
   sudo bash setup-bt-pan-fedora.sh
   Mac: Bluetooth → fedora → Connect (Network)
   Mac: bash setup-bt-pan-mac.sh
   Mac: bash connect-fedora.sh

Подробно: REMOTE_ACCESS.md
EOF

mkdir -p "$OLD_MAC/deliverable"
tar czf "$ARCHIVE" -C "$STAGE" "$PKG"
SHA="$(shasum -a 256 "$ARCHIVE" | awk '{print $1}')"

printf 'Архив: %s\n' "$ARCHIVE"
printf 'SHA256: %s\n' "$SHA"
printf 'Отправка: bash %s/send-to-fedora-bluetooth.sh %s\n' "$OLD_MAC/scripts" "$ARCHIVE"
