#!/bin/sh

# workaround TLS/SSL negotiation caching issues of Mono, see:
# https://smuxi.im/issues/show/802
MONO_TLS_SESSION_CACHE_TIMEOUT=0
export MONO_TLS_SESSION_CACHE_TIMEOUT

# Mono >= 4 SEGVs with Boehm as GC during startup
# see https://bugzilla.opensuse.org/show_bug.cgi?id=955080
if ! mono -V | grep -q -e "version [4-9]\."; then
    # HACK: forcibly disabled SGen, as it has a known SEGV bug related to the
    # Mono.Data.Sqlite binding that does not happen with the boehm GC, see:
    # https://smuxi.im/issues/show/1062
    MONO_ENV_OPTIONS="$(echo $MONO_ENV_OPTIONS | sed s/--gc=sgen//)"
    MONO_ENV_OPTIONS="--gc=boehm $MONO_ENV_OPTIONS"
    export MONO_ENV_OPTIONS
fi

# Smuxi uses an IPC channel for the single application instance feature and it
# also allows to pass links from commandline to an existing Smuxi instance. This
# IPC channel must be private to the user that executes Smuxi, else other system
# users could control the existing Smuxi instance. Mono doesn't support LOCAL\
# named pipes yet and thus we need to emulate the privateness to the user by
# using a TMP directory that is only readable by the same user who started
# Smuxi. This also workarounds the world-writable unix socket in /tmp issue of
# Mono, see: https://smuxi.im/issues/show/1072
SMUXI_TMP=$HOME/.cache/smuxi/tmp
if [ ! -d $SMUXI_TMP ]; then
    mkdir -p $SMUXI_TMP
fi
chmod 700 $SMUXI_TMP
TMP=$SMUXI_TMP
export TMP

exec mono --debug "/usr/lib/smuxi/smuxi-frontend-gnome.exe" "$@"
