aboutsummaryrefslogtreecommitdiffstats
path: root/eatit.lua
blob: 499faeddb61a6ec48538d001651b9b49003510ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local mp = require('mp')

dofile(mp.command_native({'expand-path', '~~/eatit-cfg.lua'}))

local function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

local function logwrite(string)
  if opts.logging.log then
    io.write(os.date(opts.logging.logdate) .. ' ' .. string .. '\n')
  end
end

local function getgit()
  os.execute('mkdir -p ' .. opts.dl.dir)

  logwrite('Starting Download...')
  for i in pairs(plugins) do
    logwrite('downloading ' .. plugins[i][1])

    -- construct the git cmd
    local cmd = 'git -C ' .. opts.dl.dir .. ' clone ' .. plugins[i][1]

    -- run the command and get the result for logging
    local run = io.popen(cmd)
    -- BUG: logwriting the git command doens't actually log the output
    -- (commented out for now)
    -- logwrite(run:read('*a'))
    run:close()

    -- get the file's dir
    local pluginfile = opts.dl.dir .. '/' ..
      string.match(plugins[i][1], '/([^/]+)$') ..
      '/' .. plugins[i][2]

    -- get the dest dir
    local destfile = mp.command_native({'expand-path', '~~/'}) ..
      '/' .. plugins[i][3] ..
      '/' .. plugins[i][2]

    -- copy the file contents over to the desired location
    local infile = io.open(pluginfile, 'r')
    local outfile = io.open(destfile, 'w')
    outfile:write(infile:read('*a'))
    outfile:close()
    infile:close()
  end
end

local function init()
  if opts.logging.log then -- log if asked to
    -- get our logfile's full path
    local fn = mp.command_native({'expand-path', opts.logging.logfile})

    os.remove(fn) -- remove logfile so that we start fresh
    f = io.open(fn, 'a') -- open file buffer
    io.output(f) -- set it as default
  end

  logwrite('# of plugins defined in table: ' .. tablelength(plugins))
  getgit()

  -- closing/removing everything
  if opts.dl.powerwash == true then
    logwrite('powerwashing the tmp dir (' .. opts.dl.dir .. ')')
    os.execute('rm -rf ' .. opts.dl.dir)
  end
  if opts.logging.log then
    io.close(f)
  end
end

mp.add_key_binding('U', 'UpdatePlugins', init)