#!/bin/sh
# (c) vip at linux.pl, wolf at pld-linux.org

LD_LIBRARY_PATH=/usr/lib/seamonkey${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

MOZILLA_FIVE_HOME="/usr/lib/seamonkey"
SEAMONKEY="$MOZILLA_FIVE_HOME/seamonkey-bin"
if [ "$1" == "-remote" ]; then
	exec $SEAMONKEY "$@"
fi

PING=`$SEAMONKEY -remote 'ping()' 2>&1 >/dev/null`
if [ -n "$PING" ]; then
	if [ -f "`pwd`/$1" ]; then
		exec $SEAMONKEY "file://`pwd`/$1"
	else
		exec $SEAMONKEY "$@"
	fi
fi

if [ -z "$1" ]; then
	exec $SEAMONKEY -remote 'xfeDoCommand (openBrowser)'
elif [ "$1" == "-mail" ]; then
	exec $SEAMONKEY -remote 'xfeDoCommand (openInbox)'
elif [ "$1" == "-compose" ]; then
	exec $SEAMONKEY -remote 'xfeDoCommand (composeMessage)'
fi

[[ $1 == -* ]] && exec $SEAMONKEY "$@"

if [ -f "`pwd`/$1" ]; then
	URL="file://`pwd`/$1"
else
	URL="$1"
fi
if grep -q -E 'browser.tabs.opentabfor.middleclick.*true' \
		~/.mozilla/default/*/prefs.js; then
	exec $SEAMONKEY -remote "OpenUrl($URL,new-tab)"
else
	exec $SEAMONKEY -remote "OpenUrl($URL,new-window)"
fi

echo "Cannot execute SeaMonkey ($SEAMONKEY)!" >&2
exit 1
