task rebuild_bridge: :environment do

  path = File.join(Dir.home, 'workspace', 'JamKazamBuild', 'client')

  puts "client directory: #{path}"

  interface = File.join(path, 'src', 'mumble', 'JavaScriptBridgeUntyped.h')

  puts "interface file: #{interface}"

  output = File.join('app', 'assets', 'javascripts', 'bridge_api.es6')

  puts "output file: #{output}"

  # every line is something like this:
  # Q_INVOKABLE QVariantMap FTUEGetAvailableEncodeVideoResolutions(){ return bridge->FTUEGetAvailableEncodeVideoResolutions(); }

  methods = []
  File.open(interface).each_line do |li|
    if li['Q_INVOKABLE']
      match = /^.*Q_INVOKABLE\s+\S+\s+([^\(]+)\(.*$/.match(li)
      method = match.captures[0]
      bits = method.split()
      methods << bits[bits.length - 1]
    end
  end


  header = <<-FOO
class BridgeApi {
  constructor(options) {
     this.options = options
     this.bridge = bridge
  }

  FOO



  File.open(output, 'w') { |file|
    file.write(header)

    methods.each do |method|

      method_print = <<-FOO
  #{method}() {
    return this.bridge.invokeMethod('#{method}()', arguments)
  }

      FOO

      file.write(method_print)
    end


    file.puts('}')
  }


end