#!/bin/bash
# CarePulse Edge installer — Ubuntu/Debian mini-PC or VM inside the hospital network.
# Usage: sudo ./install.sh <PAIRING-CODE> <https://carepulse-url>
set -e
CODE="$1"; SERVER="$2"
[ -z "$CODE" ] || [ -z "$SERVER" ] && { echo "Usage: sudo ./install.sh SHIVA-123456 https://monitor.hospital.com"; exit 1; }
apt-get update -y && apt-get install -y python3 python3-venv
id carepulse >/dev/null 2>&1 || useradd --system --home /var/lib/carepulse-edge --shell /usr/sbin/nologin carepulse
mkdir -p /opt/carepulse-edge /etc/carepulse /var/lib/carepulse-edge
cp -r carepulse_edge /opt/carepulse-edge/
[ -f /etc/carepulse/edge.json ] || cp edge.sample.json /etc/carepulse/edge.json
python3 -m venv /opt/carepulse-edge/venv
/opt/carepulse-edge/venv/bin/pip install --quiet paho-mqtt || true
cd /opt/carepulse-edge && /opt/carepulse-edge/venv/bin/python -m carepulse_edge --config /etc/carepulse/edge.json --pair "$CODE" --server "$SERVER"
touch /var/log/carepulse-edge.log
chown -R carepulse /var/lib/carepulse-edge /etc/carepulse /var/log/carepulse-edge.log
cat > /etc/systemd/system/carepulse-edge.service <<UNIT
[Unit]
Description=CarePulse Edge gateway (HL7 / Modbus / ECG folder / MQTT)
After=network-online.target
Wants=network-online.target
[Service]
User=carepulse
WorkingDirectory=/opt/carepulse-edge
ExecStart=/opt/carepulse-edge/venv/bin/python -m carepulse_edge --config /etc/carepulse/edge.json
Restart=always
RestartSec=5
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload && systemctl enable --now carepulse-edge
echo "Installed. Edit /etc/carepulse/edge.json (Modbus, folders, ADT out), then: systemctl restart carepulse-edge"
echo "Logs: journalctl -u carepulse-edge -f"
