#!/bin/bash
#set -x # Uncomment "set -x" for debugging

export MOZILLA_FIVE_HOME="/usr/lib/mozilla-thunderbird"

#check if command line arguments have the form "mailto:somebody at somewhere.com?subject=something"
#if that's true put "somebody at somewhere.com?subject=something" in MAILTO address
MAILTO=`echo $@ | awk 'BEGIN{FS=":"} $1 ~ /mailto/ {print $2}'`

THUNDERBIRD_PATH="/usr/lib/mozilla-thunderbird"

if [ -z "`ps x | grep \"[0-9] ${THUNDERBIRD_PATH}/mozilla-thunderbird-bin\"`" ]; then
    # No thunderbird running
    /usr/bin/mozilla-thunderbird $@
else
    # thunderbird running - raise window
     /usr/bin/mozilla-thunderbird -remote "xfeDoCommand(openInbox)"
fi 

if [ -n "$MAILTO" ]; then
    /usr/bin/mozilla-thunderbird -remote "mailto($MAILTO)"
fi

