#!/bin/bash

# Megacubo launcher wrapper with automatic sandbox detection
# This script checks if chrome-sandbox is available and properly configured
# If not, it runs Megacubo with --no-sandbox flag

DIR="$(dirname "$(readlink -f "$0")")"
SANDBOX="$DIR/chrome-sandbox"
BINARY="$DIR/megacubo.real"

# If megacubo.real doesn't exist, this IS the real binary
if [ ! -f "$BINARY" ]; then
	BINARY="$DIR/megacubo"
fi

# Detect if we need --no-sandbox
USE_NO_SANDBOX=""
if [ -f "$SANDBOX" ]; then
	# Check if sandbox has SUID bit set (octal bit 4 on owner = 4755)
	if ! [ -u "$SANDBOX" ]; then
		# Sandbox exists but no SUID bit
		USE_NO_SANDBOX="--no-sandbox"
	fi
else
	# Sandbox doesn't exist
	USE_NO_SANDBOX="--no-sandbox"
fi

# Execute with detected flags
exec "$BINARY" $USE_NO_SANDBOX "$@"
