#!/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:${unix.setup.classpath}"
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
