#!/bin/bash
#
# This script is designed to run the configuration tool.
# The script assumes all RPM dependencies were installed, so jar
# files can be found under /usr/share/java. The tool's configuration
# should be under the /etc directory.
#

die () {
        printf >&2 "$@"
        exit 1
}

usage () {
        printf "rhevm-config: get/set/list configuration\n"
        printf "USAGE:\n"
        printf "\trhevm-config ACTION [--cver=version] [-p | --properties=/path/to/alternate/property/file] [-c | --config=/path/to/alternate/config/file]\n"
        printf "Where:\n"
        printf "\tACTION              action to perform, see details below\n"
        printf "\tversion             relevant configuration version to use.\n"
        printf "\t-p, --properties=   (optional) use the given alternate properties file.\n"
        printf "\t-c, --config=       (optional) use the given alternate configuration file.\n"
        printf "\n"
        printf "\tAvailable actions:\n"
        printf "\t-l, --list\n"
        printf "\t\tlist available configuration keys.\n"
        printf "\t-a, --all\n"
        printf "\t\tget all available configuration values.\n"
        printf "\t-g key, --get=key [--cver=version]\n"
        printf "\t\tget the value of the given key for the given version. If a version is not given, the values of all existing versions are returned.\n"
        printf "\t-s key=val --cver=version, --set key=val --cver=version\n"
        printf "\t\tset the value of the given key for the given version. The cver version is mandatory for this action.\n"
        printf "\t-h, --help\n"
        printf "\t\tdisplay this help and exit.\n"
        printf "\n"
        printf "### Note: In order for your change(s) to take effect,\n"
        printf "### restart the JBoss service (using: 'service jbossas restart').\n"
        printf "#############################################################################\n"

        return 0
}

# Import configurations
PUSHDIR=`readlink -f $0`
pushd `dirname $PUSHDIR` > /dev/null

# Support alternate configuration file
CONF_FILE=/etc/rhevm/rhevm-config/rhevm-config.conf

found=0
for ((i=1; i<=$# && ! found; i++))
do
        var="${!i}"
        next=$[$i+1]
        next="${!next}"

        if [ "-c" == "${var}" ]; then
                CONF_FILE="${next}"
                found=1
        elif [ `echo "${var}" | grep -i '\-\-config\='` ]; then
                candidate=${var#--config=}
                if [ -s $candidate ]; then
                        CONF_FILE=$candidate
                else
                        CONF_FILE=
                fi
                found=1
        fi
done

if [ ${found} -eq 1 -a "x" == "x$CONF_FILE" ]; then
        die "Error! Alternate conf file '$candidate' is empty or does not exist!\n"
fi

if [ ! -s $CONF_FILE ]; then
        CONF_FILE=./rhevm-config.conf
fi
. $CONF_FILE

# Check basic argument constraints
if [ "$#" -gt 8 -o "$#" -lt 1 ]; then
        usage
        die "Error: wrong argument number: $#.\n"
fi


if [ "$1" == "--help" -o "$1" == "-h" ]; then
        usage
        exit 0
fi

# Verify all classpath elements available
JAVA_LIB_HOME=/usr/share/java
LOCAL_LIB=lib
CP=$JAVA_LIB_HOME/commons-logging.jar:$JAVA_LIB_HOME/commons-collections.jar:$JAVA_LIB_HOME/commons-lang.jar:$JAVA_LIB_HOME/commons-configuration.jar:$JAVA_LIB_HOME/commons-jxpath.jar:$JAVA_LIB_HOME/log4j.jar:$JAVA_LIB_HOME/commons-codec.jar:$JAVA_LIB_HOME/postgresql-jdbc.jar:$LOCAL_LIB/rhevmencryptutils-3.0.0-0001.jar:$LOCAL_LIB/rhevm-compat.jar:$LOCAL_LIB/rhevm-config.jar:$JAVA_LIB_HOME/rhevm-tools-common.jar

for f in $(echo $CP|sed 's/:/ /g')
do
        if [ ! -s $f ]; then
                die "Error: can't run without missing JAR file: $f\n"
        fi
done

# Run!
java -cp .:$CP org.ovirt.engine.core.config.RhevmConfig "$@"

exit $?
