#!/bin/sh

SEAMONKEY_PATH=/opt/seamonkey/bin/seamonkey-original

if $SEAMONKEY_PATH -remote 'ping()' &> /dev/null; then
   # check for empty parameter:
   if [ -z $1 ]; then
      exec $SEAMONKEY_PATH -remote "xfeDoCommand(openBrowser)"
   else
      # check for "-" parameter:
      PARAMSTART=`echo $1 | cut -c 1`
      if [ $PARAMSTART = "-" ]
      then
         case $1 in
            -browser)
               exec $SEAMONKEY_PATH -remote "xfeDoCommand(openBrowser)"
            ;;
            -mail)
               exec $SEAMONKEY_PATH -remote "xfeDoCommand(openInbox)"
            ;;
            -compose)
               exec $SEAMONKEY_PATH -remote "xfeDoCommand(composeMessage)"
            ;;
            -calendar)
               exec $SEAMONKEY_PATH -remote "xfeDoCommand(openCalendar)"
            ;;
            -*)
               exec zenity --info --title="SeaMonkey already running..." --text="An instance of SeaMonkey is already loaded, so certain SeaMonkey apps will need to be run from within that instance.

TIP: if you don't see a SeaMonkey window, maybe it crashed and the process is still running? In that case, use htop or another taskmanager to get rid of it..."
            ;;
         esac
      else
         # Assume a URL:
         exec $SEAMONKEY_PATH -remote "openURL($1,new-tab)"
      fi
   fi
else
   # no existing process
   exec $SEAMONKEY_PATH $@
fi
