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

# 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=`readlink -f $0`
    # look for Mozilla/Firefox profiles
    find -L ~/.mozilla -mindepth 3 -maxdepth 3 -name prefs.js 2> /dev/null | while read
    do
        USER_JS=`dirname "$REPLY"`/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=`readlink -f $0`
    find -L ~/.mozilla -mindepth 3 -maxdepth 3 -name prefs.js 2> /dev/null | while read
    do
        USER_JS=`dirname "$REPLY"`/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

# Check for the 32-bit OpenSSL libs that Picasa needs.
if [ "`uname -m`" = "x86_64" ] ; then
    SSLSETUP="$PIC_BINDIR/repackage32.sh"
    SSLPROMPT="Many online features of Picasa will not work
until you install a 32-bit version of OpenSSL.
If you skip this step, you can perform it later by running:
$SSLSETUP openssl
For additional assistance, please visit:
http://groups.google.com/group/Google-Labs-Picasa-for-Linux

Would you like to install 32-bit OpenSSL now?"
    "$SSLSETUP" -p "$SSLPROMPT" openssl
fi

WINEPREFIX="$HOME/.picasa"
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)
if [ $? -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 [ "$picasa_hung" = "0" ] ; then
    # Check if Picasa is still lingering around in a zombie state
    PICASA_PIDS=`ps -o pid --no-headers -C Picasa2,Picasa2.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

# We only run the media detector if the option is not disabled
registry_export=`mktemp /tmp/picasa.mediacheck.reg.XXXXXX`

"$PIC_BINDIR/wrapper" regedit /E $registry_export HKEY_USERS\\S-1-5-4\\Software\\Google\\Picasa\\Picasa2\\Preferences\\
rc=$?
if [ $rc -gt 0 ] ; then
    exit $rc
fi
grep -q 'DisableMediaDetector.*001' $registry_export
rc=$?
rm -f $registry_export

if [ $rc -ne 0 ] ; then
    "$PIC_BINDIR"/mediadetector
    rc=$?
    if [ $rc -ne 0 ] ; then
      exit $rc
    fi
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
        "$PIC_BINDIR"/wrapper browser_prompt.exe.so
    fi
fi

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

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