#!/bin/sh
# Bridges Claude's stdio to the Nisshi app's local listener (127.0.0.1:35468).
#
# If Nisshi isn't running, open it in the background and give it a few
# seconds to start listening. Without this, Claude reports "Server
# disconnected" whenever Nisshi happens to be closed, which reads like
# something broke. Nisshi must have Connect an App turned on in Settings,
# Connections; it starts listening at launch, with or without a window.
BUNDLE_ID="${NISSHI_BUNDLE_ID:-com.oltalabs.nisshi}"
if ! /usr/bin/nc -z 127.0.0.1 35468 2>/dev/null; then
  /usr/bin/open -g -b "$BUNDLE_ID" 2>/dev/null
  i=0
  while [ "$i" -lt 30 ] && ! /usr/bin/nc -z 127.0.0.1 35468 2>/dev/null; do
    sleep 0.5
    i=$((i + 1))
  done
fi
exec /usr/bin/nc 127.0.0.1 35468
