#! /usr/bin/env bash

set -o physical

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

bundle_dir="$(cd "${script_dir}/../Prefix"; pwd)"

echo "Starting NetRadiant in ${bundle_dir}"

cd "${bundle_dir}"

export FC_DEBUG=1024

# if bundled
if [ -d "${bundle_dir}/share/icons/hicolor" ]
then
	export FONTCONFIG_PATH="${bundle_dir}/etc/fonts"

	# export GDK_GL_LIBGL_PATH="${bundle_dir}/lib/libGL.1.dylib"
	# export GDK_GL_LIBGLU_PATH="${bundle_dir}/lib/libGLU.1.dylib"

	export GDK_PATH="${bundle_dir}"

	export GDK_PIXBUF_MODULE_FILE="${bundle_dir}/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache"

	export GTK_IM_MODULE_FILE="${bundle_dir}/lib/gtk-2.0/2.10.0/immodules.cache"

	export GOBJECT_PATH="${bundle_dir}"

	export GTK_DATA_PREFIX="${bundle_dir}"
	export GTK_EXE_PREFIX="${bundle_dir}"
	export GTK_PATH="${bundle_dir}"
fi

color="$(defaults read -g AppleInterfaceStyle 2>/dev/null)"
case "${color}" in
	'Dark')
		;;
	*)
	color='Light'
		;;
esac

gtkrc_file="${bundle_dir}/share/themes/Mojave-${color}/gtk-2.0/gtkrc"

if [ -f "${gtkrc_file}" ]
then
	export GTK2_RC_FILES="${gtkrc_file}"
fi

if [ "${1}" = '--debug' ]
then
	shift
	export PATH="/usr/bin:${PATH}"
	exec lldb ./netradiant "${@}"
else
	exec ./netradiant "${@}"
fi
