#!/usr/bin/env bash
# Nautilus crash workaround for Hytale Launcher
# This script prevents nautilus from crashing when launched by the Hytale Launcher

# Check if we're being called with problematic arguments
# If so, silently exit successfully instead of crashing
if [[ "$*" == *"--select"* ]] || [[ "$*" == *"file://"* ]]; then
    # Try to find the real nautilus
    REAL_NAUTILUS=""
    for path in /usr/bin/nautilus /bin/nautilus; do
        if [[ -x "$path" ]] && [[ "$path" != "$0" ]]; then
            REAL_NAUTILUS="$path"
            break
        fi
    done

    if [[ -n "$REAL_NAUTILUS" ]]; then
        # Attempt to run real nautilus, but don't fail if it crashes
        "$REAL_NAUTILUS" "$@" 2>/dev/null || true
    fi
    exit 0
fi

# For other calls, try to find and execute the real nautilus
for path in /usr/bin/nautilus /bin/nautilus; do
    if [[ -x "$path" ]] && [[ "$path" != "$0" ]]; then
        exec "$path" "$@"
    fi
done

# If no nautilus found, exit silently
exit 0
