#!/bin/bash
#
# gvSIG. Desktop Geographic Information System.
#
# Copyright (C) 2007-2020 gvSIG Association.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# 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., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.
#
# For any additional information, do not hesitate to contact us
# at info AT gvsig.com, or visit our website www.gvsig.com.
#

# h2sql

ARGS="$@" # Can't use ${@/ , use ${ARGS/ instead


# Go into the gvSIG installation folder, just in case
x=$(dirname "$0")
cd "$x"
cd ..

GVSIG_INSTALL_FOLDER="$PWD"
GVSIG_CLASSPATH=""
CPSEP=":"

UNAME=$(uname -m -o -s | tr '[:upper:]' '[:lower:]')
# Use -s to identify cygwin
case "$UNAME" in
*cygwin*)
    OS="win"
    CPSEP=";"
    ;;
*darwin*)
    OS="darwin"
    CPSEP=":"
    CYGWIN="no"
    ;;
*win*)
    OS="win"
    CPSEP=";"
    ;;
*lin*)
    OS="lin"
    CPSEP=":"
    CYGWIN="no"
    ;;
*)
    OS="unknown"
    CPSEP=":"
    CYGWIN="no"
    ;;
esac


add_classpath() {
  GVSIG_CLASSPATH="$GVSIG_CLASSPATH${CPSEP}$1"
}


# Load gvSIG Andami jars and dependencies for the classpath
for i in "$GVSIG_INSTALL_FOLDER/lib/"*.jar ; do
  if [ "$i" != "$GVSIG_INSTALL_FOLDER/lib/*.jar" -a "$i" != "" ]; then
    add_classpath "$i"
  fi
done
for i in "$GVSIG_INSTALL_FOLDER/lib/"*.zip ; do
  if [ "$i" != "$GVSIG_INSTALL_FOLDER/lib/*.zip" -a "$i" != "" ]; then
    add_classpath "$i"
  fi
done

# Load H2 jars in the classpath
for i in "$GVSIG_INSTALL_FOLDER/gvSIG/extensiones/org.gvsig.h2spatial.app.mainplugin/lib/"*.jar ; do
  if [ "$i" != "$GVSIG_INSTALL_FOLDER/gvSIG/extensiones/org.gvsig.h2spatial.app.mainplugin/lib/*.jar" -a "$i" != "" ]; then
    add_classpath "$i"
  fi
done

if test -e "$GVSIG_INSTALL_FOLDER/gvSIG/extensiones/jre/bin/java" ; then
  JAVA="$GVSIG_INSTALL_FOLDER/gvSIG/extensiones/jre/bin/java"
else
  JAVA="java"
fi
exec "$JAVA" -cp "$GVSIG_CLASSPATH"  org.h2.tools.Shell $ARGS

