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

# Variables
MOZ_DIST_BIN="/usr/lib64/thunderbird-17.0.2"
MOZ_PROGRAM=$MOZ_DIST_BIN/thunderbird
MOZ_CLIENT_PROGRAM="$MOZ_DIST_BIN/thunderbird -remote" 
MOZ_LANGPACKS_DIR="$MOZ_DIST_BIN/langpacks"
MOZ_EXTENSIONS_PROFILE_DIR="$HOME/.mozilla/extensions/{3550f703-e582-4d05-9a08-453d09bdfdc6}"

# MOZILLA_FIVE_HOME
MOZILLA_FIVE_HOME="$MOZ_DIST_BIN"
export MOZILLA_FIVE_HOME

##
## Set MOZ_ENABLE_PANGO is no longer used because Pango is enabled by default
## you may use MOZ_DISABLE_PANGO=1 to force disabling of pango
##
if [ ! "$MOZ_ENABLE_PANGO" == "1" ]
then
  MOZ_DISABLE_PANGO=1
  export MOZ_DISABLE_PANGO
fi

##
## firefox with flash on composite enabled X server
##
XLIB_SKIP_ARGB_VISUALS=1
export XLIB_SKIP_ARGB_VISUALS


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
}

##
## To disable the use of Firefox localization, set MOZ_DISABLE_LANGPACKS=1
## in your environment before launching Firefox.
##
#
# MOZ_DISABLE_LANGPACKS=1
# export MOZ_DISABLE_LANGPACKS
#

##
## Automatically installed langpacks are tracked by .vine-langpack-install
## config file.
##
VINE_LANGPACK_CONFIG="$MOZ_EXTENSIONS_PROFILE_DIR/.vine-langpack-install"

# MOZ_DISABLE_LANGPACKS disables language packs completelly
MOZILLA_DOWN=0

if ! [ $MOZ_DISABLE_LANGPACKS ] || [ $MOZ_DISABLE_LANGPACKS -eq 0 ]; then
    if [ -x $MOZ_DIST_BIN/mozilla-xremote-client ]; then
        # Is thunderbird running?
        $MOZ_DIST_BIN/mozilla-xremote-client -a thunderbird 'ping()' > /dev/null 2>&1
        MOZILLA_DOWN=$?
    fi
fi

# Modify language pack configuration only when thunderbird is not running 
# and language packs are not disabled
if [ $MOZILLA_DOWN -ne 0 ]; then

    # Clear already installed langpacks
    mkdir -p $MOZ_EXTENSIONS_PROFILE_DIR
    if [ -f $VINE_LANGPACK_CONFIG ]; then
        rm `cat $VINE_LANGPACK_CONFIG` > /dev/null 2>&1
        rm $VINE_LANGPACK_CONFIG > /dev/null 2>&1
    fi

    # Get locale from system
    CURRENT_LOCALE=$LC_ALL
    CURRENT_LOCALE=${CURRENT_LOCALE:-$LC_MESSAGES}
    CURRENT_LOCALE=${CURRENT_LOCALE:-$LANG}
    
    # 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 $CURRENT_LOCALE | sed "s|_\([^.]*\).*||g"`
    MOZLOCALE=`echo $CURRENT_LOCALE | sed "s|_\([^.]*\).*|-\1|g"`

    function create_langpack_link() {
        local language=$*
        local langpack=langpack-${language}@thunderbird.mozilla.org.xpi
        if [ -f $MOZ_LANGPACKS_DIR/$langpack ]; then
            rm -rf $MOZ_EXTENSIONS_PROFILE_DIR/$langpack
            ln -s $MOZ_LANGPACKS_DIR/$langpack \
                  $MOZ_EXTENSIONS_PROFILE_DIR/$langpack
            echo $MOZ_EXTENSIONS_PROFILE_DIR/$langpack > $VINE_LANGPACK_CONFIG
            return 0
        fi
        return 1
    }

    create_langpack_link $SHORTMOZLOCALE || create_langpack_link $MOZLOCALE || true
fi

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
            # remove 'mailto:' prefix
            ARGS="`echo $@ | sed 's/^mailto://'`"
            exec $MOZ_CLIENT_PROGRAM "mailto($ARGS)"
        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

