#!/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 iceape 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 should really not run iceape through sudo WITHOUT the -H option." >&2
        echo "Anyway, I'll do as if you did use 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
	    exit 1
        fi
    fi
elif [ -z "${HOME}" ]; then
    HOME=`getent passwd "${USER}" | cut -f6 -d:`
    if [ -z "${HOME}" ]; then
        echo "Could not find the correct home directory." >&2
	echo "Please set the HOME environment variable." >&2
	exit 1
    fi
fi

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

##
## Load system and user properties
##

RUNTIME_ICEAPE_DSP="${ICEAPE_DSP}"

if [ -f /etc/iceape/iceaperc ]; then
    . /etc/iceape/iceaperc
fi

if [ -f "${HOME}/.mozilla/iceaperc" ]; then
    . "${HOME}/.mozilla/iceaperc"
fi

if [ "${RUNTIME_ICEAPE_DSP}" ]; then
    ICEAPE_DSP="${RUNTIME_ICEAPE_DSP}"
fi

if [ -z "${ICEAPE_DSP}" ]; then
    ICEAPE_DSP="auto"
fi

##
## find /dev/dsp handler
##

if [ "${ICEAPE_DSP}" = "auto" ]; then
    ICEAPE_DSP=
    if [ -n "${AUDIOSERVER}" ]; then
        # do not prevent using other wrappers if $AUDIOSERVER was set up
        # unintentionally or audiooss is not available
        if type audiooss >/dev/null 2>&1; then
            ICEAPE_DSP=audiooss
        fi
    fi
    if pgrep -u `id -u` esd >/dev/null 2>&1; then 
        ICEAPE_DSP=esddsp 
    elif pgrep -u `id -u` arts >/dev/null 2>&1; then 
        ICEAPE_DSP=artsdsp 
    elif [ -x /usr/bin/aoss ] && [ -d /proc/asound ] ; then
        ICEAPE_DSP=aoss
    fi
elif [ "${ICEAPE_DSP}" = "none" ]; then
    ICEAPE_DSP=
fi

##
## Set LD_LIBRARY_PATH
##
EXTENT_LD_LIB_PATH=${MOZ_DIST_BIN}:${MOZ_DIST_BIN}/plugins:/usr/lib/mozilla/plugins
if [ "${LD_LIBRARY_PATH}" ]; then
    LD_LIBRARY_PATH=${EXTENT_LD_LIB_PATH}:${LD_LIBRARY_PATH}
else
    LD_LIBRARY_PATH=${EXTENT_LD_LIB_PATH}
fi

export LD_LIBRARY_PATH

EXTENT_MOZ_PLUGIN_PATH=/usr/lib/mozilla/plugins
if [ "${MOZ_PLUGIN_PATH}" ]; then
    MOZ_PLUGIN_PATH=${EXTENT_MOZ_PLUGIN_PATH}:${MOZ_PLUGIN_PATH}
else
    MOZ_PLUGIN_PATH=${EXTENT_MOZ_PLUGIN_PATH}
fi

export MOZ_PLUGIN_PATH

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=
TRY_USE_EXIST=0
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 ICEAPE_DSP OPTIONS DEBUG DEBUGGER

if [ ${DEBUG} -eq 1 ]; then
    if [ "${DEBUGGER}" = "" ]; then
        DEBUGGER=gdb
    fi
    TMPFILE=`mktemp -t iceape_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

if type "${ICEAPE_DSP}" > /dev/null 2>&1; then
    MOZ_PROGRAM="${ICEAPE_DSP} ${MOZ_PROGRAM}"
fi

exec_verbose ${MOZ_PROGRAM} "$@"
