#!/usr/bin/env bash
# Quick fix: install jimp and restart (logo printing). Run with sudo.
set -euo pipefail
INSTALL_DIR="/opt/siraya/printer"
PORT=5000

if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
  echo "Run: sudo bash $0"
  exit 1
fi

echo "→ Installing dependencies in $INSTALL_DIR"
cd "$INSTALL_DIR"
npm ci --omit=dev

echo "→ Restarting siraya-printer"
systemctl restart siraya-printer.service
sleep 2

echo "→ Logo diagnostics"
curl -s "http://127.0.0.1:${PORT}/api/branding/logo/status" | node -e "
let d=''; process.stdin.on('data',c=>d+=c);
process.stdin.on('end',()=>{
  const j=JSON.parse(d||'{}');
  console.log('  jimp installed:', j.jimpInstalled);
  console.log('  logo file:', j.logoExists ? j.logoPath : 'missing');
  console.log('  print ready:', j.printReady, '(' + (j.escposBytes||0) + ' ESC/POS bytes)');
  if (!j.printReady) process.exit(1);
});
"

echo "Logo printing should work now. Run a test print from the designer."
