#!/usr/bin/env python

import os
import subprocess
import sys


# If BDE_TOOLS_DIR is not specified, try finding it via 'which' and default
# to '/bb/bde/bbshr/bde-tools'
bdeToolsDir = os.getenv("BDE_TOOLS_DIR")

if not bdeToolsDir:
    try:
        whichBuildEnv = subprocess.run(
            ["which", "bde_build_env.py"], stdout=subprocess.PIPE, text=True
        ).stdout
        bdeToolsDir = os.path.dirname(os.path.dirname(whichBuildEnv))
    except:
        bdeToolsDir = "/bb/bde/bbshr/bde-tools"

bdeCmakeBuildDir = os.getenv("BDE_CMAKE_BUILD_DIR")
bdeCmakeToolchain = os.getenv("BDE_CMAKE_TOOLCHAIN")
bdeCmakeUfid = os.getenv("BDE_CMAKE_UFID")

if not bdeCmakeBuildDir or not bdeCmakeToolchain or not bdeCmakeUfid:
    print("Please set the BDE build environment using 'bde_build_env.py'.")
    sys.exit(1)

print(f"Generating .vscode folder...")
print(f"  BDE tools directory: {bdeToolsDir}")
print(f"  Build directory:     {bdeCmakeBuildDir}")
print(f"  Toolchain:           {bdeCmakeToolchain}")
print(f"  UFID:                {bdeCmakeUfid}")

os.makedirs(".vscode", exist_ok=True)

with open(".vscode/settings.json", "wt") as settings:
    settings.write(
        f"""
{{
    "cmake.configureOnOpen": false,
    "cmake.buildDirectory": "${{workspaceFolder}}/{bdeCmakeBuildDir}",
    "cmake.generator": "Ninja",
    "cmake.parallelJobs": 0,
    "cmake.configureSettings": {{
        "CMAKE_MODULE_PATH": "{bdeToolsDir}/cmake",
        "CMAKE_EXPORT_COMPILE_COMMANDS": true,
        "CMAKE_TOOLCHAIN_FILE": "{bdeToolsDir}/cmake/{bdeCmakeToolchain}.cmake",
        "BUILD_BITNESS": "64",
        "UFID": "{bdeCmakeUfid}"
    }},
    "cmake.ctestArgs": [],
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
    "files.associations": {{
        "*.ipp": "cpp"
    }},
    "terminal.integrated.defaultProfile.linux": "bash"
}}
"""
    )

with open(".vscode/c_cpp_properties.json", "wt") as settings:
    settings.write(
        f"""
{{
    "configurations": [
        {{
            "name": "CMake",
            "compileCommands": "${{workspaceFolder}}/{bdeCmakeBuildDir}/compile_commands.json",
            "configurationProvider": "ms-vscode.cmake-tools"
        }}
    ],
    "version": 4
}}
"""
    )

with open(".vscode/launch.json", "wt") as settings:
    settings.write(
        f"""
{{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {{
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${{command:cmake.launchTargetPath}}",
            "args": ["${{input:args}}"],
            "stopAtEntry": true,
            "cwd": "${{command:cmake.getLaunchTargetDirectory}}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {{
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }}
            ]
        }},
    ],
    "inputs": [
        {{
            "id": "args",
            "type":"promptString",
            "description": "Program Args",
            "default": "0"
        }}
    ]
}}
"""
    )


# -----------------------------------------------------------------------------
# Copyright 2022 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------- END-OF-FILE -----------------------------------

@@ -3232,24 +3232,998 @@
         %7D%7D,%0A
+        %7B%7B%0A            %22name%22: %22(UDB) Launch%22,%0A            %22type%22: %22cppdbg%22,%0A            %22request%22: %22launch%22,%0A            %22program%22: %22$%7B%7Bcommand:cmake.launchTargetPath%7D%7D%22,%0A            %22args%22: %5B%22$%7B%7Binput:args%7D%7D%22%5D,%0A            %22stopAtEntry%22: true,%0A            %22cwd%22: %22$%7B%7Bcommand:cmake.getLaunchTargetDirectory%7D%7D%22,%0A            %22environment%22: %5B%5D,%0A            %22externalConsole%22: false,%0A            %22MIMode%22: %22gdb%22,%0A            %22setupCommands%22: %5B%0A                %7B%7B%0A                    %22description%22: %22Enable pretty-printing for gdb%22,%0A                    %22text%22: %22-enable-pretty-printing%22,%0A                    %22ignoreFailures%22: true%0A                %7D%7D%0A            %5D,%0A            %22miDebuggerPath%22: %22udb%22,%0A            %22miDebuggerArgs%22: %22--max-event-log-size 4G%22,%0A            %22logging%22: %7B%7B%0A                %22trace%22: false,%0A                %22traceResponse%22: false,%0A                %22engineLogging%22: false%0A            %7D%7D,%0A            %22udb%22: %22live%22,%0A            %22timezone%22: %22%22%0A        %7D%7D,%0A
     %5D,%0A    %22

Add UDB launch config (#239)