#!/bin/sh
# $OpenBSD: iridium,v 1.6 2019/05/27 16:11:17 robert Exp $

DATASIZE="716800"
OPENFILES="400"

xm_log() {
	echo -n "$@\nDo you want to run Iridium anyway?\n\
(If you don't increase these limits, Iridium might fail to work properly.)" | \
		/usr/X11R6/bin/xmessage -file - -center -buttons yes:0,no:1 -default no
}

if [ $(ulimit -Sd) -lt ${DATASIZE} ]; then
	ulimit -Sd ${DATASIZE} || \
		xm_log "Cannot increase datasize-cur to at least ${DATASIZE}"
		[ $? -eq 0 ] || exit
fi

if [ $(ulimit -Sn) -lt ${OPENFILES} ]; then
	ulimit -Sn ${OPENFILES} || \
		xm_log "Cannot increase openfiles-cur to at least ${OPENFILES}"
		[ $? -eq 0 ] || exit
fi

#
# Temporary workaround for the case when iridium crashes and leaves
# the SingletonLock, which prevents iridium to start up.
#
if [ -h ${HOME}/.config/iridium/SingletonLock ]; then
	_pid=`readlink ${HOME}/.config/iridium/SingletonLock | cut -d '-' -f 2`
	kill -0 ${_pid} 2>/dev/null
	if [ $? -gt 0 ]; then
		rm ${HOME}/.config/iridium/SingletonLock
	fi
fi

#
# Issue #395446
# https://code.google.com/p/chromium/issues/detail?id=395446
#
[ -z ${LANG} ] && _l=en_US.UTF-8 || _l=${LANG}

# Always use our versions of ffmpeg libs.
# This also makes RPMs find the compatibly-named library symlinks.
if [[ -n "$LD_LIBRARY_PATH" ]]; then
	LD_LIBRARY_PATH="/usr/local/iridium:/usr/local/iridium/lib:$LD_LIBRARY_PATH"
else
	LD_LIBRARY_PATH="/usr/local/iridium:/usr/local/iridium/lib"
fi
export LD_LIBRARY_PATH

[ -d ~/Downloads ] || mkdir -m 0700 ~/Downloads
[ -d ~/.config ] || mkdir -m 0700 ~/.config
[ -f ~/.config/user-dirs.dirs ] || \
	echo 'XDG_DOWNLOAD_DIR="$HOME/Downloads"' > ~/.config/user-dirs.dirs

LANG=${_l} exec "/usr/local/iridium/iridium" --disable-features=WebAssembly,AsmJsToWebAssembly,WebAssemblyStreaming --js-flags=--noexpose-wasm "${@}"
