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

productname="CrossOver Office"
processes="winedbg wineloader wineclipsrv"

# 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
}



# Parse the command line options
force=""
if [ -n "$1" ]
then
    case "$1" in
        -f)
            force=1
            ;;
        *)
            echo "Usage: `basename $0` [-f]"
            echo "$msg"
            echo "If the -f option is specified, then no confirmation is asked."
            exit 2
            ;;
    esac
fi

if [ -z "$force" ]
then
    locate_cx_root
    cxmessage="$cx_root/bin/cxmessage"

    # Warn the user
    "$cxmessage" \
        -title "$productname Reset" \
        -buttons "Cancel:0,Proceed:2" \
        -default "Cancel" \
        -image "data/cxreset.xpm" \
   "This utility provides the ability to terminate any running $productname processes and to recover from crashes.

By selecting Proceed, below, you will terminate
any Wine processes on your system."
    if [ $? -ne 2 ]
    then
        exit 1
    fi
fi

# Perform the killing and cleaning
killmore=0
for i in $processes
do
  killall $i 2>/dev/null
  if [ $? -eq 0 ]
  then
    killall -CONT $i 2>/dev/null
    killmore=1
  fi
done
if [ "$killmore" = "1" ]
then
  sleep 2
  killmore=0
  for i in $processes
  do
    killall -KILL $i 2>/dev/null
    if [ $? -eq 0 ]
    then
      killmore=1
    fi
  done
  if [ "$killmore" = "1" ]
  then
    sleep 2
  fi
  sleep 3
fi
killall wineserver 2>/dev/null
if [ $? -eq 0 ]
then
  killall -CONT wineserver 2>/dev/null
  sleep 5
  killall -KILL wineserver 2>/dev/null
fi

rm -rf "/tmp/.wine-`whoami`"/server-*

# Notify the user of the completion
if [ -z "$force" ]
then
    "$cxmessage" \
        -title "$productname Reset" \
        -buttons "Ok" \
        -default "Ok" \
        -image "data/cxreset.xpm" \
        "All CrossOver processes should now be stopped."
fi

exit 0
