#!/bin/sh

HERE="$(dirname "$(readlink -f "${0}")")"

# Stash original environment values so they can be restored
# when switching to other mods using restore-environment
export OPENRA_ORIG_PATH="${PATH}"
export OPENRA_ORIG_XDG_DATA_DIRS="${XDG_DATA_DIRS}"
export OPENRA_ORIG_DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}"
export OPENRA_ORIG_LD_LIBRARY_PATH="${LD_LIBRARY_PATH}"
export OPENRA_ORIG_MONO_PATH="${MONO_PATH}"
export OPENRA_ORIG_MONO_CFG_DIR="${MONO_CFG_DIR}"
export OPENRA_ORIG_MONO_CONFIG="${MONO_CONFIG}"

# Override runtime paths to use bundled mono and shared libraries
export PATH="${HERE}/usr/bin:${PATH}"
export XDG_DATA_DIRS="${HERE}/usr/share:${XDG_DATA_DIRS}"
export DYLD_LIBRARY_PATH="${HERE}/usr/lib:${DYLD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${HERE}/usr/lib:${LD_LIBRARY_PATH}"
export MONO_PATH="${HERE}/usr/lib/mono/4.5"
export MONO_CFG_DIR="${HERE}/etc"
export MONO_CONFIG="${HERE}/etc/mono/config"

# Update/create the mono certificate store to enable https web queries
if [ -f "/etc/pki/tls/certs/ca-bundle.crt" ]; then
	mono "${HERE}/usr/lib/mono/4.5/cert-sync.exe" --quiet --user /etc/pki/tls/certs/ca-bundle.crt
elif [ -f "/etc/ssl/certs/ca-certificates.crt" ]; then
	mono "${HERE}/usr/lib/mono/4.5/cert-sync.exe" --quiet --user /etc/ssl/certs/ca-certificates.crt
else
	echo "WARNING: Unable to sync system certificate store - https requests will fail"
fi

# Run the game or server
if [ -n "$1" ] && [ "$1" = "--server" ]; then
	# Drop the --server argument
	shift
	exec "openra-{MODID}-server" "$@"
elif [ -n "$1" ] && [ "$1" = "--utility" ]; then
	# Drop the --utility argument
	shift
	exec "openra-{MODID}-utility" "$@"
else
	exec "openra-{MODID}" "$@"
fi
