post_install() {
  local _mingw_variant_c
  local _mingw_variant_cxx
  local _mingw_variant_arch
  local _mingw_variant_bit

  if [ -f "ucrt64/bin/sdktool.exe" ]; then

    cd ucrt64/bin

    if [ -f "/usr/bin/gcc.exe" ]; then
      # Add the MSYS2 toolchain.
      ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_x86_64" > /dev/null 2>&1 || true
      ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_x86_64_C" > /dev/null 2>&1 || true
      ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_x86_64_CXX" > /dev/null 2>&1 || true

      ./sdktool.exe addTC \
        "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_x86_64_C" \
        "--language" "1" \
        "--name" "MSYS2 x86_64 GCC (MSYS2)" \
        "--path" "/usr/bin/gcc.exe" \
        "--abi" "x86-windows-msys-pe-64bit" \
        "--supportedAbis" "x86-windows-msys-pe-64bit"

      ./sdktool.exe addTC \
        "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_x86_64_CXX" \
        "--language" "2" \
        "--name" "MSYS2 x86_64 G++ (MSYS2)" \
        "--path" "/usr/bin/g++.exe" \
        "--abi" "x86-windows-msys-pe-64bit" \
        "--supportedAbis" "x86-windows-msys-pe-64bit"
    fi

    # Add MinGW-w64 toolchains. Don't check for existence since
    # the user may add toolchains after Qt Creator.
    for _mingw_variant in "MINGW64" "CLANG64" "UCRT64"; do

      if [ "${_mingw_variant}" = "MINGW64" -o "${_mingw_variant}" = "UCRT64" ]; then
        _mingw_variant_c="gcc"
        _mingw_variant_cxx="g++"
        _mingw_variant_arch="x86"
        _mingw_variant_bit="64bit"
      elif [ "${_mingw_variant}" = "CLANG64" ]; then
        _mingw_variant_c="clang"
        _mingw_variant_cxx="clang++"
        _mingw_variant_arch="x86"
        _mingw_variant_bit="64bit"
      fi

      ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}" > /dev/null 2>&1 || true

      ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_C" > /dev/null 2>&1 || true
      ./sdktool.exe addTC \
        "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_C" \
        "--language" "1" \
        "--name" "MinGW-w64 ${_mingw_variant} C (MSYS2)" \
        "--path" "/${_mingw_variant,,}/bin/${_mingw_variant_c}.exe" \
        "--abi" "${_mingw_variant_arch}-windows-msys-pe-${_mingw_variant_bit}" \
        "--supportedAbis" "${_mingw_variant_arch}-windows-msys-pe-${_mingw_variant_bit}"

      ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_CXX" > /dev/null 2>&1 || true
      ./sdktool.exe addTC \
        "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_CXX" \
        "--language" "2" \
        "--name" "MinGW-w64 ${_mingw_variant} C++ (MSYS2)" \
        "--path" "/${_mingw_variant,,}/bin/${_mingw_variant_cxx}.exe" \
        "--abi" "${_mingw_variant_arch}-windows-msys-pe-${_mingw_variant_bit}" \
        "--supportedAbis" "${_mingw_variant}-windows-msys-pe-${_mingw_variant_bit}"

      ./sdktool.exe rmDebugger "--id" "MinGW.w64.GDB_${_mingw_variant}" > /dev/null 2>&1 || true
      ./sdktool.exe addDebugger \
        "--id" "MinGW.w64.GDB_${_mingw_variant}" \
        "--name" "MinGW-w64 GDB (${_mingw_variant})" \
        "--engine" "1" \
        "--binary" "/${_mingw_variant,,}/bin/gdb.exe" \
        "--abis" "${_mingw_variant_arch}-windows-msys-pe-${_mingw_variant_bit}"

      ./sdktool.exe rmCMake "--id" "MinGW.w64.CMake_${_mingw_variant}" > /dev/null 2>&1 || true
      ./sdktool.exe addCMake \
        "--id" "MinGW.w64.CMake_${_mingw_variant}" \
        "--name" "MinGW-w64 CMake (${_mingw_variant})" \
        "--path" "/${_mingw_variant,,}/bin/cmake.exe"

      # Add Qt5 shared version
      ./sdktool.exe rmQt "--id" "qt5.shared.M2_MinGW-w64_${_mingw_variant}=" > /dev/null 2>&1 || true
      ./sdktool.exe addQt \
        "--id" "qt5.shared.M2_MinGW-w64_${_mingw_variant}" \
        "--name" "Qt %{Qt:Version} (shared) MinGW-w64 ${_mingw_variant} (MSYS2)" \
        "--type" "Qt4ProjectManager.QtVersion.Desktop" \
        "--qmake" "/${_mingw_variant,,}/bin/qmake.exe"

      ./sdktool.exe rmKit "--id" "qt5.shared.M2_MinGW-w64_${_mingw_variant}_kit" > /dev/null 2>&1 || true
      ./sdktool.exe addKit \
        "--id" "qt5.shared.M2_MinGW-w64_${_mingw_variant}_kit" \
        "--name" "Desktop Qt %{Qt:Version} (shared) MinGW-w64 ${_mingw_variant} (MSYS2)" \
        "--Ctoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_C" \
        "--Cxxtoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_CXX" \
        "--qt" "qt5.shared.M2_MinGW-w64_${_mingw_variant}" \
        "--debuggerid" "MinGW.w64.GDB_${_mingw_variant}" \
        "--cmake" "MinGW.w64.CMake_${_mingw_variant}" \
        "--devicetype" "Desktop"

      # Add Qt5 static version
      ./sdktool.exe rmQt "--id" "qt5.static.M2_MinGW-w64_${_mingw_variant}=" > /dev/null 2>&1 || true
      ./sdktool.exe addQt \
        "--id" "qt5.static.M2_MinGW-w64_${_mingw_variant}" \
        "--name" "Qt %{Qt:Version} (static) MinGW-w64 ${_mingw_variant} (static) (MSYS2)" \
        "--type" "Qt4ProjectManager.QtVersion.Desktop" \
        "--qmake" "/${_mingw_variant,,}/qt5-static/bin/qmake.exe"

      ./sdktool.exe rmKit "--id" "qt5.static.M2_MinGW-w64_${_mingw_variant}_kit" > /dev/null 2>&1 || true
      ./sdktool.exe addKit \
        "--id" "qt5.static.M2_MinGW-w64_${_mingw_variant}_kit" \
        "--name" "Desktop Qt %{Qt:Version} (static) MinGW-w64 ${_mingw_variant} (MSYS2)" \
        "--Ctoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_C" \
        "--Cxxtoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_CXX" \
        "--qt" "qt5.static.M2_MinGW-w64_${_mingw_variant}" \
        "--debuggerid" "MinGW.w64.GDB_${_mingw_variant}" \
        "--cmake" "MinGW.w64.CMake_${_mingw_variant}" \
        "--devicetype" "Desktop"

      # Add Qt6 shared version
      ./sdktool.exe rmQt "--id" "qt6.shared.M2_MinGW-w64_${_mingw_variant}=" > /dev/null 2>&1 || true
      ./sdktool.exe addQt \
        "--id" "qt6.shared.M2_MinGW-w64_${_mingw_variant}" \
        "--name" "Qt %{Qt:Version} (shared) MinGW-w64 ${_mingw_variant} (MSYS2)" \
        "--type" "Qt4ProjectManager.QtVersion.Desktop" \
        "--qmake" "/${_mingw_variant,,}/bin/qmake6.exe"

      ./sdktool.exe rmKit "--id" "qt6.shared.M2_MinGW-w64_${_mingw_variant}_kit" > /dev/null 2>&1 || true
      ./sdktool.exe addKit \
        "--id" "qt6.shared.M2_MinGW-w64_${_mingw_variant}_kit" \
        "--name" "Desktop Qt %{Qt:Version} (shared) MinGW-w64 ${_mingw_variant} (MSYS2)" \
        "--Ctoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_C" \
        "--Cxxtoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_CXX" \
        "--qt" "qt6.shared.M2_MinGW-w64_${_mingw_variant}" \
        "--debuggerid" "MinGW.w64.GDB_${_mingw_variant}" \
        "--cmake" "MinGW.w64.CMake_${_mingw_variant}" \
        "--devicetype" "Desktop"

      # Add Qt6 static version
      ./sdktool.exe rmQt "--id" "qt6.static.M2_MinGW-w64_${_mingw_variant}=" > /dev/null 2>&1 || true
      ./sdktool.exe addQt \
        "--id" "qt6.static.M2_MinGW-w64_${_mingw_variant}" \
        "--name" "Qt %{Qt:Version} (static) MinGW-w64 ${_mingw_variant} (static) (MSYS2)" \
        "--type" "Qt4ProjectManager.QtVersion.Desktop" \
        "--qmake" "/${_mingw_variant,,}/qt6-static/bin/qmake.exe"

      ./sdktool.exe rmKit "--id" "qt6.static.M2_MinGW-w64_${_mingw_variant}_kit" > /dev/null 2>&1 || true
      ./sdktool.exe addKit \
        "--id" "qt6.static.M2_MinGW-w64_${_mingw_variant}_kit" \
        "--name" "Desktop Qt %{Qt:Version} (static) MinGW-w64 ${_mingw_variant} (MSYS2)" \
        "--Ctoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_C" \
        "--Cxxtoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_${_mingw_variant}_CXX" \
        "--qt" "qt6.static.M2_MinGW-w64_${_mingw_variant}" \
        "--debuggerid" "MinGW.w64.GDB_${_mingw_variant}" \
        "--cmake" "MinGW.w64.CMake_${_mingw_variant}" \
        "--devicetype" "Desktop"

    done

    cd -
  fi
}

post_upgrade() {
  post_install
}
