#!/bin/sh
# (c) Copyright 2001-2003. CodeWeavers, Inc.


# Setup logging
if [ -n "$CX_LOG" ]
then
    exec 2>>"$CX_LOG"
    echo >&2
    echo "***** `date`" >&2
    echo "Starting: $0 $@" >&2
    set -x
fi

productid="cxoffice"


# Portable which(1) implementation
cxwhich()
{
    case "$1" in
    /*)
        if [ -x "$1" ]
        then
            echo "$1"
            return 0
        fi
        ;; 
    */*)
        if [ -x "`pwd`/$1" ]
        then
            echo "`pwd`/$1"
            return 0
        fi
        ;;
    *)
        saved_ifs="$IFS"
        IFS=":"
        for d in $PATH
        do
            IFS="$saved_ifs"
            if [ -n "$d" -a -x "$d/$1" ]
            then
                echo "$d/$1"
                return 0
            fi
        done
        ;;
    esac
    return 1
}

# Locate where CrossOver is installed by looking for the directory
# where this this script is located, unwinding symlinks on the way
cx_root=""
locate_cx_root()
{
    [ -n "$cx_root" ] && return 0
    cx_root="$CX_ROOT"
    if [ -z "$cx_root" ]
    then
        argv0=`cxwhich "$0"`
        [ -z "$argv0" ] && argv0="$0"
        echo "$argv0" | egrep "^/" >/dev/null
        if [ $? -ne 0 ]
        then
            argv0="`pwd`/$argv0"
        fi
        dir=`dirname "$argv0"`
        while [ ! -x "$dir/cxtcl" ]
        do
            [ ! -h "$argv0" ] && break
            argv0=`ls -l "$argv0" | sed -e 's/^.*-> //'`
            echo "$argv0" | egrep "^/" >/dev/null
            if [ $? -ne 0 ]
            then
                argv0="$dir/$argv0"
            fi
            dir=`dirname "$argv0"`
        done
        dir=`echo "$dir" | sed -e 's%\(/\.\)*$%%' -e 's%\(/\./\(\./\)*\)%/%'`
        cx_root=`dirname "$dir"`
    fi
    if [ ! -x "$cx_root/bin/cxtcl" ]
    then
        echo "`basename \"$0\"`:error: could not find CrossOver in \"$cx_root\"" >&2
        exit 1
    fi
    CX_ROOT="$cx_root"
    export CX_ROOT
    return 0
}

locate_cx_root


usage()
{
    echo "Usage: `basename \"$0\"` [--crossover] [--prefix prefix]" >&2
    echo "Options:" >&2
    echo "  --crossover    create CrossOver menu entries" >&2
    echo "  --prefix       create CrossOver menu entries not at the root, but in prefix" >&2
    exit 2
}

if [ $# -eq 0 ]
then
    usage
fi

prefix="<auto>"
mode="create-crossover"
while [ "$#" -gt 0 ]
do
  case "$1" in
    --crossover)
        mode="create-crossover"
        shift
        ;;
    --remove-crossover)
        mode="remove-crossover"
        shift
        ;;
    --windows-applications)
        mode="windows-applications"
        shift
        ;;
    --prefix) 
        shift
        if [ "$#" -le 0 ] ; then
            echo "Error:  you must specify a prefix with the --prefix option"
            exit 2
        fi
        prefix="$1"
        shift
        ;;
    *) usage ;;
  esac
done

if [ "$prefix" = "<auto>" ]
then
    if [ -f "/etc/xandros-desktop-version" -a "$mode" != "windows-applications" ]
    then
        prefix="/Applications"
    else
        prefix=""
    fi
fi

type kde-config >/dev/null 2>&1
if [ $? = 0 ]
then
    kde_apps=`kde-config --path apps 2>/dev/null`
    rc=$?
    kde_user_prefix=`kde-config --localprefix 2>/dev/null`
    rc=`expr $rc + $?`
    kde_global_prefix=`kde-config --prefix 2>/dev/null`
    rc=`expr $rc + $?`
    kde_user_config=`kde-config --path config 2>/dev/null`
    rc=`expr $rc + $?`
    kde_version=`kde-config --version 2>/dev/null`
    rc=`expr $rc + $?`
    if [ $rc -eq 0 ]
    then
       kde_cached=1
       export kde_cached kde_apps kde_user_prefix kde_global_prefix kde_user_config kde_version
    fi
fi
type gnome-config >/dev/null 2>&1
if [ $? = 0 ]
then
    gnome_datadir=`gnome-config --datadir`
    gnome_cached=1
    export gnome_cached gnome_datadir
fi

if [ "$mode" = "create-crossover" ]
then
    if [ -d "$cx_root/support/dotwine" ]
    then
        rootonly_setup="--rootonly"
    else
        rootonly_setup=""
    fi
    "$cx_root/bin/cxmenu" --menu --createdir \
        --icon "$cx_root/support/crossover.xpm" \
        --link "$prefix/CrossOver"
    "$cx_root/bin/cxmenu" --menu $rootonly_setup \
        --icon "$cx_root/support/crossover.xpm" \
        --link "$prefix/CrossOver/OfficeSetup" \
        --name "Office Setup" \
        --path sh \
        --args "-c \"$cx_root/bin/cxsetup\""
    "$cx_root/bin/cxmenu" --menu \
        --icon "$cx_root/support/cxreset.xpm" \
        --link "$prefix/CrossOver/Reset CrossOver Office" \
        --path "$cx_root/bin/cxreset"
    "$cx_root/bin/cxmenu" --menu \
        --icon "$cx_root/support/cxreboot.xpm" \
        --link "$prefix/CrossOver/Simulate Windows Reboot" \
        --path "$cx_root/bin/cxreboot"
    "$cx_root/bin/cxmenu" --menu \
        --icon "$cx_root/support/cxdoc.xpm" \
        --link "$prefix/CrossOver/Office Documentation" \
        --path BROWSER \
        --args "file://$cx_root/doc/index.html"
    update-menus >/dev/null 2>&1
fi


if [ "$mode" = "remove-crossover" ]
then
    for menu in "Office Documentation" \
                "OfficeSetup" \
                "Office Setup" \
                "Office Uninstall" \
                "Reset CrossOver Office" \
                "Simulate Windows reboot" \
                "Simulate Windows Reboot" \
                "Uninstall Office"
    do
        "$cx_root/bin/cxmenu" --menu --remove --link "$prefix/CrossOver/$menu"
    done
fi

if [ "$mode" = "windows-applications" ]
then
    conffile="$cx_root/support/dotwine/cxbottle.conf"
    if [ ! -f "$conffile" ]
    then
        conffile="$cx_root/etc/$productid.conf"
    fi
    if [ -f "$conffile" ]
    then
        menuroot=`sed -e 's/^ *MenuRoot= *\(.*[^ ]\) *$/\1/' -e 't' -e 'd' "$conffile"`
        # expand ~ and environment variables
        menuroot=`eval echo $menuroot`

        if [ -n "$menuroot" ]
        then
            "$cx_root/bin/cxmenu" --menu --createdir \
                --icon "$cx_root/support/cxmenu.xpm" \
                --link "$menuroot"
        fi
    fi
fi

exit 0
