require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
require 'puppet-syntax/tasks/puppet-syntax'
require 'metadata-json-lint/rake_task'

PuppetLint.configuration.fail_on_warnings = true
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.send('disable_class_inherits_from_params_class')
PuppetLint.configuration.send('disable_class_parameter_defaults')

exclude_paths = [
    "pkg/**/*",
    "vendor/**/*",
    "spec/**/*",
]
PuppetLint.configuration.ignore_paths = exclude_paths
PuppetSyntax.exclude_paths = exclude_paths

desc "Run metadata_lint, lint, syntax, and spec tests."
task :test => [
  :metadata_lint,
  :lint,
  :syntax,
  :spec
]

# Check for the presence of Lua 5.1
lua_version = %x( lua5.1 -v 2>&1 )
lua_present = $?.to_i == 0 ? (lua_version =~ /^Lua 5\.1/) : false
if lua_present then
    task :test => :lua_tests
end

LUA_TESTS = Rake::FileList.new("tests/**/test_*.lua")
desc "Run Lua unit tests."
task :lua_tests => [:lua_libraries, :lib_cbuf] do |t|
    verbose(false) do
        sh "lua5.1", "-e", "require('lpeg')" do |ok, res|
            if ! ok then
                raise "Please install the Lua LPEG package by running: apt-get install lua-lpeg"
            end
        end
    end
    LUA_TESTS.each do |f|
        sh "lua5.1", f
    end
    Rake::Task[:remove_cbuf_library].invoke
end

# Need to pull date_time.lua from the lua_sandbox repository because some tests
# depend on it indirectly
task :lua_libraries => ['tests/lua/mocks/date_time.lua', 'tests/lua/mocks/inspect.lua',
                        'tests/lua/mocks/anomaly.lua', 'tests/lua/mocks/annotation.lua']

file 'tests/lua/mocks/annotation.lua' do |t|
    verbose(false) do
        sh "curl", "-s", "-o", t.name, "https://raw.githubusercontent.com/mozilla-services/heka/dev/sandbox/lua/modules/annotation.lua" do |ok, res|
            if ! ok then
                raise "Fail to download annotation.lua from lua_sandbox repository!"
            end
        end
    end
end

file 'tests/lua/mocks/anomaly.lua' do |t|
    verbose(false) do
        sh "curl", "-s", "-o", t.name, "https://raw.githubusercontent.com/mozilla-services/heka/dev/sandbox/lua/modules/anomaly.lua" do |ok, res|
            if ! ok then
                raise "Fail to download anomaly.lua from lua_sandbox repository!"
            end
        end
    end
end

file 'tests/lua/mocks/date_time.lua' do |t|
    verbose(false) do
        sh "curl", "-s", "-o", t.name, "https://raw.githubusercontent.com/mozilla-services/lua_sandbox/master/modules/date_time.lua" do |ok, res|
            if ! ok then
                raise "Fail to download date_time.lua from lua_sandbox repository!"
            end
        end
    end
end

file 'tests/lua/mocks/inspect.lua' do |t|
    verbose(false) do
        sh "curl", "-s", "-o", t.name, "https://raw.githubusercontent.com/kikito/inspect.lua/master/inspect.lua" do |ok, res|
            if ! ok then
                raise "Fail to download inspect.lua from gitub repository!"
            end
        end
    end
end

task :lib_cbuf => ['./circular_buffer.so']

cbuf_commit = 'bb6dd9f88f148813315b5a660b7e2ba47f958b31'
cbuf_tarball_url = "https://github.com/mozilla-services/lua_circular_buffer/archive/#{ cbuf_commit }.tar.gz"

file './circular_buffer.so' do |t|
    tmp_directory = File.join('/tmp', "lua_circular_buffer-#{ cbuf_commit }")
    cbuf_shared_library = File.join(tmp_directory, 'release', 'circular_buffer.so')
    unless File.exists?(cbuf_shared_library) then
        # Download the archive from Github and build the library
        sh "rm -rf #{ tmp_directory } && wget -qO - #{ cbuf_tarball_url } | tar -zxvf - -C /tmp" do |ok, res|
            unless ok then
                raise "Failed to download the lua_circular_buffer archive!"
            end
        end

        sh "cd #{ tmp_directory } && mkdir release && cd release && cmake -DCMAKE_BUILD_TYPE=release .. && make" do |ok, res|
            unless ok then
                raise "Failed to compile circular_buffer.so!"
            end
        end
    end

    FileUtils.cp(cbuf_shared_library, '.')
end

task :remove_cbuf_library do |t|
    verbose(false) do
        sh 'rm -f ./circular_buffer.so'
    end
end
