#!/bin/sh
#
# Configures the classpath for Java and starts the TFS Cross Platform Command Line Client.
#

# BASE_DIRECTORY is the directory containing the Java libraries
if [ -z "$TF_CLC_HOME" ] ; then
    BASE_DIRECTORY=`dirname "$0"`
else
    BASE_DIRECTORY="$TF_CLC_HOME"
fi

TOKEN_REQUIRED_JAR="$BASE_DIRECTORY/lib/com.microsoft.tfs.core.jar"
if [ ! -f "$TOKEN_REQUIRED_JAR" ] ; then
    echo "Unable to find a required JAR: $TOKEN_REQUIRED_JAR does not exist"
    exit
fi

# Default location for most Unix platforms.
SETTINGS_DIRECTORY="~/.microsoft/Team Foundation/4.0"

# Detect platform, architecture, and settings directory.
case `uname -s` in
    CYGWIN_NT*)
        PLATFORM=cygwin
        ARCH=`uname -m`
        SETTINGS_DIRECTORY="`cygpath -u $USERPROFILE`/Local Settings/Application Data/Microsoft/Team Foundation/4.0"
        # Need to escape embedded spaces in the directory name
        SETTINGS_DIRECTORY=`echo $SETTINGS_DIRECTORY | sed "s/ /\\ /g"`
        ;;
    AIX)
        PLATFORM=aix
        ARCH=`uname -p`
        ;;
    Linux)
        PLATFORM=linux
        ARCH=`uname -m`
        ;;
    SunOS)
        PLATFORM=solaris
        ARCH=`uname -p`
        ;;
    HP-UX)
        PLATFORM=hpux
        ARCH=`uname -m`
        ;;
    Darwin)
        PLATFORM=macosx
        ARCH=""
        SETTINGS_DIRECTORY="~/Library/Microsoft/Team Foundation/4.0/"
        ;;
    FreeBSD)
        PLATFORM=freebsd
        ARCH=`uname -m`
        ;;
    OS/390)
        PLATFORM=zos
        ARCH=390
        ;;
    *)
        PLATFORM=unknown
        ARCH=unknown
        ;;
esac

# Map uname's architecture into OSGI architecture.
case $ARCH in
    i386|i486|i586|i686)
        ARCH=x86
        
        # Solaris reports i386 for both x86 and AMD64 versions, so we have
        # to do more detection for architecture size.
        if [ "$PLATFORM" = "solaris" ] ; then
            if [ `isainfo -b` = "64" ] ; then
                ARCH="x86_64"
            fi
        fi
        ;;
    x86_64)
        ARCH=x86_64
        ;;
    sparc)
        ARCH=sparc
        ;;
    ppc|ppc64|powerpc)
        ARCH=ppc
        ;;
    9000/*)
        ARCH=PA_RISC
        ;;
    ia64)
        # Eclipse only supports the ia64_32 JVM.
        ARCH=ia64_32
        ;;
    390)
        ;;
    unknown)
        ;;
esac

CLC_CLASSPATH=
export CLC_CLASSPATH

# Add check-in policy implementations in the user's home directory
# first, so they can override standard CLC libraries.
if [ -d "$SETTINGS_DIRECTORY"/policies ] ; then
    for POLICY_JAR in "$SETTINGS_DIRECTORY"/policies/*.jar ; do
        # Test for the file to work around the empty wildcard expansion case.
        if [ -f "$POLICY_JAR" ] ; then
            CLC_CLASSPATH="$CLC_CLASSPATH:$POLICY_JAR"
        fi
    done
fi

# Standard CLC resources.  Site-wide check-in policies can be dropped in 
# the lib directory.
#
# 3/25/2015 Workaround added:
# Make sure that AppInsights SDK is the last in CLASS_PATH, 
# otherwise it will destroy CLC logging.
#
# Can be removed after (and if) the issue is fixed in AppInsights.

AI_JAR=
for LIBRARY_JAR in "$BASE_DIRECTORY"/lib/*.jar ; do
    if expr "$LIBRARY_JAR" : ".*/applicationinsights[^/]*" > /dev/null; then
        AI_JAR="$LIBRARY_JAR" 
    else
        CLC_CLASSPATH="$CLC_CLASSPATH:$LIBRARY_JAR"
    fi
done
CLC_CLASSPATH="$CLC_CLASSPATH:$AI_JAR"

# Convert to Windows-style classpath
if [ "$PLATFORM" = "cygwin" ] ; then
    CLC_CLASSPATH=`cygpath -wp "$CLC_CLASSPATH"`
    BASE_DIRECTORY=`cygpath -w "$BASE_DIRECTORY"`
fi

# Sun's JVM 1.4 on Linux may block for lack of entropy and cause the
# client to hang if /dev/random is used.  /dev/urandom will not block.
RANDOM_DEVICE_PROPERTY=""
if [ `uname` = "Linux" ] ; then
    RANDOM_DEVICE_PROPERTY="-Djava.security.egd=file:/dev/urandom"
fi

exec java -Xmx${JAVA_MEM}M -classpath "$CLC_CLASSPATH" $TF_ADDITIONAL_JAVA_ARGS \
	"-Dcom.microsoft.tfs.jni.native.base-directory=$BASE_DIRECTORY/native" \
    $RANDOM_DEVICE_PROPERTY com.microsoft.tfs.client.clc.vc.Main "$@"
