#!/usr/bin/env bash
set -euo pipefail

src="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/local-ai-coding-tools"
dst="/etc/apparmor.d/local-ai-coding-tools"

if [[ ! -f "$src" ]]; then
  echo "Profile not found: $src" >&2
  exit 1
fi

echo "Installing AppArmor profile:"
echo "  $src"
echo "  -> $dst"
echo
echo "This does not restart ssh, networking, VS Code, or the machine."
echo "It only loads/reloads the single AppArmor profile file above."
echo

if [[ -f "$dst" ]]; then
  backup="/etc/apparmor.d/local-ai-coding-tools.$(date +%Y%m%d%H%M%S).bak"
  echo "Backing up existing profile to:"
  echo "  $backup"
  sudo cp "$dst" "$backup"
fi

sudo install -m 0644 "$src" "$dst"
sudo apparmor_parser -r "$dst"

echo
echo "Loaded $dst"
echo "Restart Codex/VS Code extension hosts for already-running processes to pick up the new exec profiles."
