#!/usr/bin/env bash

# omarchy:summary=Install Socket Firewall and route npm, npx, yarn, pnpm, pip, pip3 and uv through it
# omarchy:requires-sudo=true

# Based: sfw ships only on npm, so it comes in on asdf's Node.js. One copy of
# default/socket/sfw-shim per package manager lands in
# ~/.local/share/omarchy/sfw-shims, which goes first on PATH for new shells;
# each copy strips its own directory from PATH, finds the real command and
# runs it through sfw, or straight when sfw is missing or OMARCHY_SFW_DISABLE
# is set, so an install never breaks on the firewall.

set -eo pipefail

asdf_data=${ASDF_DATA_DIR:-$HOME/.asdf}
omarchy-pkg-add asdf
export PATH="$asdf_data/shims:$PATH"

echo -e "Installing Socket Firewall...\n"
omarchy-dev-env-tool nodejs
npm install -g sfw
asdf reshim nodejs

shims="$HOME/.local/share/omarchy/sfw-shims"
mkdir -p "$shims"
for cmd in npm npx yarn pnpm pip pip3 uv; do
  install -m 0755 "$OMARCHY_PATH/default/socket/sfw-shim" "$shims/$cmd"
done

line='export PATH="$HOME/.local/share/omarchy/sfw-shims:$PATH"'
grep -qF "$line" ~/.bashrc 2>/dev/null || echo "$line" >>~/.bashrc
echo -e "\nThe shims take effect in new shells."
