#!/bin/bash # Exit on error set -e SERVICE_NAME="contec-forwarder" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" # Auto-detect script directory or fallback to standard path SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" if [ -f "${SCRIPT_DIR}/main.py" ]; then WORKING_DIR="${SCRIPT_DIR}" elif [ -d "/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main" ]; then WORKING_DIR="/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main" elif [ -d "/home/prathiyuman/ContecMonitor" ]; then WORKING_DIR="/home/prathiyuman/ContecMonitor" else WORKING_DIR="${PWD}" fi echo "=== Detected Working Directory: ${WORKING_DIR} ===" # Ensure virtual environment exists if [ ! -f "${WORKING_DIR}/venv/bin/python" ]; then echo "=== Virtual environment not found. Creating venv... ===" python3 -m venv "${WORKING_DIR}/venv" || sudo apt-get update && sudo apt-get install -y python3-venv python3-pip && python3 -m venv "${WORKING_DIR}/venv" fi PYTHON_EXEC="${WORKING_DIR}/venv/bin/python" SCRIPT_PATH="${WORKING_DIR}/main.py" # Ensure dependencies are installed if [ -f "${WORKING_DIR}/requirements.txt" ]; then echo "=== Installing/verifying dependencies ===" "${WORKING_DIR}/venv/bin/pip" install --upgrade pip "${WORKING_DIR}/venv/bin/pip" install -r "${WORKING_DIR}/requirements.txt" fi # Open firewall ports for Contec monitor (Port 511) and Web API (Port 8006) if command -v ufw >/dev/null 2>&1; then echo "=== Configuring firewall (UFW) for Port 511 and 8006 ===" sudo ufw allow 511/tcp comment "Contec Patient Monitor CMS TCP" || true sudo ufw allow 8006/tcp comment "Contec Forwarder HTTP API" || true fi echo "=== Creating systemd service file: ${SERVICE_FILE} ===" sudo bash -c "cat > ${SERVICE_FILE}" <