#!/usr/bin/bash
#
# Suricatavel CLI Wrapper
#
# This script provides a convenient way to invoke the Suricatavel CLI
#
# Usage: suricatavel [group] [action] [target] [options]
#
# Examples:
#   suricatavel                        # Interactive mode
#   suricatavel status                 # Show system status
#   suricatavel workers status         # Show workers status
#   suricatavel stop kafka             # Stop kafka consumer
#   suricatavel restart events         # Restart events worker
#   suricatavel update                 # Update Suricatavel
#   suricatavel deploy --migrate       # Run deployment tasks
#

set -e

# Determine the script's real location (resolve symlinks)
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# Find the Suricatavel application root
# Priority: 1) /var/opt/suricatavel/current 2) /var/opt/suricatavel 3) relative to script location
if [ -f "/var/opt/suricatavel/current/artisan" ]; then
    APP_DIR="/var/opt/suricatavel/current"
elif [ -f "/var/opt/suricatavel/artisan" ]; then
    APP_DIR="/var/opt/suricatavel"
elif [ -f "$SCRIPT_DIR/../../artisan" ]; then
    APP_DIR="$( cd "$SCRIPT_DIR/../.." && pwd )"
elif [ -f "$SCRIPT_DIR/../artisan" ]; then
    APP_DIR="$( cd "$SCRIPT_DIR/.." && pwd )"
else
    echo "Error: Could not find Suricatavel application root" >&2
    exit 1
fi

# Change to application directory
cd "$APP_DIR"

# Execute the artisan command
exec php artisan suricatavel "$@"
