#!/bin/sh
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org Code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

## $Id: mozilla.in,v 1.8.8.8 2008/01/10 18:47:15 reed%reedloden.com Exp $
## 
## Usage:
##
## $ mozilla [args]
##
## This script is meant to run the application binary from either 
## mozilla/xpfe/bootstrap or mozilla/dist/bin.
##
## The script will setup all the environment voodoo needed to make
## the application binary to work.
##

moz_pis_startstop_scripts()
{
  MOZ_USER_DIR=".mozilla"
  # MOZ_PIS_ is the name space for "Mozilla Plugable Init Scripts"
  # These variables and there meaning are specified in
  # mozilla/xpfe/bootstrap/init.d/README
  MOZ_PIS_API=2
  MOZ_PIS_MOZBINDIR="${dist_bin}"
  MOZ_PIS_SESSION_PID="$$"
  MOZ_PIS_USER_DIR="${MOZ_USER_DIR}"
  export MOZ_PIS_API MOZ_PIS_MOZBINDIR MOZ_PIS_SESSION_PID MOZ_PIS_USER_DIR
  
  case "${1}" in
    "start")
      for curr_pis in "${dist_bin}/init.d"/S* "${HOME}/${MOZ_USER_DIR}/init.d"/S* ; do
        if [ -x "${curr_pis}" ] ; then
          case "${curr_pis}" in
            *.sh) .  "${curr_pis}"         ;;
            *)       "${curr_pis}" "start" ;;
          esac
        fi
      done
      ;;
    "stop")
      for curr_pis in "${HOME}/${MOZ_USER_DIR}/init.d"/K* "${dist_bin}/init.d"/K* ; do
        if [ -x "${curr_pis}" ] ; then
          case "${curr_pis}" in
            *.sh) . "${curr_pis}"        ;;
            *)      "${curr_pis}" "stop" ;;
          esac
        fi
      done
      ;;
    *)
      echo 1>&2 "$0: Internal error in moz_pis_startstop_scripts."
      exit 1
      ;;
  esac
}

#uncomment for debugging
#set -x

moz_libdir=/usr/local/lib/seamonkey-1.1.16
MRE_HOME=/usr/local/lib/mre/mre-1.1.16

# Use run-mozilla.sh in the current dir if it exists
# If not, then start resolving symlinks until we find run-mozilla.sh
found=0
progname="$0"
curdir=`dirname "$progname"`
run_moz="$curdir/run-mozilla.sh"
if test -x "$run_moz"; then
  dist_bin="$curdir"
  found=1
else
  here=`/bin/pwd`
  while [ -h "$progname" ]; do
    bn=`basename "$progname"`
    cd `dirname "$progname"`
    progname=`/bin/ls -l "$bn" | sed -e 's/^.* -> //' `
    progbase=`basename "$progname"`
    if [ ! -x "$progname" ]; then
      break
    fi
    curdir=`dirname "$progname"`
    run_moz="$curdir/run-mozilla.sh"
    if [ -x "$run_moz" ]; then
      cd "$curdir"
      dist_bin=`pwd`
      run_moz="$dist_bin/run-mozilla.sh"
      found=1
      break
    fi
  done
  cd "$here"
fi
if [ $found = 0 ]; then
  # Check default compile-time libdir
  if [ -x "$moz_libdir/run-mozilla.sh" ]; then
    dist_bin="$moz_libdir"
    run_moz="$moz_libdir/run-mozilla.sh"
  else 
    echo "Cannot find SeaMonkey runtime directory. Exiting."
    exit 1
  fi
fi

if [ -z "$MOZ_NO_REMOTE" ] && [ $# -lt 5 ]; then
  mail=0
  compose=0
  profile=""
  extra=""
  i=1
  while [ $i -le $# ]; do
    eval opt=\$$i
    if [ $mail -eq 1 ] || [ $compose -eq 1 ]; then
      # look for extra arg for mail/compose
      # multiply by 2 to prevent us from coming back here
      mail=`expr $mail \* 2`
      compose=`expr $compose \* 2`
      # make sure it's not the next flag
      if [ `expr "$opt" : '-.*'` -eq 0 ]; then
        extra=",$opt"
        i=`expr $i + 1`
        continue
      fi
    fi
    case "$opt" in
      -mail)
        # look for an optional extra arg next pass
        mail=1
        ;;
      -compose)
        # look for an optional extra arg next pass
        compose=1
        ;;
      -P)
        # extra arg is required.  look for it now
        j=`expr $i + 1`
        eval profile=\$$j
        # make sure it's not a flag
        if [ -n "$profile" ] && [ `expr "$profile" : '-.*'` -eq 0 ]; then
          profile="-p $profile"
          i=$j
        else
          MOZ_NO_REMOTE=1
          break
        fi
        ;;
      -*)
        # we don't recognize this flag, so bail
        MOZ_NO_REMOTE=1
        break
        ;;
      *)
        if [ $i -eq $# ]; then
          # If this is the last command line argument and it doesn't begin with 
          # a '-' it's probably a url.
          URL="$opt"
        else
          # URL must be the last argument.  If it isn't, fallback
          MOZ_NO_REMOTE=1
          break
        fi
        ;;
    esac
    i=`expr $i + 1`
  done

  MOZ_CLIENT_PROGRAM="$dist_bin/mozilla-xremote-client"

  if [ "$OSTYPE" = "beos" ]; then
    mimeset -F "$MOZ_CLIENT_PROGRAM"
  fi

  # Open a new tab/window in an existing instance if we can find one.  Don't try if
  # there are more than 2 arguments, or the second argument is a flag (like -P)
  appname="seamonkey"
  if [ -z "$MOZ_NO_REMOTE" ] && 
     "${run_moz}" "$MOZ_CLIENT_PROGRAM" $profile -a "${appname}" 'ping()' 2>/dev/null >/dev/null; then

    if [ $mail -ne 0 ]; then
      # -mail was given
      exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" $profile -a "${appname}" "xfeDoCommand(openInbox${extra})"
    elif [ $compose -ne 0 ]; then
      # -compose was given
      exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" $profile -a "${appname}" "xfeDoCommand(composeMessage${extra})"
    elif [ -z "$URL" ]; then
      # No command line args. Open new window.
      exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" $profile -a "${appname}" "xfeDoCommand(openBrowser)"
    fi

    # There's a URL.  Try to send it to a running instance.
    if [ `expr "${URL}" : '.*:/.*'` -eq 0 -a \( -f "${URL}" -o -d "${URL}" \) ]; then
      # The URL seems to be a local file/directory, so prepend "file://"
      # First, check if it is absolutely specified (ie. /home/foo/file vs. ./file)
      # If it is just "relatively" (./file) specified, make it absolutely
      if [ `expr "${URL}" : '/.*'` -eq 0 ]; then
        URL="`pwd`/${URL}"
      fi
      URL="file://${URL}"
    fi

    exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" $profile -a "${appname}" "openURL(${URL})"
  fi
fi

# Default action - no running instance

script_args=""
debugging=0
MOZILLA_BIN="seamonkey-bin"

if [ "$OSTYPE" = "beos" ]; then
  mimeset -F "$MOZILLA_BIN"
fi

pass_arg_count=0
while [ $# -gt $pass_arg_count ]
do
  case "$1" in
    -p | --pure | -pure)
      MOZILLA_BIN="${MOZILLA_BIN}.pure"
      shift
      ;;
    -g | --debug)
      script_args="$script_args -g"
      debugging=1
      shift
      ;;
    -d | --debugger)
      script_args="$script_args -d $2"
      shift 2
      ;;
    *)
      # Move the unrecognized argument to the end of the list.
      arg="$1"
      shift
      set -- "$@" "$arg"
      pass_arg_count=`expr $pass_arg_count + 1`
      ;;
  esac
done

export MRE_HOME

## Start addon scripts
moz_pis_startstop_scripts "start"

if [ $debugging = 1 ]
then
  echo $dist_bin/run-mozilla.sh $script_args $dist_bin/$MOZILLA_BIN "$@"
fi
"$dist_bin/run-mozilla.sh" $script_args "$dist_bin/$MOZILLA_BIN" "$@"
exitcode=$?

## Stop addon scripts
moz_pis_startstop_scripts "stop"

exit $exitcode
# EOF.
