# /* --------------------------------------------------------------------------------------------
# * Copyright (c) Microsoft Corporation. All Rights Reserved.
# * See 'LICENSE' in the project root for license information.
# * ------------------------------------------------------------------------------------------ */
#!/usr/bin/env bash

fail() {
    echo "$1" >&2
    exit 1
}

usage() {
    echo "Usage: $0 [-v] path-to-extension path-to-miengine path-to-opendebug"
    echo "  e.g. $0 ~/.vscode/extensions/ms-vscode.cpptools-0.5.0 ~/src/MIEngine ~/src/OpenDebugAD7"
    echo
    echo "  Parameters can also be provided by environment variables:"
    echo "    CPPDBG_EXTENSION_PATH  (current value: ${CPPDBG_EXTENSION_PATH:-(none)})"
    echo "    MIENGINE_SRC_PATH      (current value: ${MIENGINE_SRC_PATH:-(none)})"
    echo "    OPENDEBUG_SRC_PATH     (current value: ${OPENDEBUG_SRC_PATH:-(none)})"
    exit 0
}

remove_existing_binaries() {
    local BIN_PATH=$1
    local RM_FLAGS="-f"

    if [ "${VERBOSE}" == "1" ] ; then
        RM_FLAGS="${RM_FLAGS} -v"
    fi

    rm ${RM_FLAGS} ${BIN_PATH}/*.dll || fail "Failed to clean up existing DLLs!"
    rm ${RM_FLAGS} ${BIN_PATH}/*.exe || fail "Failed to clean up existing EXEs!"
    rm ${RM_FLAGS} ${BIN_PATH}/*.mdb || fail "Failed to clean up existing MDBs!"
}

link_file() {
    local SRC=$1
    local DEST=$2

    if [ "${VERBOSE}" == "1" ] ; then
        echo "Linking '${SRC}' -> '${DEST}'"
    fi

    ln -s ${SRC} ${DEST} || fail "Failed to link file '${SRC}' to '${DEST}'!"
}

link_files() {
    local DEST_PATH=$1
    local SRC_PATH=$2

    shift 2

    while [ ! -z $1 ] ; do
        link_file ${SRC_PATH}/$1 ${DEST_PATH}/$1

        # Link symbols, if present
        if [ -e ${SRC_PATH}/$1.mdb ] ; then
            link_file ${SRC_PATH}/$1.mdb ${DEST_PATH}/$1.mdb
        fi

        shift
    done
}

VERBOSE=0
if [ "$1" == "-v" ] ; then
    VERBOSE=1
    shift
fi

EXTENSION_PATH=${1:-${CPPDBG_EXTENSION_PATH}}
MIENGINE_PATH=${2:-${MIENGINE_SRC_PATH}}
OPENDEBUG_PATH=${3:-${OPENDEBUG_SRC_PATH}}

if [ "$1" ==  "--help" -o -z "${EXTENSION_PATH}" -o -z "${MIENGINE_PATH}" -o -z "${OPENDEBUG_PATH}" ] ; then
    usage
fi

EXTENSION_BIN=${EXTENSION_PATH}/debugAdapters/bin

remove_existing_binaries ${EXTENSION_BIN}

# Link main binaries
link_files ${EXTENSION_BIN} ${OPENDEBUG_PATH}/out/Desktop.Debug OpenDebugAD7.exe Microsoft.DebugEngineHost.dll
link_files ${EXTENSION_BIN} ${MIENGINE_PATH}/bin/Desktop.Debug Microsoft.MICore.dll Microsoft.MIDebugEngine.dll Microsoft.MICore.XmlSerializers.dll osxlaunchhelper.scpt

# Link dependencies
link_files ${EXTENSION_BIN} ${OPENDEBUG_PATH}/out/Desktop.Debug Newtonsoft.Json.dll
link_files ${EXTENSION_BIN} ${MIENGINE_PATH}/bin/Desktop.Debug Microsoft.VisualStudio.Debugger.Interop.10.0.dll Microsoft.VisualStudio.Debugger.Interop.11.0.dll Microsoft.VisualStudio.Debugger.Interop.12.0.dll Microsoft.VisualStudio.Debugger.InteropA.dll

# Link engine JSON to real assembly location
if [ ! -e ${OPENDEBUG_PATH}/out/Desktop.Debug/cppdbg.ad7Engine.json ] ; then
    link_files ${OPENDEBUG_PATH}/out/Desktop.Debug ${EXTENSION_BIN} cppdbg.ad7Engine.json
fi

echo "Done!"
