#!/bin/bash

# Backup Web Application Service Manager
# Manages the backup web application as a systemd service

set -e

SERVICE_NAME="backup-web-app"
SERVICE_FILE="/home/acedanger/shell/${SERVICE_NAME}.service"
SYSTEMD_DIR="/etc/systemd/system"
APP_USER="acedanger"

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

print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

check_root() {
    if [[ $EUID -ne 0 ]]; then
        print_error "This script must be run as root (use sudo)"
        exit 1
    fi
}

install_service() {
    print_status "Installing backup web application service..."
    
    # Check if service file exists
    if [[ ! -f "$SERVICE_FILE" ]]; then
        print_error "Service file not found: $SERVICE_FILE"
        exit 1
    fi
    
    # Copy service file to systemd directory
    cp "$SERVICE_FILE" "$SYSTEMD_DIR/"
    print_success "Service file copied to $SYSTEMD_DIR"
    
    # Reload systemd daemon
    systemctl daemon-reload
    print_success "Systemd daemon reloaded"
    
    # Enable service to start on boot
    systemctl enable "$SERVICE_NAME"
    print_success "Service enabled for auto-start on boot"
    
    print_success "Service installation completed!"
    print_status "Use 'sudo systemctl start $SERVICE_NAME' to start the service"
}

start_service() {
    print_status "Starting backup web application service..."
    systemctl start "$SERVICE_NAME"
    sleep 2
    
    if systemctl is-active --quiet "$SERVICE_NAME"; then
        print_success "Service started successfully"
        systemctl status "$SERVICE_NAME" --no-pager -l
    else
        print_error "Failed to start service"
        print_status "Check logs with: sudo journalctl -u $SERVICE_NAME -f"
        exit 1
    fi
}

stop_service() {
    print_status "Stopping backup web application service..."
    systemctl stop "$SERVICE_NAME"
    print_success "Service stopped"
}

restart_service() {
    print_status "Restarting backup web application service..."
    systemctl restart "$SERVICE_NAME"
    sleep 2
    
    if systemctl is-active --quiet "$SERVICE_NAME"; then
        print_success "Service restarted successfully"
    else
        print_error "Failed to restart service"
        exit 1
    fi
}

status_service() {
    print_status "Service status:"
    systemctl status "$SERVICE_NAME" --no-pager -l
}

logs_service() {
    print_status "Following service logs (Ctrl+C to exit):"
    journalctl -u "$SERVICE_NAME" -f
}

uninstall_service() {
    print_status "Uninstalling backup web application service..."
    
    # Stop service if running
    if systemctl is-active --quiet "$SERVICE_NAME"; then
        systemctl stop "$SERVICE_NAME"
        print_status "Service stopped"
    fi
    
    # Disable service
    if systemctl is-enabled --quiet "$SERVICE_NAME"; then
        systemctl disable "$SERVICE_NAME"
        print_status "Service disabled"
    fi
    
    # Remove service file
    if [[ -f "$SYSTEMD_DIR/${SERVICE_NAME}.service" ]]; then
        rm "$SYSTEMD_DIR/${SERVICE_NAME}.service"
        print_status "Service file removed"
    fi
    
    # Reload systemd daemon
    systemctl daemon-reload
    print_success "Service uninstalled successfully"
}

show_help() {
    echo "Backup Web Application Service Manager"
    echo
    echo "Usage: $0 {install|start|stop|restart|status|logs|uninstall|help}"
    echo
    echo "Commands:"
    echo "  install    - Install the service (requires root)"
    echo "  start      - Start the service (requires root)"
    echo "  stop       - Stop the service (requires root)"
    echo "  restart    - Restart the service (requires root)"
    echo "  status     - Show service status"
    echo "  logs       - Follow service logs"
    echo "  uninstall  - Remove the service (requires root)"
    echo "  help       - Show this help message"
    echo
    echo "Examples:"
    echo "  sudo $0 install     # Install and enable the service"
    echo "  sudo $0 start       # Start the service"
    echo "  $0 status           # Check service status"
    echo "  $0 logs             # View live logs"
}

# Main script logic
case "${1:-}" in
    install)
        check_root
        install_service
        ;;
    start)
        check_root
        start_service
        ;;
    stop)
        check_root
        stop_service
        ;;
    restart)
        check_root
        restart_service
        ;;
    status)
        status_service
        ;;
    logs)
        logs_service
        ;;
    uninstall)
        check_root
        uninstall_service
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        print_error "Invalid command: ${1:-}"
        echo
        show_help
        exit 1
        ;;
esac
