#!/bin/bash
#
# Copyright (C) 2013-2016, Intel Corporation. All rights reserved.
#
# The source code, information and material ("Material") contained herein is owned by Intel Corporation
# or its suppliers or licensors, and title to such Material remains with Intel Corporation or its suppliers
# or licensors. The Material contains proprietary information of Intel or its suppliers and licensors.
# The Material is protected by worldwide copyright laws and treaty provisions. No part of the Material may be
# used, copied, reproduced, modified, published, uploaded, posted, transmitted, distributed or disclosed in
# any way without Intel's prior express written permission. No license under any patent, copyright or other
# intellectual property rights in the Material is granted to or conferred upon you, either expressly, by implication,
# inducement, estoppel or otherwise. Any license under such intellectual property rights must be express and approved
# by Intel in writing.
#
# Unless otherwise agreed by Intel in writing, you may not remove or alter this notice or any other notice embedded
# in Materials by Intel or Intel’s suppliers or licensors in any way.
#

SCRIPT_DIR=$(cd $(dirname $0); pwd)

trap "" SIGTSTP # disable Ctrl-Z

ARGS="$@"

# if ISM was called with arguments (means from some other program), it should run silently
silent_mode=0
if [ ${#ARGS} -ne 0 ]; then
    silent_mode=1
fi

# check if X is running
xset -q 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
    [ $silent_mode -eq 0 ] && echo "ERROR: Unable to run Intel(R) Software Manager. Please make sure that X server is up and running."
    exit 1
fi

ARCH=$(uname -m)
if [ "ia64" = "$ARCH" ]; then
    [ $silent_mode -eq 0 ] && echo "The platform is not supported."
    return 1;
elif [ "x86_64" = "$ARCH" ]; then
    march="intel64"
else
    march="ia32"
fi

PLUGINS_PATH=$SCRIPT_DIR/plugins/$march/dataprovider.irc
QT_LIBS_PATH=$SCRIPT_DIR/qt/$march/lib
QT_PLUGIN_PATH=""
SHARED_PATH=$SCRIPT_DIR/bin/$march

LD_LIBRARY_PATH=$PLUGINS_PATH:$QT_LIBS_PATH:$SHARED_PATH:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

# command-line interface is temporary disabled for Beta
[ $silent_mode -eq 1 ] && exit 0

err_log_file="/tmp/intel.ism.$(whoami).error.log"

( /bin/bash -c "$SCRIPT_DIR/bin/$march/ismcore $ARGS 2>${err_log_file} 1>&2" ) 2>${err_log_file} 1>&2
if [ $? -ne 0 ]; then
    echo "Intel(R) Software Manager startup error. Please see ${err_log_file} for details."
fi
