#!/bin/sh
####################################################################
# Licensed Materials - Property of IBM
# Rational ClearCase
# (C) Copyright IBM Corp. 2012, 2016.  All Rights Reserved.
# US Government Users Restricted Rights
# Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
####################################################################

set +v
set -a

# Accept overrides from the values provided at install time
#  Allows any rcleartool to run any rcleartool version based
#  on local environment variable overrides
# Path to java cmd
_JAVACMD=${_JAVACMD:-_JAVA_CMD_PATH_}
# Path to unzipped com.ibm.rational.ccrc.cli plugin project
RCLEARTOOL_HOME=${RCLEARTOOL_HOME:-_CCRC_CLI_PATH_}
# Path to unzipped com.ibm.rational.teamapi plugin project
JARS_DIR=${JARS_DIR:-_TEAM_API_PATH_}
# Path to compare/merge provider
CCRC_CLEARDIFF_PATH=${CCRC_CLEARDIFF_PATH:-_CCRC_CLEARDIFF_PATH_}
# Path to org.apache.xml plugin
ORG_APACHE_XML_PATH=${ORG_APACHE_XML_PATH:-_ORG_APACHE_XML_PATH_}

PATH_TO_RCLEARTOOL=`dirname "$0"`
# If _CCRC_CLI_PATH is still present in RCLEARTOOL_HOME
#  then we have not configured this script yet. We attempt
#  to use the eclipse application extension for this.
RCTEST=`echo "$RCLEARTOOL_HOME" | sed 's/\(.\{14\}\).*/\1/'`
if [ "$RCTEST" = '_CCRC_CLI_PATH' ]; then
    echo 'Configuring rcleartool for first time use'
    # different ways to get the launcher and Main class
    if [ -f $PATH_TO_RCLEARTOOL/startup.jar ]; then
      cpAndMain="$PATH_TO_RCLEARTOOL/startup.jar org.eclipse.core.launcher.Main"; # up to M4_33
    elif [ -f $PATH_TO_RCLEARTOOL/plugins/org.eclipse.equinox.launcher.jar ]; then
      cpAndMain="$PATH_TO_RCLEARTOOL/plugins/org.eclipse.equinox.launcher.jar org.eclipse.equinox.launcher.Main"; # M5_33
    else
      PATH_TO_LAUNCHER=`find $PATH_TO_RCLEARTOOL/ -name "org.eclipse.equinox.launcher_*.jar" | sort | head -1`
      if [ -n "$PATH_TO_LAUNCHER" ]; then
        cpAndMain=`find $PATH_TO_RCLEARTOOL/ -name "org.eclipse.equinox.launcher_*.jar" | sort | head -1`" org.eclipse.equinox.launcher.Main";
      else
        cpAndMain=`find $PATH_TO_RCLEARTOOL/../.. -name "org.eclipse.equinox.launcher_*.jar" | sort | head -1`" org.eclipse.equinox.launcher.Main";
      fi
    fi
    $_JAVACMD -cp $cpAndMain -application com.ibm.rational.ccrc.cli.CliConfigureApplication
    if [ $? -eq 0 ]; then
        echo 'RCleartool now configured for use. Please re-enter command.'
    else 
        echo 'Error when attempting to auto-configure RCleartool. You must configure manually.'
    fi
    exit $?
fi

# Pull in rcleartool.conf options for the java command
INI_PATH=$PATH_TO_RCLEARTOOL/rcleartool.conf
if [ -f $INI_PATH ];
then
  INI_OPTIONS=`cat "$INI_PATH"`
else
  INI_OPTIONS=
fi

# Execute java command with arguments
$_JAVACMD $INI_OPTIONS -Djava.util.logging.config.file=$PATH_TO_RCLEARTOOL/logging.properties -classpath $JARS_DIR/stpwvcm.jar:$JARS_DIR/stpcmmn.jar:$JARS_DIR/stpcc.jar:$JARS_DIR/remote_core.jar:$JARS_DIR/httpclient-4.5.2.jar:$JARS_DIR/httpmime-4.5.2.jar:$JARS_DIR/httpcore-4.4.4.jar:$JARS_DIR/commons-codec-1.10.jar:$RCLEARTOOL_HOME/commons-cli-1.1.jar:$JARS_DIR/icu4j-4_8.jar:$RCLEARTOOL_HOME:$ORG_APACHE_XML_PATH$CUSTOM_CLASSPATH com.ibm.rational.ccrc.cli.RCleartool "$@"
exit $?
