#!/bin/sh
#   picasa
#   Simple script to launch Picasa

# Run using linux32 if architecture is x86_64
if [ "`uname -m`" = "x86_64" ] ; then
	/usr/bin/linux32 "$0" "$@"
	exit $?
fi

close_splash() {
  if [ -n "$SPLASH_PID" ]; then
    kill "$SPLASH_PID" > /dev/null 2>&1
    SPLASH_PID=""
  fi
}

splash_on() {
  if [ -n "$SPLASH_PID" ]; then
    kill -10 "$SPLASH_PID" > /dev/null 2>&1
  fi
}

splash_off() {
  if [ -n "$SPLASH_PID" ]; then
    kill -12 "$SPLASH_PID" > /dev/null 2>&1
  fi
}

trap cleanup HUP INT QUIT ABRT TERM EXIT
cleanup() {
  set +ex
  close_splash
}

# Locate where our root is installed by looking for the directory
# where 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
}

need_to_add_picasa_handler()
{
    PICASA_ABSPATH=`which picasa`
    # look for Mozilla/Firefox profiles
    find -L ~/.mozilla -mindepth 3 -maxdepth 3 -name prefs.js 2> /dev/null | while read MOZPROFILE
    do
        USER_JS=`dirname "$MOZPROFILE"`/user.js
        egrep -qs '^user_pref\("network\.protocol-handler\.external\.picasa", true\)' "$USER_JS"
        if [ $? -gt 0 ] ; then
            return 1
        fi
        egrep -qs "^user_pref\(\"network\.protocol-handler\.app\.picasa\", \"$PICASA_ABSPATH\"" "$USER_JS"
        if [ $? -gt 0 ] ; then
            return 1
        fi
    done
}

add_picasa_handler_to_mozilla()
{
    # look for Mozilla/Firefox profiles
    PICASA_ABSPATH=`which picasa`
    find -L ~/.mozilla -mindepth 3 -maxdepth 3 -name prefs.js 2> /dev/null | while read MOZPROFILE
    do
        USER_JS=`dirname "$MOZPROFILE"`/user.js
        egrep -qs '^user_pref\("network\.protocol-handler\.app\.picasa",' "$USER_JS"
        if [ $? -gt 0 ] ; then
            # add picasa handler if it does not exist
            echo "user_pref(\"network.protocol-handler.app.picasa\", \"$PICASA_ABSPATH\");" >> "$USER_JS"
        else
            # If it exists, make sure the correct app path is set.
            sed -i -e "s,\(user_pref(\"network\.protocol-handler\.app\.picasa\"\).*,\1\, \"$PICASA_ABSPATH\");," "$USER_JS"
        fi

        egrep -qs '^user_pref\("network\.protocol-handler\.external\.picasa",' "$USER_JS"
        if [ $? -gt 0 ] ; then
            echo "user_pref(\"network.protocol-handler.external.picasa\", true);" >> "$USER_JS"
        else
            sed -i -e "s,\(user_pref(\"network\.protocol-handler\.external\.picasa\"\).*,\1\, true);," "$USER_JS"
        fi
    done
}


locate_pic_root

# Show the splash screen before any lengthy processing (e.g. Wine first run).
"$PIC_BINDIR/picasasplash" &
export SPLASH_PID="$!"

WINEPREFIX="$HOME/.google/picasa/3.0"
if [ -n "$PICASA_WINEPREFIX" ] ; then
    # User specified a prefix in the environment, use it
    WINEPREFIX="$PICASA_WINEPREFIX"
fi

# Try to avoid Wine crashes due to font problem, as described at:
# http://www.codeweavers.com/support/wiki/faq/fontissues
BADVER="Version 0.1.20060928"
FONTVER=$(WINEDEBUG=1 "$PIC_BINDIR"/wrapper winedumpfontver.exe.so "AR PL ZenKai Uni" 2> /dev/null)
rc=$?
if [ $rc -eq 42 ] ; then
  exit 1
fi
FONTVER=$(echo $FONTVER | grep "^winedumpfontver: Version" | cut -d" " -f 2-)
if [ $rc -eq 0 ] && [ "$FONTVER" = "$BADVER" ]; then
  FONTFIX="$PIC_BINDIR"/fontinstall.sh
  FONTPROMPT="
You have a version of the ukai.ttf font which is known to cause problems with
Picasa. A Picasa-compatible version of the font can be
installed automatically.  This will only affect Picasa, and will not
change the font currently installed on your system.

If you skip this step, you can perform it later by running:
$FONTFIX
For additional assistance, please visit:
http://groups.google.com/group/Google-Labs-Picasa-for-Linux

Would you like to install the updated font now?
"
  "$FONTFIX" -p "$FONTPROMPT" -d "$PIC_ROOT/wine/drive_c/windows/fonts" \
    -c "$WINEPREFIX/.fontfix"
fi

# Try to avoid having a missing "My Pictures" directory
"$PIC_BINDIR"/wrapper check_dir.exe.so

# Check log and look for hanging Picasa process
picasa_hung=0
if [ -f "$WINEPREFIX/picasa.log" ] ; then
    WAIT=`tail -n 2 "$WINEPREFIX/picasa.log" | grep -a "wait timed out in thread" | wc -l`
    if [ -n "$WAIT" ] ; then
        if [ "$WAIT" -eq "2" ] ; then
            picasa_hung=1
        fi
    fi
fi

if [ -z "$EUID" ] ; then
    IDPROG=$(which id)
    if [ $? -eq 0 ] ; then
        EUID=$(id -u)
    fi
fi

if [ "$picasa_hung" = "0" ] ; then
    # Check if Picasa is still lingering around in a zombie state
    PICASA_PIDS=`ps -o pid --no-headers -C Picasa3,Picasa3.exe`
    for p in $PICASA_PIDS ; 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" -eq "$peuid" ] ; then
                picasa_hung=1
                break
            fi
        fi
    done
fi

if [ "$picasa_hung" = "1" ] ; then
    # This does not affect the current script
    "$PIC_BINDIR/killpicasa" --force
fi

# First, scrape the theme from the environment, if we can
# "$PIC_BINDIR"/wrapper wdi SCRAPETHEME

# Do browser integration
need_to_add_picasa_handler
if [ $? -gt 0 ] ; then
    add_picasa_handler_to_mozilla

    # is the browser running?
    ps -C firefox-bin,mozilla-bin > /dev/null
    if [ $? -eq 0 ] ; then
        # let users know the change will take effect on the next restart
        splash_off
        "$PIC_BINDIR"/wrapper browser_prompt.exe.so
        splash_on
    fi
fi

# set internal browser language setting
"$PIC_BINDIR"/wrapper set_lang.exe.so

# Next is the "real" Picasa UI, so done with the splash.
close_splash

# Then, actually run Picasa
exec "$PIC_BINDIR"/wrapper license.exe.so  /gate "c:\Program Files\Google\Picasa3\Picasa3.exe" "$@"
