#!/bin/sh
#
# The contents of this file are subject to the Netscape 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/NPL/
#
# 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 Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): 
#

##
## For silly people running iceweasel through sudo
##
if [ "${SUDO_USER}" ] && [ "${SUDO_USER}" != "${USER}" ]; then
    SUDO_HOME=`getent passwd ${SUDO_USER} | cut -f6 -d:`
    if [ "${SUDO_HOME}" = "${HOME}" ]; then
        echo "You shouldn't really run Iceweasel through sudo WITHOUT the -H option." >&2
        echo "Continuing as if you used the -H option." >&2
        HOME=`getent passwd ${USER} | cut -f6 -d:`
        if [ -z "${HOME}" ]; then
            echo "Could not find the correct home directory. Please use the -H option of sudo." >&2
        fi
    fi
fi

##
## Variables
##
MOZ_APP_LAUNCHER="$(which $0)"
MOZ_DIST_BIN="$(dirname "$(readlink -f "${MOZ_APP_LAUNCHER}")")"
MOZ_PROGRAM="${MOZ_DIST_BIN}/firefox-bin"
export MOZ_APP_LAUNCHER

##
## Load system and user properties
##

verbose () {
    if [ "${VERBOSE}" ]; then
        echo $@
    fi
}

echo_vars () {
    if [ "${VERBOSE}" ]; then
      for var in "$@"; do
          echo "$var=`eval echo \\${$var}`"
      done
    fi
}
    
# exec wrapper for verbosity
exec_verbose () {
    verbose Running: $@
    exec "$@"
}

# exec wrapper for verbosity
run_verbose () {
    verbose Running: $@
    "$@"
}

# OK, here's where all the real work gets done

# parse command line
VERBOSE=
DEBUG=0
DEBUGGER=
first=1
prev=
for arg in "$@"; do
    if [ ${first} -eq 1 ]; then
        set dummy
        first=0
    fi

    if [ "${prev}" ]; then # That can only be --debugger
        DEBUGGER="${arg}"
        prev=
    else
        case "$arg" in
            --verbose | -V)
                VERBOSE=1
                ;;
            -g | -debug)
                DEBUG=1
                ;;
            --debugger)
                DEBUG=1
                prev=${arg}
                ;;
            *)
                set "$@" "${arg}"
                ;;
        esac
    fi
done

if [ $# -ne 0 ]; then
    shift
fi
OPTIONS="$@"

echo_vars OPTIONS DEBUG DEBUGGER

if [ ${DEBUG} -eq 1 ]; then
    if [ "${DEBUGGER}" = "" ]; then
        DEBUGGER=gdb
    fi
    TMPFILE=`mktemp -t iceweasel_argsXXXXXX`
    echo set args "$@" > ${TMPFILE}
    case "${DEBUGGER}" in
        gdb)
            run_verbose gdb "${MOZ_PROGRAM}" -x ${TMPFILE}
            ;;
        ddd)
            run_verbose ddd --debugger "gdb -x ${TMPFILE}" "${MOZ_PROGRAM}"
            ;;
        *)
            run_verbose ${DEBUGGER} "${MOZ_PROGRAM}" "$@"
            ;;
    esac
    rm ${TMPFILE}
    exit
fi

exec_verbose ${MOZ_PROGRAM} "$@"
