#!/bin/env ruby

require 'ffi'

class ExportScanner
  NEEDLE = "PUBLIC"
  NVCCFLAGS = []

  attr_reader :structs
  attr_reader :functions

  def initialize(typedefs = Hash.new)
    @functions = Array.new
    @structs = Array.new
    @typedefs = typedefs
  end

  FunctionPrototype = Struct.new(:name, :return_type, :args, :definition)
  
  class Argument
    attr_accessor :const
    attr_accessor :type
    attr_accessor :name
    attr_accessor :pointer
    
    def initialize(str)
      m = str.match(/\s*(((\w+) )+\s*(([*]+)?(\w+)?)\s*)/m)
      if m
        self.name = m[-1]
        self.const = m[0] =~ /const/
        self.pointer = m[-2]
        self.type = m[3]
      end
    end
  end

  def parse_args(arg_str)
    args = arg_str.split(',').collect(&:strip)
    arg_types = args.collect do |a|
      Argument.new(a)
    end
  end

  def scan_function(f)
    m = f.match(/(\w+)\s+(\w+)\s*\((.*)\)/)
    if m
      func = m[2]
      args = parse_args(m[3])
      ret = m[1].strip
      FunctionPrototype.new(func, ret, args, f)
    end
  end

  def scan_for_functions(path)
    `nvcc -E -DIN_PUBLIC=1 -D#{NEEDLE}=#{NEEDLE} #{NVCCFLAGS.join(' ')} #{path} | grep -e '#{NEEDLE}'`.
      split("\n").
      collect { |f| m = f.match(/(#{NEEDLE}.*[)]([^;]|$))/); m && m[1] }.
      reject(&:nil?).
      collect { |f| scan_function(f[0, f.index(')') + 1]) }
  end

  StructDef = Struct.new(:name, :definition, :fields)
  StructField = Struct.new(:name, :definition, :type, :pointer)

  def scan_struct_field(field)
    m = field.match(/\s*(((\w+) )+\s*(([*]+)?(\w+))\s*)/m)
    if m
      StructField.new(m[-1], field, m[2].strip, m[-2])
    end
  end
  
  def scan_struct_fields(fields)
    fields.collect { |field| scan_struct_field(field) }.reject(&:nil?)
  end

  def scan_for_structs_inner(data, acc = [])
    m = data.match(/(typedef\s+struct\s+(\w+)\s+{(.*)}\s+[*](\w+);)(.*)/m)
    if m
      acc << StructDef.new(m[4], m[1], scan_struct_fields(m[3].split(';').collect(&:strip)))
      scan_for_structs_inner(m[5], acc)
    else
      acc
    end
  end
  
  def scan_for_structs(path)
    scan_for_structs_inner(File.read(path))
  end

  def scan(path)
    @functions += scan_for_functions(path)
    @structs += scan_for_structs(path)

    self
  end

  def header(name)
    head = File.basename(name).upcase
    <<-EOT
\#ifndef #{head}
\#define #{head}

\#include "public.h"

extern "C" {
  #{structs.collect(&:definition).join('\n')}

  #{functions.collect(&:definition).join(";\n  ")};
}

\#endif /* #{head} */
EOT
  end

  def ffi_type(a, pointer = nil)
    a = @typedefs[a] if @typedefs.has_key?(a)
    if FFI::TypeDefs[a.to_sym]
      ":" + FFI::TypeDefs.key(FFI::TypeDefs[a.to_sym]).to_s
    elsif pointer
      "#{a}.auto_ptr"
    else
      a
    end
  end
  
  def ffi_def(f)
    func = f.name
    args = f.args.collect do |a|
      if a.pointer
        ":pointer"
      else
        ffi_type(a.type)
      end
    end

    ret = f.return_type
    ret = ffi_type(ret, true)

    "    attach_function :#{func}, [ #{args.join(', ')} ], #{ret}"
  end

  def ffi_struct(s)
    layout = s.fields.collect { |f| [ ":#{f.name}", ffi_type(f.type, f.pointer) ] }
    <<-EOT
class #{s.name} < ::FFI::Struct
  layout(#{layout.join(', ')})

  def self.release(ptr)
    #{s.name.downcase}_free(ptr)
  end
end
EOT
  end
  
  def ffi(name, library)
    <<-EOT
module #{name}
  module FFI
    extend ::FFI::Library
    ffi_lib Pathname.new(__FILE__).join('..', "#{library}.#{RbConfig::CONFIG['DLEXT']}").to_s

#{structs.collect { |s| ffi_struct(s) }.compact.join("\n")}
#{functions.collect { |f| ffi_def(f) }.compact.join("\n")}
  end
end
EOT
  end
end

def write_header(header, sources)
  # empty the file for preprocessing
  if header != nil && header != '-'
    File.open(header, 'w') do |f|
      f.puts
    end
  end

  scanner = ExportScanner.new
  sources.each(&-> (s) { scanner.scan(s) })

  if header == nil || header == '-'
    $stdout.puts(scanner.header("STDOUT"))
  else
    File.open(header, 'w') do |f|
      f.puts(scanner.header(header.gsub('.', '_')))
    end
  end
end

def write_ffi(mod, library, output, sources, typedefs)
  raise ArgumentError.new('No --module name given.') if mod == nil
  raise ArgumentError.new('No --library given.') if library == nil

  scanner = ExportScanner.new(typedefs)
  sources.each(&->(s) { scanner.scan(s) })

  if output == nil || output == '-'
    $stdout.puts(scanner.ffi(mod, library))
  else
    File.open(output, 'w') do |f|
      f.puts(scanner.ffi(mod, library))
    end
  end
end

require 'fileutils'
def write_source(output, source)
  FileUtils.copy(source, output)
end


if __FILE__ == $0
  require 'ostruct'
  options = OpenStruct.new
  options.mode = :help
  options.sources = Array.new
  options.typedefs = Hash.new
  
  require 'optparse'
  opts = OptionParser.new() do |o|
    o.on('-h', 'Help') do
      options.mode = :help
    end

    o.on('--header', 'Generate a C/C++ header.') do
      options.mode = :header
    end

    o.on('--ffi', 'Generate a Ruby FFI.') do
      options.mode = :ffi
    end

    o.on('--source', 'Generate C/C++ source code.') do
      options.mode = :source
    end

    o.on('-o', '--output PATH', 'Write the output to PATH.') do |path|
      options.output = path
    end

    o.on('--module NAME', 'The name of the generated FFI module.') do |m|
      options.module = m
    end

    o.on('-l', '--library NAME', 'The shared library name to load.') do |l|
      options.library = l
    end

    o.on('-t', '--typedef TYPE=NAME') do |v|
      type, name = v.split('=')
      options.typedefs[name] = type if type && name
    end
  end

  options.sources = opts.parse!(ARGV)

  unless options.mode == :help
    raise ArgumentError.new("No sources found.") unless options.sources.size > 0
  end

  case options.mode
    when :ffi then write_ffi(options.module, options.library, options.output, options.sources, options.typedefs)
    when :header then write_header(options.output, options.sources)
    when :source then write_source(options.output, options.sources[0])
    else puts(opts.help)
  end
end
