import configparser
import requests
import hashlib

urls = []

for plugin_and_version in '''\
    vscode/bat/latest
    vscode/clojure/latest
    vscode/coffeescript/latest
    vscode/configuration-editing/latest
    vscode/cpp/latest
    vscode/csharp/latest
    vscode/css/latest
    vscode/css-language-features/latest
    vscode/debug-auto-launch/latest
    vscode/docker/latest
    vscode/emmet/latest
    vscode/fsharp/latest
    vscode/go/latest
    vscode/groovy/latest
    vscode/grunt/latest
    vscode/gulp/latest
    vscode/handlebars/latest
    vscode/hlsl/latest
    vscode/html/latest
# latest fails to activate:
# Activating extension 'HTML Language Features (built-in)' failed: The language client requires VS Code version ^1.52.0 but received version 1.50.0
    vscode/html-language-features/1.52.1
    vscode/ini/latest
    vscode/jake/latest
    vscode/java/latest
    vscode/javascript/latest
    ms-vscode/js-debug/latest
    vscode/json/latest
# latest json-language-features does offer completions with .theia/settings.json
    vscode/json-language-features/1.45.1
    vscode/less/latest
    vscode/log/latest
    vscode/lua/latest
    vscode/make/latest
    vscode/markdown/latest
# https://github.com/eclipse-theia/theia/issues/7780
    vscode/markdown-language-features/1.39.2
    vscode/merge-conflict/latest
    vscode/npm/latest
    ms-vscode/node-debug/latest
    ms-vscode/node-debug2/latest
    vscode/objective-c/latest
    vscode/perl/latest
    vscode/powershell/latest
    vscode/pug/latest
    vscode/python/latest
    vscode/r/latest
    vscode/razor/latest
    vscode/ruby/latest
    vscode/rust/latest
    vscode/scss/latest
    vscode/shaderlab/latest
    vscode/shellscript/latest
    vscode/sql/latest
    vscode/swift/latest
    vscode/theme-abyss/latest
    vscode/theme-defaults/latest
    vscode/theme-kimbie-dark/latest
    vscode/theme-monokai/latest
    vscode/theme-monokai-dimmed/latest
    vscode/theme-quietlight/latest
    vscode/theme-red/latest
    vscode/theme-solarized-dark/latest
    vscode/theme-tomorrow-night-blue/latest
    vscode/typescript/latest
    vscode/typescript-language-features/latest
    vscode/vb/latest
    vscode/vscode-theme-seti/latest
    vscode/xml/latest
    vscode/yaml/latest
    EditorConfig/EditorConfig/latest
    dbaeumer/vscode-eslint/latest
    ms-vscode/references-view/latest
# golang.Go removed because it overwrites the PATH in theia shell
# golang/Go/0.16.2
    vscjava/vscode-java-debug/0.29.0
    redhat/java/0.61.0
    vscjava/vscode-java-test/0.26.0
    ms-python/python/2020.9.112786
    perrinjerome/vscode-zc-buildout/latest
    jebbs/plantuml/2.14.0
    rafaelmaiolla/diff/latest
    perrinjerome/git-commit-syntax/latest
    perrinjerome/git-rebase-syntax/latest
'''.splitlines():
  plugin_and_version = plugin_and_version.strip()
  if not plugin_and_version or plugin_and_version.startswith('#'):
    continue
  publisher, extension_name, version = plugin_and_version.split('/')

  api_url = f'https://open-vsx.org/api/{publisher}/{extension_name}/{version}'
  download_url = requests.get(api_url).json()['files']['download']
  md5sum = hashlib.md5(requests.get(download_url).content).hexdigest()
  urls.append(f'{publisher}-{extension_name} {download_url} {md5sum}')

cfg = configparser.ConfigParser()
cfg.add_section('theia-download-plugins')
cfg.set('theia-download-plugins', 'urls', '\n'.join(urls))

with open('download-plugins.cfg', 'w') as f:
  f.write(f"""\
# This file is automatically generated from {__file__}
# Do not edit directly.
""")
  cfg.write(f)
