#!/bin/bash
#       killpicasa
#   Script to kill all running instances of Picasa
#   
#   With --force, it will not prompt for confirmation


# Locate where our root is installed by looking for the directory
# where this this script is located, unwinding symlinks on the way
locate_pic_root()
{
    if [ -z "$PIC_ROOT" ]
    then
        argv0=`which "$0"`
        [ -z "$argv0" ] && argv0="$0"
        echo "$argv0" | egrep "^/" >/dev/null
        if [ $? -ne 0 ]
        then
            argv0="`pwd`/$argv0"
        fi
        dir=`dirname "$argv0"`
        while [ ! -x "$dir/../wine/bin/wine" ]
        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%\(/\./\(\./\)*\)%/%'`
        PIC_ROOT="$dir/.."
        PIC_BINDIR="$dir"
    fi
    if [ ! -x "$PIC_ROOT/wine/bin/wine" ]
    then
        if [ "$1" = "--no-fail" ]
        then
            PIC_ROOT=""
            return 1
        fi
        echo "`basename \"$0\"`:error: could not find wine/bin/wine in '$PIC_ROOT'" >&2
        exit 1
    fi
    # Make sure it's exported
    export PIC_ROOT
    export PIC_BINDIR
    return 0
}


warn_user() {

    # First, send it to stdout for the power users who like looking at the console

    echo "$@"

    # Then send it to a GUI thingy for everyone else
    MESSAGE=true
    type xmessage > /dev/null 2>&1 && MESSAGE=xmessage
    type gmessage > /dev/null 2>&1 && MESSAGE=gmessage
    type kmessage > /dev/null 2>&1 && MESSAGE=kmessage

    $MESSAGE -center -buttons "    Continue    ":7,"    Quit    ":8 -title "Warning" "$@"
    case $? in
        7)  return 7;;         # user picked 'Continue'
    	8)  return 8 ;;  # user picked 'Quit'
    esac

    # Fall back to the console if there is no clear answer
    echo Enter YES to proceed
    read ans
    if [ "$ans" = "YES" ] ; then
        return 7
    fi

    return 1
}

# Return a list of running processes that are clearly connected to our
#   instance of Picasa, or Picasa processes that have been zombied and
#   have no connected executable
running_picasa_processes() {
    let showspace=0
    match_inode=`stat --dereference --format %i "$PIC_ROOT/wine/bin/wine-preloader"`
    pid_list=`ps -o pid --no-headers -C wine-preloader,Picasa2,Picasa2.exe,PicasaMediaDetector,PicasaRestore.exe,explorer.exe,winefile.exe`
    for p in $pid_list ; do
        my_inode=`stat --dereference --format %i /proc/$p/exe 2>/dev/null`
        if [ "$my_inode" = "$match_inode" ] ; then
            if [ $showspace -gt 0 ] ;then
                echo -n " "
            fi
            echo -n "$p"
            let showspace=1
        fi
    done

    pid_list=`ps -o pid --no-headers -C Picasa2,Picasa2.exe,PicasaMediaDetector,PicasaRestore.exe,explorer.exe`
    for p in $pid_list ; do
        zombiecheck=`ps -o stat --no-headers -p $p | grep Z`
        peuid=`ps -o euid --no-headers -p $p`
         if [ -n "$zombiecheck" -a -n "$peuid" ] ; then
            if [ "$EUID" = "0" -o "$EUID" -eq "$peuid" ] ; then
                if [ $showspace -gt 0 ] ;then
                    echo -n " "
                fi
                echo -n "$p"
                let showspace=1
            fi
        fi
    done
}

# Ask the running wineservers to shut things down...
ask_servers_to_shutdown() {
    let showspace=0
    match_inode=`stat --dereference --format %i "$PIC_ROOT/wine/bin/wineserver"`
    pid_list=`ps -o pid --no-headers -C wineserver`
    for p in $pid_list ; do
        my_inode=`stat --dereference --format %i /proc/$p/exe 2>/dev/null`
        if [ "$my_inode" = "$match_inode" ] ; then
	    serveruser=`ps -o user --no-headers $p`
	    if [ "$USER" != "$serveruser" ] ; then
	    	su -c "$PIC_ROOT/bin/wrapper wineserver -k" - "$serveruser"
	    else
	    	"$PIC_ROOT/bin/wrapper" wineserver -k
	    fi
        fi
    done

}


killpicasa() {
    locate_pic_root

    pids=`running_picasa_processes`

    if [ -z "$pids" ] ; then
        exit 0
    fi

    if [ "$1" != "--force" ] ; then
        pids_desc=`ps -o user,pid,cmd -wp "$pids"  | sort`
        newline=`echo " "`
        warn_user "We are about to attempt to stop the following processes:  
    $pids_desc"
        if [ $? -ne 7 ] ; then
            exit 1
        fi
    fi

    ask_servers_to_shutdown

    let x=0
    while [ $x -lt 5 ] ; do
        sleep 1
        pids=`running_picasa_processes`
        if [ -z "$pids" ] ; then
            exit 0
        fi
        let x=x+1
    done

    echo Normal shutdown did not work, invoking kill
    kill $pids
    let x=0
    while [ $x -lt 5 ] ; do
        sleep 1
        pids=`running_picasa_processes`
        if [ -z "$pids" ] ; then
            exit 0
        fi
        let x=x+1
    done


    echo A normal kill did not work after 5 seconds, trying kill -9
    kill -9 $pids
}
killpicasa "$@"

