#!/bin/sh
echo KolourPaint 1.2.2_kde3 Binary Release \#3
echo


echo -n Detecting wrapper location ..." "
if OUR_DIRECTORY="`dirname "$0"`"
then
    echo OK
else
    echo FAIL
    echo
    echo Unexpected error: could not determine wrapper location.
    echo For support, email "<kolourpaint-support@lists.sourceforge.net>".
    echo
    exit 1
fi


# kio_help is invoked by kioslave (with kioslave's environment), which will
# probably not contain our KDEDIRS, hence docs won't work.
#
# We have a few choices:
#
# 1. Make a symlink to our docs in $KDEHOME: Works because $KDEHOME is
#    always searched by KDE.  Bad because it won't work on readonly system
#    (not really an issue but...) and messes with the user's home
#    directory (only an issue if the user starts putting his/her custom
#    KolourPaint documentation there which is highly unlikely).
#    [summary: messes with home directory]
#
# 2. "dcop klauncher default setLaunchEnv": Requires klauncher to be
#    running or we'll have to invoke it.  Replaces, rather than appending
#    to, environment variable - impossible to restore properly without a
#    "getLaunchEnv" call (which doesn't exist).
#    [summary: may stuff up environment permanently]
#
# 3. Restart kdeinit with "KDEDIRS=$OUR_DIRECTORY:$KDEDIRS kdeinit": Too
#    slow.  Performance penalty on startup is awful; not to mention that
#    it'll need to be run again on kolourpaint exit to restore environment.
#    [summary: slow]
#
# All of the above solutions affect the environment for any apps started
# while this script is active :(
#
# Solution Chosen: #1
#

# Stolen from startkde 3.3
kdehome=$HOME/.kde
test -n "$KDEHOME" && kdehome=`echo "$KDEHOME"|sed "s,^~/,$HOME/,"`

# (if this fails, all the user loses is the docs - no need to abort)
echo -n Setting up HTML docs symlink ..." "
if ! mkdir -p "$kdehome/share/doc/HTML/en"
then
    echo Could not create "$kdehome/share/doc/HTML/en"
else
    DOC_LINK="$kdehome/share/doc/HTML/en/kolourpaint"
    if [ -L "$DOC_LINK" ]
    then
        # Sigh :(
        if ! rm -f "$DOC_LINK"
        then
            echo $DOC_LINK already exists as a link but cannot be deleted
            unset DOC_LINK
        fi
    elif [ -e "$DOC_LINK" ]
    then
        echo $DOC_LINK already exists but is not a link
        unset DOC_LINK
    fi
        
    if [ -n "$DOC_LINK" ]
    then
        DOC_LOCATION="$OUR_DIRECTORY/share/doc/HTML/en/kolourpaint"

        if [ -z "`echo "$DOC_LOCATION" | egrep "^/"`" ]
        then
            DOC_LOCATION="$PWD/$DOC_LOCATION"
        fi
        
        if ! ln -s "$DOC_LOCATION" "$DOC_LINK"
        then
            echo Could not create $DOC_LINK
            unset DOC_LINK
        else
            echo OK
        fi
    fi
fi


echo -n Adding $OUR_DIRECTORY to KDEDIRS=$KDEDIRS ..." "
export KDEDIRS="$OUR_DIRECTORY:$KDEDIRS"
echo OK


echo


echo Starting KolourPaint ...
if ! "$OUR_DIRECTORY/bin-do_not_enter/kolourpaint" "$@"
then
    echo

    # kde-config is rather slow - we only use it to check KDE ver at the
    # moment ...
    #
    # So _only_ run it as a diagnostic if kolourpaint aborts.  There is
    # no need to run it before the kolourpaint bin - it only slows startup.
    KDE_CONFIG_BIN="`type -P kde-config 2>/dev/null`"
    echo -n Checking for KDE 3.x using kde-config \($KDE_CONFIG_BIN\) ..." "
    if [ ! -z "$KDE_CONFIG_BIN" ] && [ -x "$KDE_CONFIG_BIN" ] &&
       [ ! -z "`kde-config --version | egrep ^KDE:\ 3\\.`" ]
    then
        echo Yes
    
        # Qt 3.0.5 has new ABI by accident and we (and most distros)
        # support it instead of e.g. Qt 3.0.4.  We still let them run
        # with Qt 3.0.4 at their own risk but if it fails, we do this
        # test.
        #
        # ASSUMPTION: If have KDE 3.x, then have Qt 3.x.
        echo -n 'Checking for Qt 3 (Qt >= 3.0.5) ... '
        if [ ! -z "`kde-config --version | egrep '^Qt: 3\.([^0]|0\.[5-9])'`" ]
        then
            echo Yes

            echo
            echo If KolourPaint aborted unexpectedly, email
            echo "<kolourpaint-support@lists.sourceforge.net>" for support.
        else
            echo FAIL

            echo
            echo "KolourPaint requires Qt 3 (>= 3.0.5)."
        fi
    else
        echo FAIL
        
        echo
        echo "KolourPaint requires KDE 3.x."
    fi

    
    [ -n "$DOC_LINK" ] && [ -L "$DOC_LINK" ] && rm -f "$DOC_LINK"
    
    echo
    exit 1
fi
  

[ -n "$DOC_LINK" ] && [ -L "$DOC_LINK" ] && rm -f "$DOC_LINK"

echo

