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

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

#
##
## Functions
##
##########################################################################
moz_usage()
{
echo "Usage:  ${cmdname} [options] [program]"
echo ""
echo "  options:"
echo ""
echo "    -g                   Run in debugger."
echo "    --debug"
echo ""
echo "    -d debugger          Debugger to use."
echo "    --debugger debugger"
echo ""
echo "    -a debugger_args     Arguments passed to [debugger]."
echo "    --debugger-args debugger_args"
echo ""
echo "  Examples:"
echo ""
echo "  Run the mozilla-bin binary"
echo ""
echo "    ${cmdname} mozilla-bin"
echo ""
echo "  Debug the mozilla-bin binary in gdb"
echo ""
echo "    ${cmdname} -g mozilla-bin -d gdb"
echo ""
echo "  Run mozilla-bin under valgrind with arguments"
echo ""
echo "    ${cmdname} -g -d valgrind -a '--tool=memcheck --leak-check=full' mozilla-bin"
echo ""
        return 0
}
##########################################################################
moz_bail()
{
    message=$1
    echo
    echo "$cmdname: $message"
    echo
    exit 1
}

##########################################################################
moz_get_debugger()
{
    debuggers="ddd gdb dbx bdb native-gdb"
    debugger="notfound"
    done="no"
    for d in $debuggers
    do
        dpath=`which ${d}`      
        if [ -x "$dpath" ]
        then
            debugger=$dpath
            break
        fi
    done
    echo $debugger
    return 0
}
##########################################################################
moz_run_program()
{
    prog=$MOZ_PROGRAM
    ##
    ## Make sure the program is executable
    ##
    if [ ! -x "$prog" ]
    then
        moz_bail "Cannot execute $prog."
    fi
    ##
    ## Run the program
    ##
    exec "$prog" ${1+"$@"}
    exitcode=$?
}
##########################################################################
moz_debug_program()
{
    prog=$MOZ_PROGRAM
    ##
    ## Make sure the program is executable
    ##
    if [ ! -x "$prog" ]
    then
        moz_bail "Cannot execute $prog."
    fi
    if [ -n "$moz_debugger" ]
    then
        debugger=`which $moz_debugger` 
    else
        debugger=`moz_get_debugger`
    fi
    if [ -x "$debugger" ] 
    then
        case `basename $debugger` in
            gdb) echo "$debugger $moz_debugger_args --args $prog" ${1+"$@"}
                exec "$debugger" $moz_debugger_args --args "$prog" ${1+"$@"}
                exitcode=$?
                ;;
            *) echo "$debugger $moz_debugger_args $prog ${1+"$@"}"
                exec $debugger $moz_debugger_args "$prog" ${1+"$@"}
                exitcode=$?
                ;;
        esac
    else
        moz_bail "Could not find a debugger on your system."
    fi
}

##
## 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
    # Is thunderbird running?
    exec ps ax | grep $MOZ_PROGRAM | grep -v grep > /dev/null 2>&1
    MOZILLA_DOWN=$?
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

##########################################################################
##
## Command line arg defaults
##
moz_debug=0
moz_debugger=""
moz_debugger_args=""
#
##
## Parse the command line
##
while [ $# -gt 0 ]
do
  case $1 in
    -g | --debug)
      moz_debug=1
      shift
      ;;
    -d | --debugger)
      moz_debugger=$2;
      if [ "${moz_debugger}" != "" ]; then
        shift 2
      else
        echo "-d requires an argument"
        exit 1
      fi
      ;;
    -a | --debugger-args)
      moz_debugger_args=$2;
      if [ "${moz_debugger_args}" != "" ]; then
        shift 2
      else
        echo "-a requires an argument"
        exit 1
      fi
      ;;
    *)
      break;
      ;;
  esac
done

##
## Program not given, try to guess a default
##
if [ -z "$MOZ_PROGRAM" ]
then
        ##
        ## Try this script's name with '-bin' appended
        ##
        if [ -x "$MOZ_DEFAULT_NAME" ]
        then
                MOZ_PROGRAM=$MOZ_DEFAULT_NAME
        ##
        ## Try mozilla-bin
        ## 
        elif [ -x "$MOZ_APPRUNNER_NAME" ]
        then
                MOZ_PROGRAM=$MOZ_APPRUNNER_NAME
        fi
fi
#
#
##
## Make sure the program is executable
##
if [ ! -x "$MOZ_PROGRAM" ]
then
        moz_bail "Cannot execute $MOZ_PROGRAM."
fi

# 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

if [ $moz_debug -eq 1 ]
then
        moz_debug_program ${1+"$@"}
else
        moz_run_program ${1+"$@"}
fi

exit $exitcode

