#!/bin/bash

# If passed nothing, about:blank.
# If passed a single filename, prepend file://cwd/ to it.
# If passed a single pathname, check if it's absolute or relative.
#  If absolute, prepend file://
#  If relative, prepend cwd, realpath(), then prepend file://
# If passed a URL, do nothing to it.

# Check if firefox is running (ping)
# If firefox isn't running, start it naked first, and check to see when it starts pinging. (timeout 60 seconds or so)
# Pass the URL to firefox via -remote openURL()

# If multiple parameters are passed, they had better be escaped properly. Treat each as a separate file/path/url
#  and openURL() each.

# Select action based on command name (ffw = new windows, ffo = all URLs in tabs of new window, ff = new tabs)
me="$(basename "${0}")"
if [ "${me}" = "ffw" ]
then
        action="new-window"
elif [ "${me}" = "ffo" ]
then
        action="new-window"
        nextaction="new-tab"
else
        action="new-tab"
fi

# Parse the arguments to launch things.

startff() {
        url="$1"
        action="$2"

        # Check if firefox is running. If not, start naked
        if ! /usr/bin/firefox -a firefox -remote "ping()"
        then
                ( cd ~; exec /usr/bin/firefox ) &
                timeout=30
                while ! /usr/bin/firefox -a firefox -remote "ping()"
                do
                        sleep 1
                        if [ "$(( timeout-- ))" -lt 1 ]
                        then
                                # Not responding. Kill ourselves.
                                echo "Not responding."
                                exit 64
                        fi
                done
        fi

        # Should be running now.
        if ! /usr/bin/firefox -a firefox -remote "openURL(${url},${action})"
        then
                echo "Fail: openURL(${url},${action})"
        fi
}

if [ "${#}" -eq 0 ]
then
        startff "about:blank" ${action}
        exit 0
fi

while [ -n "${1}" ]
do
        spec="${1}"

        # Check if it's a URL (://)
        if [ -n "$(echo "${spec}" | grep "://")" ]
        then
                # url
                url="${spec}"
        else
                # Check if it's a file.
                if [ "${spec:0:1}" = "/" ]
                then
                        # Absolute path
                        url="$(printf "file://%s" "${spec}")"
                else
                        # Relative path
                        url="$(printf "file://%s/%s" "${PWD}" "${spec}")"
                fi
        fi

        startff "${url}" "${action}"

        if [ -n "${nextaction}" ]
        then
                action="${nextaction}"
                unset nextaction
        fi

        shift
done

exit 0

# Legacy code below here.
if [ -z "${*}" ]
then
        url="about:blank"
elif [ -z "$(echo ${*} | grep "://")" ]
then
        # Assemble a 'local' URL
        url="$(printf "file://%s/%s" "$(pwd)" "${*}")"
else
        url="${*}"
fi

echo ${url}
exec /usr/bin/firefox -a firefox -remote "openURL(${url},new-tab)"