#!/bin/sh
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
#
# The contents of this file are subject to the terms
# of the Common Development and Distribution License
# (the License). You may not use this file except in
# compliance with the License.
#
# You can obtain a copy of the License at
# https://opensso.dev.java.net/public/CDDLv1.0.html or
# opensso/legal/CDDLv1.0.txt
# See the License for the specific language governing
# permission and limitations under the License.
#
# When distributing Covered Code, include this CDDL
# Header Notice in each file and include the License file
# at opensso/legal/CDDLv1.0.txt.
# If applicable, add the following below the CDDL Header,
# with the fields enclosed by brackets [] replaced by
# your own identifying information:
# "Portions Copyrighted [year] [name of copyright owner]"
#
# $Id: setup,v 1.11 2009/01/28 05:34:46 ww203982 Exp $
#

# Portions Copyrighted 2010-2015 ForgeRock AS.

TOOLS_HOME="."

if [ -z "$JAVA_HOME" ]; then
  echo "Please define JAVA_HOME environment variable before running this program"
  echo "setup program will use the JVM defined in JAVA_HOME for all the CLI tools"
  exit 1
fi

if [ ! -x "$JAVA_HOME"/bin/java ]; then
  echo "The defined JAVA_HOME environment variable is not correct"
  echo "setup program will use the JVM defined in JAVA_HOME for all the CLI tools"
  exit 1
fi

AWK=`which awk`
if [ -z $AWK ]; then
    echo "setup fails because awk is not found"
    exit 1
fi

JAVA_VER=`$JAVA_HOME/bin/java -version 2>&1 | $AWK -F'"' '{print $2}'`

case $JAVA_VER in
1.0* | 1.1* | 1.2* | 1.3*)
  echo "This program is designed to work with 1.4 or newer JRE."
  exit 0
  ;;
esac

while test $# -gt 0
do
    case "$1" in
        --acceptLicense) accept_license=--acceptLicense
                    ;;
        -h) help_print=yes
            ;;
        --help) help_print=yes
            ;;
        -d) shift
        	path_debug=$1
            ;;
    	--debug) shift
        	path_debug=$1
            ;;
        -p) shift
        	path_AMConfig=$1
            ;;
    	--path) shift
        	path_AMConfig=$1
            ;;
        -l) shift
        	path_log=$1
            ;;
    	--log) shift
        	path_log=$1
            ;;
    esac
    shift
done

CLASSPATH=""
CLASSPATH="$CLASSPATH:$TOOLS_HOME/lib/openam-dtd-schema-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/slf4j-api-1.7.5.jar:$TOOLS_HOME/lib/grizzly-framework-2.3.23.jar:$TOOLS_HOME/lib/opendj-core-3.0.0.jar:$TOOLS_HOME/lib/i18n-slf4j-1.4.2.jar:$TOOLS_HOME/lib/openam-license-manager-cli-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/forgerock-guava-collect-18.0.3.jar:$TOOLS_HOME/lib/openam-entitlements-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/opendj-config-3.0.0.jar:$TOOLS_HOME/lib/opendj-server-legacy-3.0.0.jar:$TOOLS_HOME/lib/opendj-grizzly-3.0.0.jar:$TOOLS_HOME/lib/commons-lang-2.6.jar:$TOOLS_HOME/lib/org.apache.servicemix.bundles.javax-inject-1_2.jar:$TOOLS_HOME/lib/chf-http-core-3.0.1.jar:$TOOLS_HOME/lib/opendj-cli-3.0.0.jar:$TOOLS_HOME/lib/commons-collections-3.2.1.jar:$TOOLS_HOME/lib/json-20090211.jar:$TOOLS_HOME/lib/forgerock-util-3.0.2.jar:$TOOLS_HOME/lib/joda-time-2.7.jar:$TOOLS_HOME/lib/openam-license-core-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/forgerock-guava-base-18.0.3.jar:$TOOLS_HOME/lib/i18n-core-1.4.2.jar:$TOOLS_HOME/lib/openam-ldap-utils-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/openam-core-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/openam-shared-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/forgerock-guice-core-1.1.0.jar:$TOOLS_HOME/lib/guice-3.0-no_aop.jar:$TOOLS_HOME/lib/openam-audit-configuration-14.0.0-SNAPSHOT.jar:$TOOLS_HOME/lib/forgerock-audit-core-5.0.0.jar:$TOOLS_HOME/lib/opendj-server-3.0.0.jar:$TOOLS_HOME/lib/slf4j-nop-1.7.5.jar:$TOOLS_HOME/lib/xmlsec-1.5.6.jar:$TOOLS_HOME/lib/guice-assistedinject-3.0.jar:$TOOLS_HOME/lib/openam-audit-context-14.0.0-SNAPSHOT.jar"
CLASSPATH="$CLASSPATH:$TOOLS_HOME/resources"

$JAVA_HOME/bin/java -D"load.config=yes" -D"help.print=$help_print" \
                      -D"path.AMConfig=$path_AMConfig" -D"path.debug=$path_debug" \
                      -D"path.log=$path_log" \
                      -cp "$CLASSPATH" \
                      com.sun.identity.tools.bundles.Main $accept_license
