#!/bin/sh
#
# Startup script for the fedora.us Thunderbird RPM
# (based on the Mozilla RPM launch script)
#

MOZ_LIB_DIR="/usr/lib"
if [ -x "/usr/lib64/thunderbird-1.5.0.12/thunderbird-bin" ]
then
  MOZ_LIB_DIR="/usr/lib64"
fi

MOZILLA_FIVE_HOME="$MOZ_LIB_DIR/thunderbird-1.5.0.12"
MRE_HOME="$MOZ_LIB_DIR/thunderbird-1.5.0.12"
export MOZILLA_FIVE_HOME MRE_HOME

MOZ_PROGRAM=$MRE_HOME/thunderbird
#MOZ_CLIENT_PROGRAM="$MRE_HOME/mozilla-xremote-client -a thunderbird"
MOZ_CLIENT_PROGRAM="$MRE_HOME/thunderbird -remote" 

##
## In order to better support certain scripts (such as Indic and some CJK
## scripts), Red Hat builds its Firefox, with permission from the Mozilla
## Corporation, with the Pango system as its text renderer.  This change
## may negatively impact performance on some pages.  To disable the use of
## Pango, set MOZ_DISABLE_PANGO=1 in your environment before launching
## Firefox.
##
#
# MOZ_DISABLE_PANGO=1
# export MOZ_DISABLE_PANGO
#

function check_running() {
    $MOZ_CLIENT_PROGRAM 'ping()' 2>/dev/null >/dev/null
    RETURN_VAL=$?
    if [ "$RETURN_VAL" -eq "2" ]; then
      echo 0
      return 0
    else
      echo 1
      return 1
    fi
}

function rm_shit() {
    find $HOME/.thunderbird -name XUL.mfasl 2>/dev/null | xargs rm -f
}

# currently unused
MOZARGS=""

# Try without a local variant first, then with a local variant
# So that pt-BR doesn't try to use pt for example
SHORTMOZLOCALE=`echo $LANG | sed "s|_\([^.]*\).*||g"`
[ -f $MOZILLA_FIVE_HOME/extensions/langpack-${SHORTMOZLOCALE}@thunderbird.mozilla.org/chrome/$SHORTMOZLOCALE.jar ] && MOZARGS="-UILocale $SHORTMOZLOCALE"

MOZLOCALE=`echo $LANG | sed "s|_\([^.]*\).*|-\1|g"`
[ -f $MOZILLA_FIVE_HOME/chrome/$MOZLOCALE.jar -o \
  -f $MOZILLA_FIVE_HOME/extensions/langpack-${MOZLOCALE}@thunderbird.mozilla.org/chrome/$MOZLOCALE.jar ] && MOZARGS="-UILocale $MOZLOCALE"


ALREADY_RUNNING=`check_running`

# If no command-line arguments given...
if [ -z "$1" ]; then
    if [ "${ALREADY_RUNNING}" -eq "1" ]; then
        exec $MOZ_CLIENT_PROGRAM "xfeDoCommand(openInbox)" >/dev/null 2>&1
    else
        rm_shit
        exec $MOZ_PROGRAM $MOZARGS >/dev/null 2>&1
    fi
fi

for arg in $@ ; do
    case "$1" in
    -remote)
        shift
        exec $MOZ_PROGRAM -remote "$@"
        ;;

    -mail)
        shift
        if [ "${ALREADY_RUNNING}" -eq "1" ]; then
            # remove 'mailto:' prefix
            ARGS="`echo $@ | sed 's/^mailto://'`"
            exec $MOZ_CLIENT_PROGRAM "mailto($ARGS)"
        else
            rm_shit
            exec $MOZ_PROGRAM $MOZARGS -mail "$@"
        fi
        ;;

    -compose)
        shift
        if [ "${ALREADY_RUNNING}" -eq "1" ]; then
            if `echo $@ | grep -q attachment=` ; then
                ARGS="`echo $@`"
                exec $MOZ_CLIENT_PROGRAM "xfeDoCommand(composeMessage,$ARGS)"
            else
                # remove 'mailto:' prefix
                ARGS="`echo $@ | sed 's/^mailto://'`"
                exec $MOZ_CLIENT_PROGRAM "mailto($ARGS)"
            fi
        else
            rm_shit
            exec $MOZ_PROGRAM $MOZARGS -compose "$@"
        fi
        ;;

    *)
        # for now, pass it on and hope for the best
        exec $MOZ_PROGRAM $MOZARGS "$@"
        ;;
    esac
    shift
done

