#!/bin/sh
##
##  firefox -- Mozilla Firefox Startup Control Utility
##  Copyright (c) 2000-2006 OpenPKG Foundation e.V. <http://openpkg.net/>
##  Copyright (c) 2000-2006 Ralf S. Engelschall <http://engelschall.com/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   configuration
firefox_dir="@l_prefix@/lib/firefox"
firefox_browser_bin="$firefox_dir/firefox"
firefox_remote_bin="$firefox_dir/run-mozilla.sh $firefox_dir/mozilla-xremote-client"
xsel_bin="@l_prefix@/bin/xsel"
wmctrl_bin="@l_prefix@/bin/wmctrl"

#   option parsing
opt_v=no
opt_w=no
opt_t=no
opt_r=""
while [ ".$1" != . ]; do
    case $1 in
        -v|--verbose ) opt_v=yes; shift ;;
        -w|--window  ) opt_w=yes; shift ;;
        -t|--tab     ) opt_t=yes; shift ;;
        -r|-remote|--remote ) opt_r="$2"; shift; shift ;;
        -* ) echo "$0:ERROR: invalid option \"$1\"" 1>&2; exit 1 ;;
        * ) break ;;
    esac
done
if [ $# -eq 0 ]; then
    set -- ""
fi

verbose () {
    if [ ".$opt_v" = .yes ]; then
        echo "firefox: $*" 1>&2
    fi
}

#   establish Mozilla environment
MOZILLA_FIVE_HOME="$firefox_dir"
export MOZILLA_FIVE_HOME
LANG=en_US.ISO8859-1
export LANG

#   special case of direct remote option
if [ ".$opt_r" != . ]; then
    verbose "exec: $firefox_remote_bin \"$opt_r\""
    exec $firefox_remote_bin "$opt_r"
fi

#   determine Mozilla run-time status
$firefox_remote_bin "ping()" >/dev/null 2>&1
if [ $? -eq 0 ]; then
    verbose "process already running (available remotely)"
    restart=no
else
    verbose "process still not running (not available remotely)"
    restart=yes
fi

#   open one or more URLs
first=yes
for url in "$@"; do
    #   determine URL to open
    if [ ".`echo .$url | grep '@'`" != . ]; then
        #   expand at-sign into X11 selection buffer
        xsel=`($xsel_bin --paste) 2>/dev/null`
        url=`echo "$url" | sed -e "s;@;$xsel;"`
    fi
    if [ ".$url" = . ]; then
        #   expand an empty URL into the internal blank page
        url="about:blank"
    else
        #   expand special URL constructs
        case "$url" in
            auto:* )
                url=`echo "$url" | sed -e 's;^auto:;;'`
                case "$url" in
                    http://* | https://* | ftp://* | file://* ) ;;
                    www.* ) url="http://$url" ;;
                    ftp.* ) url="ftp://$url" ;;
                    *.tar.gz | *.tgz ) url="ftpsearch:$url" ;;
                    *" "* ) url="google:$url" ;;
                    [0-9]* ) url="rfc:$url" ;;
                    * ) url="leo:$url" ;;
                esac
                ;;
        esac
        case "$url" in
            http://* | https://* | ftp://* | file://* )
                : # URL is already fully qualified
                ;;
            google:* ) 
                #   expand Google query
                url=`echo "$url" | sed -e 's;^google:;;' | tr ' ' '+'`
                url="http://www.google.com/search?q=$url"
                ;;
            leo:* ) 
                #   expand Leo Dictionary query
                url=`echo "$url" | sed -e 's;^leo:;;' | tr ' ' '+'`
                url="http://dict.leo.org/?search=$url"
                ;;
            ftpsearch:* ) 
                #   expand FTPSearch query
                url=`echo "$url" | sed -e 's;^ftpsearch:;;' | sed -e 's; ;%20;g'`
                url="http://www.alltheweb.com/search?advanced=1&cat=ftp&q=$url"
                ;;
            rfc:* )
                #   expand Zvon.org RFC query
                url=`echo "$url" | sed -e 's;^rfc:;;'`
                url="http://zvon.org/tmRFC/RFC$url/Output/index.html"
                ;;
            whois:* )
                #   expand Geektools.com WHOIS query
                url=`echo "$url" | sed -e 's;^whois:;;'`
                url="http://www.whois-search.com/whois/$url"
                ;;
            jargon:* )
                #   expand UMEC Jargon File query
                url=`echo "$url" | sed -e 's;^jargon:;;' | tr ' ' '+'`
                url="http://www.tf.hut.fi/cgi-bin/jargon?search=$url"
                ;;
            /* ) 
                #   expand absolute filename into file URL
                url="file://$url"
                ;;
            * ) 
                #   expand relative filename into file URL
                url="file://`pwd`/$url"
                ;;
        esac
    fi

    if [ ".$first" = .yes -a ".$restart" = .yes ]; then
        #   handling of first URL if restarting is necessary
        verbose "running new process"
        if [ ".$url" = ".about:blank" ]; then
            verbose "exec: $firefox_browser_bin >/dev/null 2>&1 &"
            $firefox_browser_bin >/dev/null 2>&1 &
        else
            verbose "exec: $firefox_browser_bin \"$url\" >/dev/null 2>&1 &"
            $firefox_browser_bin "$url" >/dev/null 2>&1 &
        fi
    else
        #   delayed waiting for process to be finally remotely available
        #   if it was started from scratch for the handling of the first
        #   URL. This way we wait only if necessary, i.e., if more than
        #   one URL was given.
        if [ ".$first" = .no -a ".$restart" = .yes ]; then
            verbose "waiting for new process to be available remotely"
            sleep 4 # give it a little bit of time to fully startup
            i=0
            while [ $i -lt 10 ]; do
                $firefox_remote_bin "ping()" >/dev/null 2>&1
                if [ $? -eq 0 ]; then
                    break
                fi
                sleep 1 # give it a little bit more time to startup
                i=`expr $i + 1`
            done
        fi

        #   try to activate (and bring to foreground) the Mozilla Firefox window
        $wmctrl_bin -a "Mozilla Firefox" >/dev/null 2>&1 || true

        #   determine remote command
        if [ ".$opt_w" = .yes ]; then
            if [ ".$url" = ".about:blank" ]; then
                cmd="xfeDoCommand(openBrowser)"
            else
                cmd="openURL($url, new-window)"
            fi
        elif [ ".$opt_t" = .yes ]; then
            cmd="openURL($url, new-tab)"
        else
            cmd="openURL($url)"
        fi

        #   perform remote command
        verbose "sending remote command to running process"
        verbose "exec: $firefox_remote_bin \"$cmd\" >/dev/null 2>&1"
        $firefox_remote_bin "$cmd" >/dev/null 2>&1
    fi
    first=no
done

