#!/bin/bash
#
# This script is designed to run the manage domains utility.
# 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 "$@\n"
        exit 1
}

# Import configurations
PUSHDIR=`readlink -f $0`
pushd `dirname $PUSHDIR` > /dev/null
CONF_DIR=/etc/rhevm/rhevm-manage-domains
CONF_FILE=$CONF_DIR/rhevm-manage-domains.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 '\-configFile\='` ]; then
                candidate=${var#-configFile=}
                if [ -s $candidate ]; then
                        CONF_FILE=$candidate
                else
                        die "Error: Alternate conf file $candidate is either empty or does not exist"
                fi
                found=1
        fi
done

if [ ! -s $CONF_FILE ]; then
		die "Error: Configuration file $CONF_FILE is either empty or does not exist"
fi


. $CONF_FILE


usage () {
        printf "rhevm-manage-domains: add/edit/delete/validate/list domains\n"
        printf "USAGE:\n"
        printf "\trhevm-manage-domains -action=ACTION [-domain=DOMAIN -user=USER -passwordFile=PASSWORD_FILE -interactive -configFile=PATH] -report\n"
        printf "Where:\n"
        printf "\tACTION             action to perform (add/edit/delete/validate/list). See details below.\n"
        printf "\tDOMAIN             	(mandatory for add, edit and delete) the domain you wish to perform the action on.\n"
        printf "\tUSER   			 (optional for edit, mandatory for add) the domain user.\n"
        printf "\tPASSWORD_FILE   		 (optional for edit, mandatory for add) a file containing the password in the first line.\n"
	printf "\tinteractive        alternative for using -passwordFile - read the password interactively.\n"
        printf "\tPATH               (optional) use the given alternate configuration file.\n"
        printf "\n"
        printf "\tAvailable actions:\n"
        printf "\tadd\n"
		printf "\tExamples:\n"
		printf "\t\t-action=add -domain=example.com -user=admin -passwordFile=/tmp/.pwd\n"
		printf "\t\t\tAdd a domain called example.com, using user admin and read the password from /tmp/.pwd.\n"
		printf "\t\t-action=edit -domain=example.com -passwordFile=/tmp/.new_password\n"
		printf "\t\t\tEdit the domain example.com, using another password file.\n"
		printf "\t\t-action=delete -domain=example.com\n"
		printf "\t\t\tDelete the domain example.com.\n"
		printf "\t\t-action=validate\n"
		printf "\t\t\tValidate the current configuration (go over all the domains, try to authenticate to each domain using the configured user/password.).\n"
		printf "\t\t-report In combination with -action=validate will report all validation error, if occured.\n"
		printf "\t\t\tDefault behaviour is to exit when a validation error occurs.\n"
		printf "\t\t-action=list\n"
		printf "\t\t\tLists the current configuration.\n"
		printf "\t\t-h\n"
		printf "\t\t\tShow this help.\n"

        return 0
}

if [ "$#" -gt 5 -o "$#" -lt 1 ]; then
        usage
		exit 1
fi


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

# Take configuration from configuration file
RHEVM_LIB_PATH=$rhevmLibPath
JAVA_LIB_HOME=/usr/share/java
LOCAL_LIB=lib

if [ ! -d $RHEVM_LIB_PATH ]; then
		die "Error: RHEV-M library path does not exist"
fi

if [ ! -d $JAVA_LIB_HOME ]; then
		die "Error: java library path does not exist"
fi

CP=$CONF_DIR:$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/rhevm-compat.jar:$JAVA_LIB_HOME/rhevm-tools-common.jar:$RHEVM_LIB_PATH/utils-3.0.0-0001.jar:$RHEVM_LIB_PATH/rhevm-common.jar:$RHEVM_LIB_PATH/spring-tx-2.5.6.SEC02.jar:$RHEVM_LIB_PATH/commons-lang-2.4.jar:$JAVA_LIB_HOME/spring2/beans-2.5.6.jar:$JAVA_LIB_HOME/spring2/core-2.5.6.jar:$RHEVM_LIB_PATH/spring-ldap-core-1.3.0.RELEASE.jar:$RHEVM_LIB_PATH/rhevmencryptutils-3.0.0-0001.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

PROPERTIES_FILE=`mktemp`

if [ ! -e $PROPERTIES_FILE ]; then
	die "Error: Temporary properties file cannot be created\n"
fi

cat << EOF > $PROPERTIES_FILE
AdUserName
AdUserPassword.type=CompositePassword
LDAPSecurityAuthentication
DomainName
AdUserId
LdapServers
EOF

java -cp .:$CP org.ovirt.engine.core.utils.kerberos.ManageDomains "$@" -propertiesFile=$PROPERTIES_FILE

RET_VAL=$?

rm $PROPERTIES_FILE

exit $RET_VAL

