#!/bin/bash
# ==============================================================================
# Script: soe-service-init
# Description:
#   This script detects the init system (systemd or SysVinit) and enables or
#   disables a startup script based on the provided arguments.
#
# Usage:
#   ./soe-service-init <program_name> <action>
#
# Arguments:
#   <program_name> - The base name of the service file (e.g., "myservice").
#                    The script assumes the source file is named
#                    <program_name>.service for systemd or <program_name>
#                    for SysVinit.
#   <action>       - "enable" to enable the service at startup.
#                    "disable" to disable the service from starting automatically.
#
# Assumptions:
#   - This script must be run with root privileges (sudo).
# ==============================================================================

# --- Function to display usage information ---
show_usage() {
    echo "Usage: $0 <program_name> <action>"
    echo "  <program_name>  : The name of the service (e.g., 'myservice')"
    echo "  <action>        : 'enable' or 'disable'"
    echo ""
    echo "Example: $0 myservice enable"
    exit 1
}

# Check if exactly two arguments were provided.
if [ "$#" -ne 2 ]; then
    echo "Error: Incorrect number of arguments."
    show_usage
fi

PROGRAM_NAME=$1
ACTION=$2

# Validate the action argument.
if [ "$ACTION" != "enable" ] && [ "$ACTION" != "disable" ]; then
    echo "Error: Invalid action. Action must be 'enable' or 'disable'."
    show_usage
fi

# We'll check for the /run/systemd/system/ directory to detect systemd.
# This is a reliable and common method.
if [ -d /run/systemd/system/ ]; then
    INIT_SYSTEM="systemd"
    echo "Detected init system: systemd"
else
    INIT_SYSTEM="sysvinit"
    echo "Detected init system: SysVinit"
fi

# Build the source directory path
SOURCE_DIR="/usr/share/doc/soe-utils"

# Check if the source directory exists.
if [ ! -d "$SOURCE_DIR" ]; then
    echo "Error: Source directory not found: $SOURCE_DIR"
    echo "Please make sure your service files are in a subdirectory named '$PROGRAM_NAME'."
    exit 1
fi

if [ "$INIT_SYSTEM" == "systemd" ]; then
    SOURCE_FILE="$SOURCE_DIR/templates/systemd.service.tpl"
    DEST_DIR="/etc/systemd/system"
    DEST_FILE="$DEST_DIR/$PROGRAM_NAME.service"
else
    SOURCE_FILE="$SOURCE_DIR/templates/sysvinit.tpl"
    DEST_DIR="/etc/init.d"
    DEST_FILE="$DEST_DIR/$PROGRAM_NAME"
fi

# Check if the source file exists.
if [ ! -f "$SOURCE_FILE" ]; then
    echo "Error: Source file not found: $SOURCE_FILE"
    echo "Please make sure your service file exists in the same directory."
    exit 1
fi

# --- 4. Perform the Action ---
echo "Executing '$ACTION' for service '$PROGRAM_NAME'..."

if [ "$INIT_SYSTEM" == "systemd" ]; then
    # --- systemd actions ---
    if [ "$ACTION" == "enable" ]; then
        echo "Copying service file to $DEST_DIR..."
        cp -f "$SOURCE_FILE" "$DEST_DIR/"
        echo "Enabling service with systemctl..."
        systemctl daemon-reload
        systemctl enable "$PROGRAM_NAME.service"
        echo "Service enabled successfully."
    elif [ "$ACTION" == "disable" ]; then
        echo "Disabling service with systemctl..."
        systemctl disable "$PROGRAM_NAME.service"
        echo "Removing service file from $DEST_DIR..."
        rm -f "$DEST_FILE"
        systemctl daemon-reload
        echo "Service disabled successfully."
    fi

else
    # --- SysVinit actions ---
    if [ "$ACTION" == "enable" ]; then
        echo "Copying script to $DEST_DIR..."
        cp -f "$SOURCE_FILE" "$DEST_DIR/"
        chmod +x "$DEST_FILE"
        echo "Enabling script with update-rc.d or chkconfig..."
        if command -v update-rc.d &> /dev/null; then
            update-rc.d "$PROGRAM_NAME" defaults
        elif command -v chkconfig &> /dev/null; then
            chkconfig --add "$PROGRAM_NAME"
        else
            echo "Warning: Neither update-rc.d nor chkconfig found. Please enable the service manually."
        fi
        echo "Service enabled successfully."
    elif [ "$ACTION" == "disable" ]; then
        echo "Disabling script with update-rc.d or chkconfig..."
        if command -v update-rc.d &> /dev/null; then
            update-rc.d -f "$PROGRAM_NAME" remove
        elif command -v chkconfig &> /dev/null; then
            chkconfig --del "$PROGRAM_NAME"
        else
            echo "Warning: Neither update-rc.d nor chkconfig found. Please disable the service manually."
        fi
        echo "Removing script from $DEST_DIR..."
        rm -f "$DEST_FILE"
        echo "Service disabled successfully."
    fi
fi

echo "Operation completed."
