#!/bin/bash
#
# Church Music Database - Systemd Setup Script
# This script installs and enables systemd services for automatic startup
#
# Usage: sudo ./systemd-setup.sh
#

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${RED}Error: This script must be run as root (use sudo)${NC}"
    exit 1
fi

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Church Music Database - Systemd Setup${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""

PROJECT_DIR="/media/pts/Website/Church_HOP_MusicData"
BACKEND_SERVICE="church-music-backend.service"
FRONTEND_SERVICE="church-music-frontend.service"
SYSTEMD_DIR="/etc/systemd/system"

# Step 1: Stop any existing instances
echo -e "${YELLOW}[1/7] Stopping existing services (if running)...${NC}"
systemctl stop $BACKEND_SERVICE 2>/dev/null || true
systemctl stop $FRONTEND_SERVICE 2>/dev/null || true
echo -e "${GREEN}✓ Existing services stopped${NC}"
echo ""

# Step 2: Build frontend production files
echo -e "${YELLOW}[2/7] Building frontend production files...${NC}"
cd $PROJECT_DIR/frontend
sudo -u pts npm run build
echo -e "${GREEN}✓ Frontend built successfully${NC}"
echo ""

# Step 3: Copy service files to systemd directory
echo -e "${YELLOW}[3/7] Installing systemd service files...${NC}"
cp $PROJECT_DIR/$BACKEND_SERVICE $SYSTEMD_DIR/
cp $PROJECT_DIR/$FRONTEND_SERVICE $SYSTEMD_DIR/
chmod 644 $SYSTEMD_DIR/$BACKEND_SERVICE
chmod 644 $SYSTEMD_DIR/$FRONTEND_SERVICE
echo -e "${GREEN}✓ Service files installed${NC}"
echo ""

# Step 4: Reload systemd daemon
echo -e "${YELLOW}[4/7] Reloading systemd daemon...${NC}"
systemctl daemon-reload
echo -e "${GREEN}✓ Systemd daemon reloaded${NC}"
echo ""

# Step 5: Enable services (start on boot)
echo -e "${YELLOW}[5/7] Enabling services for automatic startup...${NC}"
systemctl enable $BACKEND_SERVICE
systemctl enable $FRONTEND_SERVICE
echo -e "${GREEN}✓ Services enabled for automatic startup${NC}"
echo ""

# Step 6: Start services
echo -e "${YELLOW}[6/7] Starting services...${NC}"
systemctl start $BACKEND_SERVICE
sleep 3
systemctl start $FRONTEND_SERVICE
sleep 2
echo -e "${GREEN}✓ Services started${NC}"
echo ""

# Step 7: Verify status
echo -e "${YELLOW}[7/7] Verifying service status...${NC}"
echo ""
echo -e "${GREEN}Backend Service Status:${NC}"
systemctl status $BACKEND_SERVICE --no-pager -l || true
echo ""
echo -e "${GREEN}Frontend Service Status:${NC}"
systemctl status $FRONTEND_SERVICE --no-pager -l || true
echo ""

# Final summary
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Installation Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${GREEN}Services installed and started successfully!${NC}"
echo ""
echo -e "${YELLOW}Quick Reference Commands:${NC}"
echo ""
echo "  # Check status"
echo "  sudo systemctl status church-music-backend"
echo "  sudo systemctl status church-music-frontend"
echo ""
echo "  # View logs"
echo "  sudo journalctl -u church-music-backend -f"
echo "  sudo journalctl -u church-music-frontend -f"
echo ""
echo "  # Restart services"
echo "  sudo systemctl restart church-music-backend"
echo "  sudo systemctl restart church-music-frontend"
echo ""
echo "  # Stop services"
echo "  sudo systemctl stop church-music-backend"
echo "  sudo systemctl stop church-music-frontend"
echo ""
echo "  # Disable auto-start"
echo "  sudo systemctl disable church-music-backend"
echo "  sudo systemctl disable church-music-frontend"
echo ""
echo -e "${GREEN}Backend API: http://localhost:8080${NC}"
echo -e "${GREEN}Frontend UI: http://localhost:5100${NC}"
echo ""
echo -e "${YELLOW}Note: Services will automatically start on every reboot${NC}"
echo ""
