#!/bin/bash
#
#  Script to start the help system.
#
#  This file is part of the SuSE help system.
#
#  Copyright (C) 1994-2001  SuSE GmbH
#  Copyright (C) 2002  SuSE Linux AG, Nuernberg
#  Copyright (C) 2004  SUSE LINUX AG, Nuernberg
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


DEBUG_ON=

TEXTDOMAIN=susehelp

function shlog() {
    if [ -n "$DEBUG_ON" ]; then
	echo $*
    fi
};

echo_help() {
echo $"
 susehelp - the SUSE help center start 
 
 susehelp starts up the SUSE online help. The way the help is
 displayed depends on the system settings.
 
 For X-Windows susehelp starts either the SUSE KDE-helpcenter in
 case you are running KDE or one of konqueror, mozilla or netscape
 if the helpcenter could not be found.
 
 On terminals, the system is searched for a textbrowser like w3m.
 
 You can specify the browser you like to use setting the environment
 variables 'XBROWSER' or 'TEXTBROWSER'.
 
 susehelp knows two call arguments:
 
    -nox     -> Don't use a X Window browser, even while running
 		X Window.
    -x       -> Use the X Window browser
"
    exit
}


# Read in configuration variables
. /etc/sysconfig/susehelp
test -f /etc/sysconfig/apache2 && . /etc/sysconfig/apache2

# Do we want an X browser?

# Is there an X display?
if [ -z "$DISPLAY" ] ; then
    USE_X=0
else
    USE_X=1
fi

# Check command line arguments.
if [ $# -gt 0 ] ; then
    if [ $1 = "--help" -o $1 = "-help" -o $1 = "-h" ] ; then
        echo_help
    fi

    if [ $1 = "-nox" ] ; then
        USE_X=0
    else
        if [ $1 = "-x" ] ; then
            USE_X=1
        else
            URI="$1"
        fi
    fi

    if [ "$URI" = "" ]; then
      URI="$2"
    fi
fi

if [ "$URI" = "" ]; then
  URI="$SUSEHOMEPAGE"
fi

# Are we running KDE?
USE_DESKTOP=${WINDOWMANAGER##*/}

# Are we running a local doc server?
if [ -w /dev/stdout ]; then
    if curl -q -s -m 2 http://localhost/susehelp/ -o - | grep -q "<title>SUSE Help"; then
        USE_LOCALSERVER=1
    fi
else
    if [ "$DOC_SERVER" = "yes" ] && ps ax|grep -q [h]ttpd; then
        USE_LOCALSERVER=1
    fi
fi

# Which help browsers are available?
GNOME_HELP=$(which 2>/dev/null yelp)
KDE_HELP=$(which 2>/dev/null khelpcenter)

shlog USE_X: $USE_X
shlog USE_DESKTOP: $USE_DESKTOP
shlog USE_LOCALSERVER: $USE_LOCALSERVER

# If no external doc host is used and x is available start khelpcenter.
# For non-KDE desktops only start it, when no doc server is available
if [ "$DOC_HOST" = "localhost" ] && \
   [ "$USE_X" = 1 ]; then
    if [ \( "$USE_DESKTOP" = "gnome" -o "$USE_DESKTOP" = "startmoblin" \) -a "$GNOME_HELP" != "" ]; then
        $GNOME_HELP $URI
        exit 0
    else
        if [ "$USE_DESKTOP" = kde -o "$USE_DESKTOP" = startkde -o -z "$USE_LOCALSERVER" ]; then
            URI=$(echo $URI | sed 's/^ghelp:/help:/')
            if [ "$KDE_HELP" != "" ]; then
                susehelp-khelpcenter $URI
                exit 0
            else 
                echo $"SUSE help center not found. Trying doc server..."
            fi
        fi
    fi
fi

if [ "$DOC_HOST" = "localhost" -a -z "$USE_LOCALSERVER" ]; then
    echo $"\
Unable to find local doc server. If you want to use the doc server you
have to set the variable DOC_SERVER in /etc/sysconfig/apache2 to 'yes'
and start apache."
    exit 1
fi

shlog Use DocServer $DOC_HOST


# Now find a web browser

# If we have X, check X browsers first
if [ "$USE_X" = 1 ]; then

    # If XBROWSER is set and valid, use that.
    if [ -n "$XBROWSER" ] && type >/dev/null 2>&1 $XBROWSER; then
        BROWSER=$XBROWSER
    else
        # If we run KDE, use kfmclient
        if [ "$USE_DESKTOP" = kde ] && type >/dev/null 2>&1 kfmclient; then
            BROWSER="kfmclient openURL"
        else
            # Check standard X browsers
            for i in firefox mozilla netscape Mosaic xmosaic arena; do
                if type >/dev/null 2>&1 $i; then
                    BROWSER=$i
                    break
                fi
            done
        fi
    fi
fi

# If we still don't have a browser check text browsers
if [ -z "$BROWSER" ]; then
    # If TEXTBROWSER is set and valid, use that.
    if [ -n "$TEXTBROWSER" ] && type >/dev/null 2>&1 $TEXTBROWSER; then
        BROWSER=$TEXTBROWSER
    else
        for i in w3m links lynx ; do
            if type >/dev/null 2>&1 $i; then
                BROWSER=$i
                break
            fi
        done
    fi
fi

if [ -z "$BROWSER" ]; then
    echo $"Unable to find a web browser."
    exit 1;
fi

shlog Using browser $BROWSER


# check the language
LANGPATH=${LANG:-$LANGUAGE}
if [ -z "$LANGPATH" ] ; then
   . /etc/sysconfig/language

   LANGPATH=${LANGPATH:=${DEFAULT_LANGUAGE:-$RC_LANG}}
fi

# query for extension
case $LANGPATH in
     deutsch|german|de|de_*)     LANGPATH="de" ;;
     fran*ais|french|fr|fr_*)    LANGPATH="fr" ;;
     espa*ol|spanish|es|es_*|sp) LANGPATH="es" ;;
     italian*|it|it_*)           LANGPATH="it" ;;
     magyar|hungarian|hu|hu_*)   LANGPATH="hu" ;;
     cesky|czech|cz|cz_*)        LANGPATH="cz" ;;
     *)                          LANGPATH="en" ;;
esac

shlog "Using effective LangPath $LANGPATH"


if [ -z $SUSEHOMEPAGE ]; then
  HOMEPAGE=http://$DOC_HOST/susehelp/index.html.$LANGPATH
else
  HOMEPAGE=$SUSEHOMEPAGE
fi

shlog HOMEPAGE: $HOMEPAGE


echo $"Starting Browser..."
case "`basename $BROWSER`" in
    *etscape|*ozilla)
        $BROWSER -remote "openUrl($HOMEPAGE)" > /dev/null || \
            $BROWSER $HOMEPAGE > /dev/null &
        ;;
    *)
        exec $BROWSER $HOMEPAGE
        ;;
esac
