#!/bin/bash

# Install Horizon Supervisor Script
# Run this script with sudo to install and configure Supervisor for Laravel Horizon

set -e

echo "Installing Supervisor..."
sudo apt update
sudo apt install -y supervisor

echo "Copying Horizon supervisor config..."
sudo cp /opt/projects/ezscale_site/ezscale-horizon.conf /etc/supervisor/conf.d/

echo "Creating log directory..."
sudo mkdir -p /opt/projects/ezscale_site/website/storage/logs
sudo chown -R ezscale:ezscale /opt/projects/ezscale_site/website/storage/logs

echo "Reloading supervisor configuration..."
sudo supervisorctl reread
sudo supervisorctl update

echo "Starting Horizon..."
sudo supervisorctl start ezscale-horizon

echo "Checking status..."
sudo supervisorctl status ezscale-horizon

echo ""
echo "✓ Horizon supervisor installed successfully!"
echo ""
echo "Useful commands:"
echo "  sudo supervisorctl status ezscale-horizon    # Check status"
echo "  sudo supervisorctl stop ezscale-horizon      # Stop Horizon"
echo "  sudo supervisorctl start ezscale-horizon     # Start Horizon"
echo "  sudo supervisorctl restart ezscale-horizon   # Restart Horizon"
echo "  sudo supervisorctl tail ezscale-horizon      # View logs"
echo ""
