#!/bin/bash
# Rai Printing launcher for macOS / Linux. Double-click to run (you may need to
# `chmod +x start-rai-printing.command` once, or right-click → Open the first time).
cd "$(dirname "$0")" || exit 1
echo "=================================================="
echo "   Rai Printing - keep this window open while you bill"
echo "=================================================="
echo

# Get the bridge if it isn't next to this file yet
if [ ! -f rai-print-bridge.js ]; then
  echo "Downloading the print bridge..."
  curl -fsSL -o rai-print-bridge.js https://www.raikitchen.in/rai-print-bridge.js || {
    echo "Could not download. Check your internet connection and try again."
    read -n1 -r -p "Press any key to close..."
    exit 1
  }
fi

# Make sure Node.js is available
if ! command -v node >/dev/null 2>&1; then
  echo "Node.js is not installed."
  if command -v brew >/dev/null 2>&1; then
    echo "Installing Node.js via Homebrew..."
    brew install node
  else
    echo "Please install Node.js from https://nodejs.org, then run this again."
    command -v open >/dev/null 2>&1 && open "https://nodejs.org/en/download"
    read -n1 -r -p "Press any key to close..."
    exit 1
  fi
fi

echo "Starting Rai Print Bridge..."
echo "In Rai, go to Printing and set the bridge URL to http://127.0.0.1:7777"
echo
node rai-print-bridge.js
