-- Copyright (c) 2017-present, Facebook, Inc.
-- All rights reserved.

-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree.

#!/bin/lua

local xlabel
local ylabel
local title

for i=#arg,1,-1 do
   if arg[i] == '-x' then
      table.remove(arg, i)
      xlabel = assert(table.remove(arg, i), '-x <string> expected')
   elseif arg[i] == '-y' then
      table.remove(arg, i)
      ylabel = assert(table.remove(arg, i), '-y <string> expected')
   elseif arg[i] == '-t' then
      table.remove(arg, i)
      title = assert(table.remove(arg, i), '-t <string> expected')
   end
end
if #arg > 0 then
   error(string.format([[
usage: %s [options]
options:
-x <string>  -- x legend
-y <string>  -- y legend
-t <string>  -- title
]], arg[0]))
end

local config = [[

red_000 = "#F9B7B0"
red_025 = "#F97A6D"
red_050 = "#E62B17"
red_075 = "#8F463F"
red_100 = "#6D0D03"

blue_000 = "#A9BDE6"
blue_025 = "#7297E6"
blue_050 = "#1D4599"
blue_075 = "#2F3F60"
blue_100 = "#031A49"

green_000 = "#A6EBB5"
green_025 = "#67EB84"
green_050 = "#11AD34"
green_075 = "#2F6C3D"
green_100 = "#025214"

brown_000 = "#F9E0B0"
brown_025 = "#F9C96D"
brown_050 = "#E69F17"
brown_075 = "#8F743F"
brown_100 = "#6D4903"

grid_color = "#d5e0c9"
text_color = "#6a6a6a"

my_font = "Verdana, 8"

my_line_width = "2"
my_axis_width = "1.5"
my_ps = "1.2"

set term pngcairo enhanced font my_font size 1024,768

set pointsize my_ps

set style line 1 linecolor rgbcolor blue_025 linewidth my_line_width pt 7
set style line 2 linecolor rgbcolor green_025 linewidth my_line_width pt 5
set style line 3 linecolor rgbcolor red_025 linewidth my_line_width pt 9
set style line 4 linecolor rgbcolor brown_025 linewidth my_line_width pt 13
set style line 5 linecolor rgbcolor blue_050 linewidth my_line_width pt 11
set style line 6 linecolor rgbcolor green_050 linewidth my_line_width pt 7
set style line 7 linecolor rgbcolor red_050 linewidth my_line_width pt 5
set style line 8 linecolor rgbcolor brown_050 linewidth my_line_width pt 9
set style line 9 linecolor rgbcolor blue_075 linewidth my_line_width pt 13
set style line 10 linecolor rgbcolor green_075 linewidth my_line_width pt 11
set style line 11 linecolor rgbcolor red_075 linewidth my_line_width pt 7
set style line 12 linecolor rgbcolor brown_075 linewidth my_line_width pt 5
set style line 13 linecolor rgbcolor blue_100 linewidth my_line_width pt 9
set style line 14 linecolor rgbcolor green_100 linewidth my_line_width pt 13
set style line 15 linecolor rgbcolor red_100 linewidth my_line_width pt 11
set style line 16 linecolor rgbcolor brown_100 linewidth my_line_width pt 7
set style line 17 linecolor rgbcolor "#224499" linewidth my_line_width pt 5

set style increment user

set xtics textcolor rgb text_color font my_font
set ytics textcolor rgb text_color font my_font
set ztics textcolor rgb text_color font my_font

set label textcolor rgb text_color font my_font

set border 31 lw my_axis_width lc rgb text_color

set key width 1 height 1 noenhanced spacing 1

]]

local data = io.read('*all')
local header = data:match('([^\n]+)')
local tmpfname = os.tmpname()
local f = io.open(tmpfname, "w")
f:write(data)
f:close()

local plots = {}
local i = 0
for xt, yt in header:gmatch('(%S+)%s+(%S+)') do
   yt = yt:gsub("_", "\\_")
   io.stderr:write(yt)
   io.stderr:write('\n')
   i = i + 1
   table.insert(
      plots,
      string.format(
         '"%s" using %d:%d with linespoints title "%s"',
         tmpfname,
         (i-1)*2+1,
         (i-1)*2+2, yt
      )
   )
end

local plot = {}
if xlabel then
   table.insert(
      plot,
      string.format(
         'set xlabel "%s" textcolor rgb text_color font my_font',
         xlabel
      )
   )
end
if ylabel then
   table.insert(
      plot,
      string.format(
         'set ylabel "%s" textcolor rgb text_color font my_font',
         ylabel
      )
   )
end
if title then
   table.insert(
      plot,
      string.format(
         'set title "%s" textcolor rgb text_color font my_font',
         title
      )
   )
end
table.insert(
   plot,
   string.format("plot %s\n", table.concat(plots, ", "))
)
plot = table.concat(plot, "\n")

local f = io.popen('gnuplot', 'w')
f:write(config)
f:write(plot)
f:close()

os.execute(string.format('rm -f "%s"', tmpfname))
