#!/bin/sh
#
# Sets Picasa as the default Gnome camera handler
#
# Copyright 2008 Google Inc. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
#   copyright notice, this list of conditions and the following disclaimer
#   in the documentation and/or other materials provided with the
#   distribution.
# * Neither the name of Google Inc. nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Include common functions.
export SCRIPTDIR=$(readlink -f $(dirname "$0"))
. "$SCRIPTDIR/common.sh"

usage() {
  SEP="-------------------------------------------------------"
  prompt_info "
$SEP
This program sets Picasa as the default Gnome camera handler
$SEP
Usage: $(basename $0) [-n] [-c config_file] [-v]
  -c  Config file to store preferences (i.e. 'don't ask again')
  -n  Don't actually install, just show what would happen.
  -v  Be verbose, even when no actions taken or errors encountered.
  -h  Show this help message.
$SEP"
}

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

is_supressed() {
  GNOMEHALID=$1
  grep -qs "$GNOMEHALID 1 $(hostname -f)" "$CFGFILE"
  return $?
}

#=========
# MAIN
#=========
while getopts ":nc:vh" OPTNAME
do
  case $OPTNAME in
    n )
      enable_dry_run
      ;;
    c )
      CFGFILE="$OPTARG"
      ;;
    v )
      VERBOSE=1
      ;;
    h )
      usage
      exit 0
      ;;
    \: )
      prompt_err "'-$OPTARG' needs an argument."
      usage
      exit 1
      ;;
    * )
      prompt_err "invalid command-line option: $OPTARG"
      usage
      exit 1
      ;;
  esac
done
shift $(($OPTIND - 1))

GCONFTOOL=$(which gconftool-2)
if [ -z "$GCONFTOOL" ] ; then
  GCONFTOOL=$(which gconftool-1)
fi
if [ -z "$GCONFTOOL" ] ; then
  GCONFTOOL=$(which gconftool)
fi
if [ -z "$GCONFTOOL" ] ; then
  exit 1;
fi

GNOMEPHOTOCMD=$("$GCONFTOOL" --get /desktop/gnome/volume_manager/autophoto_command 2> /dev/null)
if [ $? -ne 0 ]; then
  exit 1
fi

echo $GNOMEPHOTOCMD | egrep -qs "^$SCRIPTDIR/gnomehal.sh %h$"
if [ $? -eq 0 ]; then
  exit 0
fi

# Now that we're past general usage errors, we'll allow GUI output.
if [ ! $DRYRUN ]; then
  enable_gui_output
fi

GNOMEHALID="GNOMEHAL"
is_supressed "$GNOMEHALID"
if [ $? -eq 0 ]; then
  exit 0
fi

SELF=$(readlink -f "$0")
PROMPTMSG="
When Gnome detects a camera, it offers you the choice to import from it.
Picasa can be integrated with Gnome, such that pressing import
launches Picasa and takes you to the import screen.

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

Would you like Gnome's camera import button to launch Picasa?
"

# Don't offer the "don't ask again" option if we can't save the preference.
touch "$CFGFILE" > /dev/null 2>&1
if [ $? -eq 0 ]; then
  prompt_yesno_never "$PROMPTMSG"
else
  prompt_yesno "$PROMPTMSG"
fi
RES=$?
if [ $RES -gt 0 ]; then
  if [ $RES -eq 2 ]; then
    echo "$GNOMEHALID 1 $(hostname -f)" >> "$CFGFILE"
  fi
  exit 1
fi

if [ ! $DRYRUN ]; then
  "$GCONFTOOL" --set /desktop/gnome/volume_manager/autophoto_command -t string "$SCRIPTDIR/gnomehal.sh %h"
fi
