set -euo pipefail
WORKTREE_PATH="${1:?Usage: $0 <worktree-path> [--go-only]}"
GO_ONLY="${2:-}"
MAIN_WORKTREE="$(git -C "$WORKTREE_PATH" worktree list --porcelain | head -1 | sed 's/^worktree //')"
if [ ! -d "$WORKTREE_PATH" ]; then
echo "ERROR: Worktree path does not exist: $WORKTREE_PATH"
exit 1
fi
cd "$WORKTREE_PATH"
echo "==> Setting up worktree at $WORKTREE_PATH"
echo "==> Downloading Go dependencies..."
go mod download
if [ "$GO_ONLY" != "--go-only" ]; then
echo "==> Installing frontend dependencies..."
(cd ui && npm ci --prefer-offline --no-audit 2>/dev/null || npm ci)
else
echo "==> Skipping frontend setup (--go-only)"
fi
mkdir -p data
if [ ! -f navidrome.toml ] && [ -f "$MAIN_WORKTREE/navidrome.toml" ]; then
echo "==> Copying navidrome.toml from main worktree..."
cp "$MAIN_WORKTREE/navidrome.toml" navidrome.toml
fi
if [ ! -f data/navidrome.db ] && [ -f "$MAIN_WORKTREE/data/navidrome.db" ]; then
echo "==> Copying database from main worktree (pre-migrated, pre-scanned)..."
cp "$MAIN_WORKTREE/data/navidrome.db" data/navidrome.db
fi
echo "==> Worktree setup complete: $WORKTREE_PATH"