#!/bin/sh
# -------
# File:        modellus
# Description: simple wrapper to launch modellus
#              the script creates an user configuration directory
#              in $HOME/.modellus and populates it with a default
#              configuration file if empty
# Author:      Luis Garcia Gisbert <garcia_luigis@gva.es>
# 
# 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 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., 
# 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA
# --------

# --------
# variables
# --------

MODELLUS_JAR="/usr/share/java/modellus/Modellus#915.jar"
MODELLUS_CONF_DIR="$HOME/.modellus"
MODELLUS_SAMPLE_CONF="/usr/share/modellus/ModellusConfig.xml"

# --------
# functions
# --------

warn_msg(){
	echo "Warning: $1" >&2
	return 0
}

create_conf(){
	if mkdir "$MODELLUS_CONF_DIR" ; then
		cp "$MODELLUS_SAMPLE_CONF" "$MODELLUS_CONF_DIR/" || warn_msg "Unable to create initial Modellus configuration in \"$MODELLUS_CONF_DIR/\" from \"$MODELLUS_SAMPLE_CONF\""
	else
		warn_msg "Unable to create Modellus configuration dir \"$MODELLUS_CONF_DIR\""
	fi
	return 0
}

# --------
#   main
# --------

[ -d "$MODELLUS_CONF_DIR" ] || create_conf
CURRENT_DIR="$(pwd)"
[ ! -d "$MODELLUS_CONF_DIR" ] || cd "$MODELLUS_CONF_DIR"

rc=0
java -jar "$MODELLUS_JAR" "$@" || rc=$?
cd "$CURRENT_DIR"
exit $rc
