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

#EduLinux 2004
DIR="/usr/lib/mozilla-thunderbird-0.5"
if [ -d $DIR ]; then
  FF=$DIR
  FBIN=/usr/bin/mozilla-thunderbird
fi

#Download manuel dans /opt
DIR="/opt/thunderbird"
if [ -d $DIR ]; then
  FF=$DIR
  FBIN=$DIR/thunderbird
fi

export MOZILLA_FIVE_HOME="$FF/"

#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}'`

if [ -z "`ps x | grep \"[0-9] $FF/thunderbird-bin\"`" ]; then
    # No thunderbird running
    if [ -n "$MAILTO" ]; then
      $FBIN -compose $@
    else
      $FBIN
    fi
    exit
else
    # thunderbird running - raise window
     $FBIN -remote "xfeDoCommand(openInbox)"
fi 

if [ -n "$MAILTO" ]; then
    $FBIN -remote "mailto($MAILTO)"
fi

