post_install() {
  local _msys2_machine
  local _library_variant_suffix
  local _library_variant_idsuffix
  local _library_variant_prefix
  local _mingw_variant_suffix
  local _mingw_variant_idsuffix

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

    cd mingw64/bin

    # Delete old debuggers.xml since it will suffer from the
    # problem fixed by https://codereview.qt-project.org/#/c/94248/
    # This can be removed after a few releases as it will remove
    # any debuggers added by the user.
    [ -f "$APPDATA"/QtProject/qtcreator/debuggers.xml ] && rm -rf "$APPDATA"/QtProject/qtcreator/debuggers.xml > /dev/null 2>&1 || true

    if [ -f "/usr/bin/gcc.exe" ]; then
      # Add the MSYS2 toolchain.
      _msys2_machine=$(/usr/bin/gcc.exe -dumpmachine)
      if [ "${_msys2_machine}" = "x86_64-pc-msys" ]; then
        ./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 64bit 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 64bit G++ (MSYS2)" \
          "--path" "/usr/bin/g++.exe" \
          "--abi" "x86-windows-msys-pe-64bit" \
          "--supportedAbis" "x86-windows-msys-pe-64bit"
      else
        ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_i686" > /dev/null 2>&1 || true
        ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_i686_C" > /dev/null 2>&1 || true
        ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MSYS2_i686_CXX" > /dev/null 2>&1 || true

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

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

    # Add MinGW-w64 toolchains. Don't check for existence since
    # the user may add toolchains after Qt Creator.
    ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_x86_64" > /dev/null 2>&1 || true
    ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_x86_64_C" > /dev/null 2>&1 || true
    ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_x86_64_CXX" > /dev/null 2>&1 || true

    ./sdktool.exe addTC \
      "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_x86_64_C" \
      "--language" "1" \
      "--name" "MinGW-w64 64bit GCC (MSYS2)" \
      "--path" "/mingw64/bin/gcc.exe" \
      "--abi" "x86-windows-msys-pe-64bit" \
      "--supportedAbis" "x86-windows-msys-pe-64bit"

    ./sdktool.exe addTC \
      "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_x86_64_CXX" \
      "--language" "2" \
      "--name" "MinGW-w64 64bit G++ (MSYS2)" \
      "--path" "/mingw64/bin/g++.exe" \
      "--abi" "x86-windows-msys-pe-64bit" \
      "--supportedAbis" "x86-windows-msys-pe-64bit"

    ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_i686" > /dev/null 2>&1 || true
    ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_i686_C" > /dev/null 2>&1 || true
    ./sdktool.exe rmTC "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_i686_CXX" > /dev/null 2>&1 || true

    ./sdktool.exe addTC \
      "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_i686_C" \
      "--language" "1" \
      "--name" "MinGW-w64 32bit GCC (MSYS2)" \
      "--path" "/mingw32/bin/gcc.exe" \
      "--abi" "x86-windows-msys-pe-32bit" \
      "--supportedAbis" "x86-windows-msys-pe-32bit"

    ./sdktool.exe addTC \
      "--id" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64_i686_CXX" \
      "--language" "2" \
      "--name" "MinGW-w64 32bit G++ (MSYS2)" \
      "--path" "/mingw32/bin/g++.exe" \
      "--abi" "x86-windows-msys-pe-32bit" \
      "--supportedAbis" "x86-windows-msys-pe-32bit"

    ./sdktool.exe rmDebugger "--id" "MinGW.w64.GDB_i686" > /dev/null 2>&1 || true
    ./sdktool.exe rmDebugger "--id" "MinGW.w64.GDB_x86_64" > /dev/null 2>&1 || true
    
    ./sdktool.exe addDebugger \
      "--id" "MinGW.w64.GDB_i686" \
      "--name" "MinGW-w64 GDB (32bit)" \
      "--engine" "1" \
      "--binary" "/mingw32/bin/gdb.exe" \
      "--abis" "x86-windows-msys-pe-32bit"

    ./sdktool.exe addDebugger \
      "--id" "MinGW.w64.GDB_x86_64" \
      "--name" "MinGW-w64 GDB (64bit)" \
      "--engine" "1" \
      "--binary" "/mingw64/bin/gdb.exe" \
      "--abis" "x86-windows-msys-pe-64bit"

    # Same with Qt. Add both 32 and 64bit, static and non-static.
    for _library_variant in "shared" "static"; do

      if [ "${_library_variant}" = "shared" ]; then
        _library_variant_suffix=""
        _library_variant_idsuffix=""
        _library_variant_prefix=""
      else
        _library_variant_suffix=" (static)"
        _library_variant_idsuffix="_static"
        _library_variant_prefix="/qt5-static"
      fi

      for _mingw_variant in "mingw32" "mingw64"; do

        if [ "${_mingw_variant}" = "mingw32" ]; then
          _mingw_variant_suffix=" 32bit"
          _mingw_variant_idsuffix="_i686"
        else
          _mingw_variant_suffix=" 64bit"
          _mingw_variant_idsuffix="_x86_64"
        fi

        ./sdktool.exe rmQt "--id" "qt.M2_MinGW-w64${_mingw_variant_idsuffix}${_library_variant_idsuffix}" > /dev/null 2>&1 || true
        ./sdktool.exe addQt \
          "--id" "qt.M2_MinGW-w64${_mingw_variant_idsuffix}${_library_variant_idsuffix}" \
          "--name" "Qt ${_library_variant_suffix} MinGW-w64${_mingw_variant_suffix} (MSYS2)" \
          "--type" "Qt4ProjectManager.QtVersion.Desktop" \
          "--qmake" "/${_mingw_variant}${_library_variant_prefix}/bin/qmake.exe"

        ./sdktool.exe rmKit "--id" "qt.M2_MinGW-w64${_mingw_variant_idsuffix}${_library_variant_idsuffix}_kit" > /dev/null 2>&1 || true
        ./sdktool.exe addKit \
          "--id" "qt.M2_MinGW-w64${_mingw_variant_idsuffix}${_library_variant_idsuffix}_kit" \
          "--name" "Desktop Qt ${_library_variant_suffix} MinGW-w64${_mingw_variant_suffix} (MSYS2)" \
          "--Ctoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64${_mingw_variant_idsuffix}_C" \
          "--Cxxtoolchain" "ProjectExplorer.ToolChain.Mingw:M2_MinGW-w64${_mingw_variant_idsuffix}_CXX" \
          "--qt" "qt.M2_MinGW-w64${_mingw_variant_idsuffix}${_library_variant_idsuffix}" \
          "--debuggerid" "MinGW.w64.GDB${_mingw_variant_idsuffix}" \
          "--devicetype" "Desktop"

      done

    done

    cd -
  fi
}

post_upgrade() {
  post_install
}
