#!/bin/ksh

get_browser_path (){
    BROSER_PATH=`whence $1`
    echo $BROSER_PATH
}

launch_browser (){
    exec "$@"
}

add_dtbin (){
    IFS=":"
    DTBIN="false"
    for DIR in $PATH ; do
        DIR=`echo "$DIR" | sed -e "s|\(/usr/dt/bin\)[/\.]*|\1|"`
        if [ "$DIR" = "/usr/dt/bin" ] ; then
            DTBIN="true"
            break
        fi
    done

    if [ "$DTBIN" = "true" ] ; then
        echo "$PATH"
    else
        echo "$PATH:/usr/dt/bin"
    fi
}

# main
{
    GCONFTOOL="/usr/bin/gconftool-2"
    GCONF_KEY="/desktop/gnome/url-handlers/unknown/command"
    CDE_BROWSER="/usr/dt/bin/sdtwebclient"
    NETSCAPE="/usr/dt/bin/netscape"
    DEFAULT_URL="file:///usr/lib/im/locale/ja/atokserver/help/index.html"
    URL=$1

    if [ -z "$URL" ] ; then
        URL=$DEFAULT_URL
    else
        URL=`echo "$URL" | sed -e 's/ /%20/g'`
    fi

    PATH=`add_dtbin`
    export PATH

    CHECK_DISPLAY=`echo $DISPLAY | grep ".*:[0-9][0-9]*\.[0-9][0-9]*$"`
    if [ ! -z "$CHECK_DISPLAY" ] ; then
        DISPLAY=`echo $DISPLAY | sed -e  "s/\(.*:[0-9][0-9]*\)\.[0-9][0-9]*$/\1/"`
    fi
    export DISPLAY

    if [ -x "$GCONFTOOL" ] ; then
        GNOME_BROWSER=`$GCONFTOOL --get $GCONF_KEY`
        BROWSER_PATH=`get_browser_path $GNOME_BROWSER`
        if [ -x "$BROWSER_PATH" ] ; then
            GNOME_BROWSER=`echo $GNOME_BROWSER | sed -e "s/\"\(.*\)%s\(.*\)\"/\1%s\2/"`
            BROWSER=`printf "$GNOME_BROWSER" "$URL"`
        else
            BROWSER="$NETSCAPE $URL"
        fi
    else
        BROWSER="$CDE_BROWSER $URL"
    fi

    launch_browser $BROWSER
}

