description = [[
vulns-test-1 script. This is a hostrule script.
]]

-- @args vulns-test-1.number  The number of vulnerability table to
--       generate. Default will be 10 for each host.

categories = {"vuln"}

require 'stdnse'
require 'vulns'

local vuln_number = stdnse.get_script_args(SCRIPT_NAME..".number")
vuln_number = tonumber(vuln_number) or 10

local nmap_id_link = function(id)
  return string.format("%s%s", "http://scanme.org/id?",id)
end

hostrule = function(host)
  -- We call it here so it can register the popular link for all the
  -- hostrule/portrule scripts.
  vulns.register_popular_id('NMAP', nmap_id_link)
  return true
end

local generate_vuln = function()
  local list = {}
  for i = 1, vuln_number do
    local state = (i % vulns.STATE.NOT_VULN == 0)
        and vulns.STATE.NOT_VULN or vulns.STATE.VULN
    local vuln = {
      title = "Vuln X "..i, -- Make Lua create different string objects
      state = state,
      IDS = {NMAP = tostring(i)},
      description = string.format("Generated vulnerability ... %d - by %s",
                        i, SCRIPT_NAME) -- Make Lua create different string objects
    }
    table.insert(list, vuln)
  end
  -- Will fail if there are more than 8000 element
  return unpack(list)
end

action = function(host)
  local report = vulns.Report:new(SCRIPT_NAME, host)
  return report:make_output(generate_vuln())
end
