#!/usr/bin/env bash

# This script is intended to be run after `snap-build`.
#
# It expects a version to be passed as the first argument, and expects
# the built `.snap` for that version to be in the current directory.
#
# This will uninstall the current `coop` snap, replacing it with a snap
# that directly uses the `snap/unpacked` directory.

set -euxo pipefail

if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <release_version>"
  exit 1
fi

# Get system architecture (same mapping as script/bundle-snap)
ARCH=$(uname -m)
case "$ARCH" in
  x86_64) ARCH_SUFFIX="x86_64" ;;
  aarch64) ARCH_SUFFIX="aarch64" ;;
  *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
esac

snap_file="coop_${1}_${ARCH_SUFFIX}.snap"
if [ ! -f "$snap_file" ]; then
  echo "Snap file not found: $snap_file"
  echo "Build it first with: script/bundle-snap $1"
  exit 1
fi

# Rerun as root
[ "$UID" -eq 0 ] || exec sudo bash -e "$0" "$@"

snap remove coop || true
mkdir -p snap
rm -rf snap/unpacked
unsquashfs -dest snap/unpacked "$snap_file"
snap try --classic snap/unpacked
