#!/bin/sh

# Copyright (c) 2004-2005 Sun Microsystems, Inc. All rights reserved. Use is
# subject to license terms.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

#
# Wrapper shell script for Linux or Solaris to launch the Java Screensaver 
# bridge for this screensaver.
#
# Executable: Gallery-bin
# JAR: GalleryRemoteScreenSaver.jar
# Class: com.gallery.GalleryRemote.GRScreenSaver
#
# Do not edit.  This file is automatically generated by the 
# Java Screensaver distribution
#

# Find architecture
UNAME=`uname`
if [ "$UNAME" = "Linux" ]; then
    ARCH=i386
elif [ "$UNAME" = "SunOS" ]; then
    ARCH=sparc
fi

# Find Java Runtime
# (Modify JAVA_HOME to suit your environment if this doesn't work)
# Currently JDK 1.5 is a minimum requirement for Linux and Solaris.

checkJDKHomeValid() {
    if [ ! -f ${jdkhome}/bin/java ] ; then
        jdkhome=
    fi
    # We should also test here if they have JDK 1.5 or later installed.
    # However, don't make this take long since this has to be run each time
    # a Java screensaver is started.
}
                      
# Use -jdkhome parameter if specified
PARAMS=""
while [ ! -z "$1" ] ; do
  if [ "$1" = "-jdkhome" ] ; then
    shift
    jdkhome=$1
    checkJDKHomeValid
  else
    PARAMS="${PARAMS} $1"
  fi
  shift
done
                                                          
# Use JAVA_HOME if set and exists
if [ -z "$jdkhome" -a ! -z "$JAVA_HOME" ] ; then
    jdkhome="$JAVA_HOME"
    checkJDKHomeValid
fi
                                                                                
# Else, if on Linux try using 'rpm -ql j2sdk'
if [ -z "$jdkhome" -a "$ARCH" = "i386" ] ; then
    jdkhome=`rpm -ql j2sdk | grep bin | head -1`
    jdkhome=`dirname $jdkhome`
    checkJDKHomeValid
fi
                                                                                
# Else, try using 'which java' to determine directory
if [ -z "$jdkhome" ] ; then
    jdkhome=`which java`
    jdkhome=`dirname $jdkhome`
    jdkhome=`dirname $jdkhome`
    checkJDKHomeValid
fi
                                                                                
# Else, error
if [ -z "$jdkhome" ] ; then
    echo Error: Could not find Java Virtual Machine.  Please set JAVA_HOME.
    exit 1
fi

# Set LD_LIBRARY_PATH:
LD_LIBRARY_PATH=${jdkhome}/jre/lib/${ARCH}:${jdkhome}/jre/lib/${ARCH}/client:.
export LD_LIBRARY_PATH

# Launch screensaver:
# Note: exec is important, else xscreensaver will not kill saverbeans
# Note: -jar and -class default to the correct values.
cd `dirname $0`
exec ./Gallery-bin $PARAMS
