#!/bin/bash -f

# ------------------------------------------------------------------------------------
# This script copies all required files into the Output directory
# (Third party libraries, etc)
# ------------------------------------------------------------------------------------
# For Linux, It assumes that the release builds of the 3rd party libraries are used

# Make sure we use the right tools (no aliases etc')
ECHO=`which echo`
LS=`which ls`
CP=`which cp`
__CP_OPTIONS=' -f -P '
FIND=`which find`
GREP=`which grep`
MKDIR=`which mkdir`

NARGS=$#
# Use a global status to return the overall success or failure of this
# critical setup script
GLOBAL_STATUS=0

# Get the OS type:
osType=$(uname -o 2>&1)

if [ "$osType" == "darwin" ]  ; then
	$ECHO "Mac OS X -> $osType"
elif [ "$osType" == "GNU/Linux" ]  ; then
	$ECHO "Generic Linux -> $osType"
else
	$ECHO "Unknown OS TYPE"
	exit 1
fi

# Get the OS bit size:
machineType=$(uname -m 2>&1)

# Override host OS size with optional argument
# it will be x86 or x86_64
if [ $NARGS -eq 1 ]
then
    machineType=$1
fi

# Channel all shell commands throught this function to get some proper feedback
function echo_system()
{
    CMD=$@
    ret=$($CMD 2>&1)
    if [ "$?" != "0" ]; then
	$ECHO "---- FAILED ----"
	$ECHO "                $CMD"
	$ECHO "                $ret"
    GLOBAL_STATUS=1
    fi
}

# use this function to overcome symbolic links bugs in CP using BASH
function copy_all_files()
{
    DST=$1
    SRC=$2
    shift
    shift
    GREP_EXPR=$@
    if [ -n "$GREP_EXPR" ]; then
	list=`$LS  $SRC | $GREP $GREP_EXPR`
    else
	list=`$LS  $SRC`
    fi
    for file in $list
    do
	if [ -f $SRC/$file ] || [ -h $SRC/$file ]; then
	    echo_system $CP $__CP_OPTIONS $SRC/$file $DST
	else
		if [ -d $SRC/$file ]; then
			echo_system $MKDIR -p $DST/$file
			echo_system $CP $__CP_OPTIONS -r $SRC/$file $DST/
		fi
	fi
    done
}

# Display a header message:
$ECHO 
$ECHO 
$ECHO "Copying files into driveo directories"
$ECHO "====================================="

# Create directories:
$ECHO
$ECHO "Creating directories:"
$ECHO "---------------------------"
echo_system $MKDIR -p $AMD_OUTPUT/release/
echo_system $MKDIR -p $AMD_OUTPUT/release/bin

echo_system $MKDIR -p $AMD_OUTPUT/debug/
echo_system $MKDIR -p $AMD_OUTPUT/debug/bin/

$ECHO
$ECHO "Copying wxWidgets libraries:"
$ECHO "---------------------------"

    # For now, this is only CentOS6.2.  Only copy libraries
    copy_all_files $AMD_OUTPUT/debug/bin/   $AMD_EXTLIBS/wxWidgets/2.9.2/Build/CentOS6.2/${machineType}/debug/lib/  "lib*"
    copy_all_files $AMD_OUTPUT/release/bin/ $AMD_EXTLIBS/wxWidgets/2.9.2/Build/CentOS6.2/${machineType}/release/lib/    "lib*"

$ECHO
$ECHO "Copying AMDTQTControls libraries:"
$ECHO "----------------------"
if [ "$machineType" == "x86_64" ]; then
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/AMDTQtControls/1.0/Lib/x86_64/libAMDTQtControls-d.so  $AMD_OUTPUT/debug/bin/
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/AMDTQtControls/1.0/Lib/x86_64/libAMDTQtControls.so    $AMD_OUTPUT/release/bin/
else
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/AMDTQtControls/1.0/Lib/x86/libAMDTQtControls-d.so  $AMD_OUTPUT/debug/bin/
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/AMDTQtControls/1.0/Lib/x86/libAMDTQtControls.so    $AMD_OUTPUT/release/bin/
fi


$ECHO
$ECHO "Copying QT libraries:"
$ECHO "----------------------"
export QT_VER=4.7.4
export QT_LOCATION=${AMD_EXTLIBS}/Qt/${QT_VER}/Build/CentOS6.2/${machineType}
    copy_all_files $AMD_OUTPUT/debug/bin/   ${QT_LOCATION}/debug/lib/		libQtGuiAmdDt474d.
    copy_all_files $AMD_OUTPUT/debug/bin/   ${QT_LOCATION}/debug/lib/		libQtCoreAmdDt474d.
    copy_all_files $AMD_OUTPUT/debug/bin/   ${QT_LOCATION}/debug/lib/		libQtOpenGLAmdDt474d.
    copy_all_files $AMD_OUTPUT/debug/bin/   ${QT_LOCATION}/debug/lib/		libQtNetworkAmdDt474d.
    copy_all_files $AMD_OUTPUT/debug/bin/   ${QT_LOCATION}/debug/lib/		libQtXmlAmdDt474d.
    copy_all_files $AMD_OUTPUT/debug/bin/   ${QT_LOCATION}/debug/lib/		libQtWebKitAmdDt474d.

    copy_all_files $AMD_OUTPUT/release/bin/   ${QT_LOCATION}/release/lib/	libQtGuiAmdDt474.
    copy_all_files $AMD_OUTPUT/release/bin/   ${QT_LOCATION}/release/lib/	libQtCoreAmdDt474.
    copy_all_files $AMD_OUTPUT/release/bin/   ${QT_LOCATION}/release/lib/	libQtOpenGLAmdDt474.
    copy_all_files $AMD_OUTPUT/release/bin/   ${QT_LOCATION}/release/lib/	libQtNetworkAmdDt474.
    copy_all_files $AMD_OUTPUT/release/bin/   ${QT_LOCATION}/release/lib/	libQtXmlAmdDt474.
    copy_all_files $AMD_OUTPUT/release/bin/   ${QT_LOCATION}/release/lib/	libQtWebKitAmdDt474.

$ECHO 
$ECHO "Copying Qt Config files: [WARNING - The Sconstruct file should be fixed]"
$ECHO "Modify it to assume that qconfig.h comes from ${QT_LOCATION}/release/include"
$ECHO "------------------------"
    echo_system $CP $__CP_OPTIONS -fv ${QT_LOCATION}/release/include/QtCore/qconfig.h $AMD_EXTLIBS/Qt/4.7.4/src/corelib/global/
    # This is probably wrong as well
    echo_system $CP $__CP_OPTIONS -fv $AMD_EXTLIBS/Qt/4.7.4/src/corelib/global/linux/x86_64/CentOS6/qconfig.cpp $AMD_EXTLIBS/Qt/4.7.4/src/corelib/global/

$ECHO
$ECHO "Copying QScintilla libraries:"
$ECHO "-----------------------------"
export QSCI_LOCATION=${AMD_EXTLIBS}/QScintilla/2.5.1/Build/CentOS6.2/${machineType}
    copy_all_files $AMD_OUTPUT/debug/bin/   ${QSCI_LOCATION}/debug/lib 	    libqscintillaAmdDt251d.
    copy_all_files $AMD_OUTPUT/release/bin/ ${QSCI_LOCATION}/release/lib    libqscintillaAmdDt251.

$ECHO
$ECHO "Copying FreeImage libraries:"
$ECHO "---------------------------"

export FREEIM_LOC=${AMD_EXTLIBS}/FreeImage/3.9.3/Build/CentOS6.2/${machineType}
echo_system $CP $__CP_OPTIONS ${FREEIM_LOC}/release/lib/libfreeimage.a $AMD_OUTPUT/release/bin/

# There really is only a release build of FreeImage, so copy that into the debug tree
echo_system $CP $__CP_OPTIONS ${FREEIM_LOC}/release/lib/libfreeimage.a $AMD_OUTPUT/debug/bin/

$ECHO "Changing the loading search-path of the wxWidgets and FreeImage libraries to be local:"
$ECHO "-------------------------------------------------------------------------------------"

$ECHO
$ECHO "Copying Updater libraries:"
$ECHO "--------------------------"
if [ "$machineType" == "x86_64" ]; then
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/QtAMDUpdater/Lib/$machineType/libAMDUpdater.a $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/QtAMDUpdater/Lib/$machineType/libAMDUpdater-d.a $AMD_OUTPUT/debug/bin/
else
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/QtAMDUpdater/Lib/$machineType/libAMDUpdater.a $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/QtAMDUpdater/Lib/$machineType/libAMDUpdater-d.a $AMD_OUTPUT/debug/bin/
fi

$ECHO 
$ECHO "Copying tinyXML library:"
$ECHO "------------------------"
    echo_system $CP $__CP_OPTIONS -fv $AMD_EXTLIBS/tinyxml/2.5.2/Build/CentOS6.2/release/${machineType}/lib/libtinyXML.a $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_EXTLIBS/tinyxml/2.5.2/Build/CentOS6.2/debug/${machineType}/lib/libtinyXML_d.a $AMD_OUTPUT/debug/bin/


$ECHO 
$ECHO "Copying promoted libraries"
$ECHO "--------------------------"
if [ "$machineType" == "x86_64" ]; then
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAPIClasses/1.0/Lib/x64/libAMDTAPIClasses.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAPIClasses/1.0/Lib/x64/libAMDTAPIClasses_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplication/1.0/Lib/x64/CodeXL-bin $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplication/1.0/Lib/x64/CodeXL-bin_d $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationComponents/1.0/Lib/x64/libAMDTApplicationComponents.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationComponents/1.0/Lib/x64/libAMDTApplicationComponents_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationFramework/2.0/Lib/x64/libAMDTApplicationFramework.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationFramework/2.0/Lib/x64/libAMDTApplicationFramework_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAssertionHandlers/1.0/Lib/x64/libAMDTAssertionHandlers.a $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAssertionHandlers/1.0/Lib/x64/libAMDTAssertionHandlers_d.a $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTBaseTools/1.0/Lib/x64/libAMDTBaseTools.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTBaseTools/1.0/Lib/x64/libAMDTBaseTools_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTOSWrappers/1.0/Lib/x64/libAMDTOSWrappers.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTOSWrappers/1.0/Lib/x64/libAMDTOSWrappers_d.so $AMD_OUTPUT/debug/bin/
else
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAPIClasses/1.0/Lib/x86/libAMDTAPIClasses.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAPIClasses/1.0/Lib/x86/libAMDTAPIClasses_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplication/1.0/Lib/x86/CodeXL-bin $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplication/1.0/Lib/x86/CodeXL-bin_d $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationComponents/1.0/Lib/x86/libAMDTApplicationComponents.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationComponents/1.0/Lib/x86/libAMDTApplicationComponents_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationFramework/2.0/Lib/x86/libAMDTApplicationFramework.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTApplicationFramework/2.0/Lib/x86/libAMDTApplicationFramework_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAssertionHandlers/1.0/Lib/x86/libAMDTAssertionHandlers.a $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTAssertionHandlers/1.0/Lib/x86/libAMDTAssertionHandlers_d.a $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTBaseTools/1.0/Lib/x86/libAMDTBaseTools.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTBaseTools/1.0/Lib/x86/libAMDTBaseTools_d.so $AMD_OUTPUT/debug/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTOSWrappers/1.0/Lib/x86/libAMDTOSWrappers.so $AMD_OUTPUT/release/bin/
    echo_system $CP $__CP_OPTIONS -fv $AMD_AMDLIBS/AMDTOSWrappers/1.0/Lib/x86/libAMDTOSWrappers_d.so $AMD_OUTPUT/debug/bin/
fi

$ECHO 
$ECHO "Copying AMDOpenCLDebug libraries:"
$ECHO "--------------------------------"
if [ "$machineType" == "x86_64" ]; then
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/../AMD/OpenCLDebugAPI/1.2/Lib/x64/libAMDOpenCLDebugAPI64.so  $AMD_OUTPUT/debug/bin/
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/../AMD/OpenCLDebugAPI/1.2/Lib/x64/libAMDOpenCLDebugAPI64.so    $AMD_OUTPUT/release/bin/
else
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/../AMD/OpenCLDebugAPI/1.2/Lib/x86/libAMDOpenCLDebugAPI32.so  $AMD_OUTPUT/debug/bin/
	echo_system $CP $__CP_OPTIONS $AMD_AMDLIBS/../AMD/OpenCLDebugAPI/1.2/Lib/x86/libAMDOpenCLDebugAPI32.so    $AMD_OUTPUT/release/bin/
fi

# if we failed anywhere, return a global failure - this is a critical
# pre-requisite step
exit ${GLOBAL_STATUS}
