#!/bin/bash
# Install QuickBooks POS Help Server as a system service

set -e

echo "=========================================="
echo "QuickBooks POS Help Service Installer"
echo "=========================================="

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
   echo "Please run with sudo: sudo bash install_service.sh"
   exit 1
fi

SERVICE_FILE="qbpos-help.service"
SYSTEMD_PATH="/etc/systemd/system/"

echo "1. Stopping old server processes..."
pkill -9 -f production_server.py 2>/dev/null || true
pkill -9 -f secure_production_server.py 2>/dev/null || true

echo "2. Making server script executable..."
chmod +x /home/pts/Documents/QBPOS_Help_Web/secure_production_server.py

echo "3. Copying service file to systemd..."
cp "$SERVICE_FILE" "$SYSTEMD_PATH"

echo "4. Reloading systemd..."
systemctl daemon-reload

echo "5. Enabling service to start on boot..."
systemctl enable qbpos-help.service

echo "6. Starting service..."
systemctl start qbpos-help.service

echo ""
echo "=========================================="
echo "Installation Complete!"
echo "=========================================="
echo ""
echo "Service Status:"
systemctl status qbpos-help.service --no-pager

echo ""
echo "Useful Commands:"
echo "  Start:    sudo systemctl start qbpos-help"
echo "  Stop:     sudo systemctl stop qbpos-help"
echo "  Restart:  sudo systemctl restart qbpos-help"
echo "  Status:   sudo systemctl status qbpos-help"
echo "  Logs:     sudo journalctl -u qbpos-help -f"
echo ""
echo "The server will now auto-start on system reboot."
