#!/bin/sh

# Resolve symlinks to find the real script location
# This allows Xonotic.app to be symlinked (e.g., from /Applications) while
# the actual app bundle remains in the Xonotic folder with the data files
# Uses a loop with readlink (no flags) for compatibility with all macOS versions
SCRIPT_DIR=$(
    script="$0"
    cd "$(dirname "$script")"
    script="$(basename "$script")"
    while [ -L "$script" ]; do
        script="$(readlink "$script")"
        cd "$(dirname "$script")"
        script="$(basename "$script")"
    done
    pwd -P
)

# if we ever want to remove this script, install_name_tool can be used to replace the DYLD_LIBRARY_PATH hack
export DYLD_LIBRARY_PATH="$SCRIPT_DIR"
	# same dir as the executable

# Basedir is 3 levels up from MacOS folder (MacOS -> Contents -> Xonotic.app -> Xonotic)
BASEDIR="$SCRIPT_DIR/../../.."

set -- "$SCRIPT_DIR/xonotic-macos-sdl-bin" "$@" -notexturenonpoweroftwo -basedir "$BASEDIR"

case "$(sw_vers -productVersion)" in
	# OSX Lion up to 10.7.2 has problems with SDL and running fullscreen, hence we force windowed mode
	10.7.[0-2])
		exec "$@" +vid_fullscreen 0
		;;
	# shell pattern for "10.6 and higher"
	10.[6-9]*|10.[1-5][0-9]*|1[1-9]*|[2-9]*)
		# no workaround needed on 10.6+
		exec "$@"
		;;
	*)
		# need to force a 32bit arch on 10.5 and below
		exec arch -arch i386 -arch ppc "$@"
		;;
esac
