#!/bin/bash

# =================================================================
# Bushnell Northstar / Meade Autostar Telescope Control Script
# =================================================================
# Controls a telescope via serial port using Arrow Keys and Number Keys.
# Protocol: Meade LX200 / Autostar Generic
#
# INTERACTIVE MODE:
#   Arrow Keys : Start moving (North/South/East/West)
#   Spacebar   : STOP Moving (Important!)
#   1-9        : Set Speed (1=Slowest/Guide, 9=Fastest/Slew)
#   q          : Quit the program
#
# COMMAND-LINE MODE:
#   -u|--up       : Move North
#   -d|--down     : Move South
#   -l|--left     : Move West
#   -r|--right    : Move East
#   -s|--speed N  : Set speed (1-9)
#   -t|--time N   : Move duration in seconds
#   -p|--port DEV : Serial port device
#   --scan        : Auto-scan for telescope
#   --debug       : Raw data monitor mode
#   --stop        : Send stop command
#   -h|--help     : Show help
# =================================================================

# SAFETY CHECK: Ensure we are running in Bash, not SH
if [ -z "$BASH_VERSION" ]; then
    echo "ERROR: This script requires BASH."
    echo "Please run it using: sudo bash $0"
    exit 1
fi

# Default Serial Port (can be changed by user input)
SERIAL_PORT="/dev/ttyS0"

# Command-line mode flags
CLI_MODE=0
CLI_DIRECTION=""
CLI_SPEED=""
CLI_DURATION=""
CLI_SCAN=0
CLI_DEBUG=0
CLI_STOP=0

# Function to show help
show_help() {
    cat << EOF
Telescope Control Script - Usage Guide
=======================================

INTERACTIVE MODE (no arguments):
  Launches interactive keyboard control interface

COMMAND-LINE MODE:

Directional Controls:
  -u, --up          Move telescope North
  -d, --down        Move telescope South
  -l, --left        Move telescope West
  -r, --right       Move telescope East

Speed Control:
  -s, --speed N     Set speed (1-9)
                    1-2: Guide (Slowest/Nudge)
                    3-5: Center (Slow)
                    6-8: Find (Medium)
                    9:   Slew (Max Speed)

Timing:
  -t, --time N      Movement duration in seconds (default: 1)

Port Configuration:
  -p, --port DEV    Specify serial port (e.g., /dev/ttyS0)
  --scan            Auto-scan ports for telescope

Utility Commands:
  --stop            Send emergency stop command
  --debug           Launch raw data monitor for troubleshooting
  -h, --help        Show this help message

EXAMPLES:

  # Interactive mode (default)
  sudo bash $0

  # Move North at speed 5 for 2 seconds
  sudo bash $0 --up --speed 5 --time 2

  # Move West at max speed for 3 seconds on specific port
  sudo bash $0 -l -s 9 -t 3 -p /dev/ttyUSB0

  # Auto-scan for telescope
  sudo bash $0 --scan

  # Debug mode on specific port
  sudo bash $0 --debug --port /dev/ttyS0

  # Emergency stop
  sudo bash $0 --stop --port /dev/ttyUSB0

EOF
    exit 0
}

# Function to monitor raw data for debugging
debug_raw_monitor() {
    local port="$1"
    echo ""
    echo "====================================================="
    echo "  RAW DATA MONITOR (Debug Mode)"
    echo "====================================================="
    echo "  Monitoring port: $port"
    echo "  Settings: 9600 baud, 8N1, Raw, No Flow Control"
    echo "-----------------------------------------------------"
    echo "  Sending test command (:GR#) every 2 seconds..."
    echo "  Incoming data will be shown as HEXADECIMAL values."
    echo "  - 0000        : Null bytes (line noise)"
    echo "  - 3a47 5223   : Valid ASCII text"
    echo "-----------------------------------------------------"
    echo "  Press Ctrl+C to Stop Monitoring and Exit."
    echo "====================================================="

    # Configure port explicitly
    # Added -crtscts (no hardware flow control) and -hupcl (no hangup/DTR reset)
    stty -F "$port" 9600 cs8 -cstopb -parenb raw -echo -crtscts clocal -hupcl

    # Start a background loop to ping the telescope so we provoke a response
    # We use a subshell to keep it contained
    (
        while true; do
            echo -ne ":GR#" > "$port" 2>/dev/null
            sleep 2
        done
    ) &
    BG_PID=$!

    # Ensure we kill the background pinger when this function/script stops
    trap "kill $BG_PID 2>/dev/null; exit" INT TERM EXIT

    # Listen to the port and dump hex
    # We use 'od -x' (octal dump -hex) to see exactly what bytes come in
    od -x < "$port"
}

# Function to attempt to auto-detect the telescope
scan_ports() {
    echo "Scanning ports for telescope response (sending :GR#)..."
    FOUND_PORT=""

    # Check common serial device patterns
    for port in /dev/ttyUSB* /dev/ttyS[0-9]; do
        # Check if port exists
        if [ -e "$port" ]; then

            # Try 9600 baud first (Standard Meade)
            # Added clocal -crtscts -hupcl to prevent telescope resets
            if stty -F "$port" 9600 cs8 -cstopb -parenb raw -echo -crtscts clocal -hupcl 2>/dev/null; then

                # Clear any existing data in buffer
                timeout 0.2s cat "$port" >/dev/null 2>&1

                # Send "Get RA" command
                echo -ne ":GR#" > "$port" 2>/dev/null

                # Try to read response
                RESPONSE=$(timeout 1s head -c 10 "$port" 2>/dev/null | tr -d '\0')

                if [[ "$RESPONSE" == *"#"* ]] || [[ "$RESPONSE" =~ [0-9] ]]; then
                    echo "  [FOUND] Telescope detected on $port (Response: $RESPONSE)"
                    FOUND_PORT="$port"
                else
                    echo "  [ ... ] Checked $port (9600) - No valid response"
                fi
            fi

            # If not found, try 19200 baud (Some Northstar clones use this)
            if [ -z "$FOUND_PORT" ]; then
                 stty -F "$port" 19200 cs8 -cstopb -parenb raw -echo -crtscts clocal -hupcl 2>/dev/null
                 timeout 0.2s cat "$port" >/dev/null 2>&1
                 echo -ne ":GR#" > "$port" 2>/dev/null
                 RESPONSE=$(timeout 1s head -c 10 "$port" 2>/dev/null | tr -d '\0')

                 if [[ "$RESPONSE" == *"#"* ]] || [[ "$RESPONSE" =~ [0-9] ]]; then
                    echo "  [FOUND] Telescope detected on $port (19200 baud) (Response: $RESPONSE)"
                    FOUND_PORT="$port"
                    # Reconfigure main script variable if found at different baud
                    stty -F "$port" 19200 cs8 -cstopb -parenb raw -echo -crtscts clocal -hupcl 2>/dev/null
                 fi
            fi
        fi
    done

    if [ ! -z "$FOUND_PORT" ]; then
        echo ""
        echo "Auto-selected likely port: $FOUND_PORT"
        SERIAL_PORT="$FOUND_PORT"
    else
        echo "Scan complete. No obvious telescope response found."

        # Fallback Monitor Prompt
        echo ""
        echo "(!) Connection Troubleshooting:"
        echo "    If the telescope RESETS when scanned, the DTR line is likely active."
        echo "    This script now attempts to disable DTR (-hupcl)."
        read -p "    Start Raw Data Monitor to debug? (y/n): " RUN_DEBUG

        if [[ "$RUN_DEBUG" =~ ^[Yy]$ ]]; then
             # Ask which port to monitor since we didn't find one
             echo ""
             echo "Available Ports:"
             ls /dev/ttyS* /dev/ttyUSB* 2>/dev/null
             read -p "Enter Port to Monitor [default: /dev/ttyS0]: " DEBUG_PORT
             if [ -z "$DEBUG_PORT" ]; then DEBUG_PORT="/dev/ttyS0"; fi

             if [ -e "$DEBUG_PORT" ]; then
                 debug_raw_monitor "$DEBUG_PORT"
                 exit 0 # Exit script after monitoring
             else
                 echo "Error: Port $DEBUG_PORT does not exist."
                 exit 1
             fi
        fi
    fi
}

# Function to send telescope command
send_command() {
    local port="$1"
    local cmd="$2"
    echo -ne "$cmd" > "$port"
}

# Function to set speed
set_speed() {
    local port="$1"
    local speed="$2"

    case "$speed" in
        1|2)
            send_command "$port" ":RG#"  # Guide Rate
            ;;
        3|4|5)
            send_command "$port" ":RC#"  # Center Rate
            ;;
        6|7|8)
            send_command "$port" ":RM#"  # Find Rate
            ;;
        9)
            send_command "$port" ":RS#"  # Slew Rate
            ;;
        *)
            echo "Invalid speed: $speed (must be 1-9)"
            exit 1
            ;;
    esac
}

# Function to execute CLI command
execute_cli_command() {
    local port="$1"
    local direction="$2"
    local speed="$3"
    local duration="$4"

    # Configure port
    stty -F "$port" 9600 cs8 -cstopb -parenb raw -echo -crtscts clocal -hupcl 2>/dev/null

    # Set speed if specified
    if [ ! -z "$speed" ]; then
        echo "Setting speed to: $speed"
        set_speed "$port" "$speed"
        sleep 0.2
    fi

    # Send movement command
    case "$direction" in
        up|north)
            echo "Moving North for ${duration}s..."
            send_command "$port" ":Mn#"
            ;;
        down|south)
            echo "Moving South for ${duration}s..."
            send_command "$port" ":Ms#"
            ;;
        left|west)
            echo "Moving West for ${duration}s..."
            send_command "$port" ":Mw#"
            ;;
        right|east)
            echo "Moving East for ${duration}s..."
            send_command "$port" ":Me#"
            ;;
        *)
            echo "Invalid direction: $direction"
            exit 1
            ;;
    esac

    # Wait for specified duration
    sleep "$duration"

    # Send stop command
    echo "Stopping..."
    send_command "$port" ":Q#"
    echo "Done."
}

# -----------------------------------------------------------------
# Parse Command-Line Arguments
# -----------------------------------------------------------------

while [[ $# -gt 0 ]]; do
    case "$1" in
        -h|--help)
            show_help
            ;;
        -u|--up)
            CLI_MODE=1
            CLI_DIRECTION="up"
            shift
            ;;
        -d|--down)
            CLI_MODE=1
            CLI_DIRECTION="down"
            shift
            ;;
        -l|--left)
            CLI_MODE=1
            CLI_DIRECTION="left"
            shift
            ;;
        -r|--right)
            CLI_MODE=1
            CLI_DIRECTION="right"
            shift
            ;;
        -s|--speed)
            CLI_SPEED="$2"
            shift 2
            ;;
        -t|--time)
            CLI_DURATION="$2"
            shift 2
            ;;
        -p|--port)
            SERIAL_PORT="$2"
            shift 2
            ;;
        --scan)
            CLI_SCAN=1
            shift
            ;;
        --debug)
            CLI_DEBUG=1
            shift
            ;;
        --stop)
            CLI_STOP=1
            shift
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use -h or --help for usage information"
            exit 1
            ;;
    esac
done

# Set default duration if not specified
if [ -z "$CLI_DURATION" ]; then
    CLI_DURATION="1"
fi

# -----------------------------------------------------------------
# Handle CLI Mode Operations
# -----------------------------------------------------------------

# Debug mode
if [ "$CLI_DEBUG" -eq 1 ]; then
    if [ ! -e "$SERIAL_PORT" ]; then
        echo "Error: Port $SERIAL_PORT does not exist."
        exit 1
    fi
    debug_raw_monitor "$SERIAL_PORT"
    exit 0
fi

# Scan mode
if [ "$CLI_SCAN" -eq 1 ]; then
    scan_ports
    exit 0
fi

# Stop command
if [ "$CLI_STOP" -eq 1 ]; then
    if [ ! -e "$SERIAL_PORT" ]; then
        echo "Error: Port $SERIAL_PORT does not exist."
        exit 1
    fi
    stty -F "$SERIAL_PORT" 9600 cs8 -cstopb -parenb raw -echo -crtscts clocal -hupcl 2>/dev/null
    echo "Sending stop command to $SERIAL_PORT..."
    send_command "$SERIAL_PORT" ":Q#"
    echo "Stop command sent."
    exit 0
fi

# Movement command
if [ "$CLI_MODE" -eq 1 ]; then
    if [ -z "$CLI_DIRECTION" ]; then
        echo "Error: No direction specified."
        echo "Use -h or --help for usage information"
        exit 1
    fi

    if [ ! -e "$SERIAL_PORT" ]; then
        echo "Error: Port $SERIAL_PORT does not exist."
        exit 1
    fi

    execute_cli_command "$SERIAL_PORT" "$CLI_DIRECTION" "$CLI_SPEED" "$CLI_DURATION"
    exit 0
fi

# -----------------------------------------------------------------
# 1. Setup Serial Port (Interactive Mode)
# -----------------------------------------------------------------
echo "-----------------------------------------------------"
echo "  Telescope Control Interface (Bash)"
echo "-----------------------------------------------------"

# Run the scan function
scan_ports

echo "-----------------------------------------------------"
echo "Available Serial Ports:"
ls /dev/ttyS* /dev/ttyUSB* 2>/dev/null
echo ""
read -p "Enter Serial Port [default: $SERIAL_PORT]: " USER_PORT

if [ ! -z "$USER_PORT" ]; then
    SERIAL_PORT="$USER_PORT"
fi

if [ ! -e "$SERIAL_PORT" ]; then
    echo "Error: Port $SERIAL_PORT does not exist."
    exit 1
fi

# Configure the serial port using stty
# Added clocal -crtscts -hupcl to prevent resets
stty -F "$SERIAL_PORT" 9600 cs8 -cstopb -parenb raw -echo -crtscts clocal -hupcl

echo "Connected to $SERIAL_PORT at 9600 baud."
echo "Initializing..."

# Send a Stop command just in case
echo -ne ":Q#" > "$SERIAL_PORT"

# -----------------------------------------------------------------
# 2. Control Loop
# -----------------------------------------------------------------
CURRENT_SPEED="Max"

print_ui() {
    clear
    echo "====================================================="
    echo "  TELESCOPE CONTROL PANEL"
    echo "====================================================="
    echo "  Port: $SERIAL_PORT  |  Status: Ready"
    echo "-----------------------------------------------------"
    echo "  [ UP ]    Move North"
    echo "  [DOWN]    Move South"
    echo "  [LEFT]    Move West"
    echo "  [RGHT]    Move East"
    echo "  [SPACE]   STOP ALL MOTORS"
    echo "-----------------------------------------------------"
    echo "  Current Speed Setting: $CURRENT_SPEED"
    echo "  [1-2] Guide (Slowest/Nudge)"
    echo "  [3-5] Center (Slow)"
    echo "  [6-8] Find (Medium)"
    echo "  [9]   Slew (Max Speed)"
    echo "-----------------------------------------------------"
    echo "  Press 'q' to Quit Script"
    echo "====================================================="
}

print_ui

# Loop to capture keystrokes one by one
while true; do
    # Read 1 character silently (-s), 3 chars if it's an escape sequence
    read -rsn1 key

    # If key is empty (sometimes happens), skip
    if [[ -z "$key" ]]; then continue; fi

    # Handle Escape Sequences (Arrow Keys start with ESC)
    if [[ "$key" == $'\x1b' ]]; then
        read -rsn2 key # Read the next 2 characters
        case "$key" in
            '[A') # UP Arrow
                echo -ne ":Mn#" > "$SERIAL_PORT"
                ;;
            '[B') # DOWN Arrow
                echo -ne ":Ms#" > "$SERIAL_PORT"
                ;;
            '[D') # LEFT Arrow
                echo -ne ":Mw#" > "$SERIAL_PORT"
                ;;
            '[C') # RIGHT Arrow
                echo -ne ":Me#" > "$SERIAL_PORT"
                ;;
        esac
    else
        # Handle Standard Keys
        case "$key" in
            # SPACEBAR - The most important key! Stops the telescope.
            ' ')
                echo -ne ":Q#" > "$SERIAL_PORT"
                ;;

            # Speed Controls (Mapped to Meade Standard Rate Commands)
            '1'|'2')
                # Guide Rate (Very Slow / Nudge)
                echo -ne ":RG#" > "$SERIAL_PORT"
                CURRENT_SPEED="Guide (Slowest)"
                print_ui
                ;;
            '3'|'4'|'5')
                # Center Rate (Slow)
                echo -ne ":RC#" > "$SERIAL_PORT"
                CURRENT_SPEED="Center (Slow)"
                print_ui
                ;;
            '6'|'7'|'8')
                # Find Rate (Medium)
                echo -ne ":RM#" > "$SERIAL_PORT"
                CURRENT_SPEED="Find (Medium)"
                print_ui
                ;;
            '9')
                # Slew Rate (Fastest)
                echo -ne ":RS#" > "$SERIAL_PORT"
                CURRENT_SPEED="Slew (Max)"
                print_ui
                ;;

            # Quit Application
            'q')
                echo "Stopping telescope..."
                echo -ne ":Q#" > "$SERIAL_PORT"
                exit 0
                ;;
        esac
    fi
done
