#!/bin/bash

# include user-defined overrides and customization
if [ -f ~/.sql-dk/environment.sh ]; then # .sql-dk must match with DIR in Constants.java
    . ~/.sql-dk/environment.sh
fi

# link/copy support files for configuration
[ -f ~/.sql-dk/config.xsd ] || ln -s /usr/share/doc/sql-dk/config.xsd ~/.sql-dk/config.xsd
[ -f ~/.sql-dk/config.rnc ] || ln -s /usr/share/doc/sql-dk/config.rnc ~/.sql-dk/config.rnc
[ -f ~/.sql-dk/config.xsl ] || cp /usr/share/doc/sql-dk/config.xsl ~/.sql-dk/config.xsl # might not work in www browser if just symlinked


# Standard JDBC drivers
[ -n "$STANDARD_JDBC" ] || STANDARD_JDBC=(
	"/usr/share/java/postgresql-jdbc4.jar"
	"/usr/share/java/mysql.jar"
);

# Additional JDBC drivers might be specified in CUSTOM_JDBC array
# Plugins (formatters etc.) might be specified in PLUGINS array

# JAR containing SQL-DK application
[ -n "$JAR" ] || JAR="/usr/share/sql-dk/sql-dk.jar";

CLASS_PATH="$JAR";

for e in "${STANDARD_JDBC[@]}" "${CUSTOM_JDBC[@]}" "${PLUGINS[@]}"; do
	CLASS_PATH="$CLASS_PATH:$e";
done


MAIN_CLASS="info.globalcode.sql.dk.CLIStarter";

LOGGER="-Djava.util.logging.config.class=info.globalcode.sql.dk.logging.LoggerInitializer";
[ -n "$LOG_LEVEL" ] && LOGGER_LEVEL="-Dinfo.globalcode.sql.dk.logging.LoggerInitializer.level=$LOG_LEVEL";

java $LOGGER $LOGGER_LEVEL -cp "$CLASS_PATH" $MAIN_CLASS "$@"
