aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/unpacker.lua
blob: 60da25dad1740eadd96cc327281014eec5f0d33b (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env luajit

local args = ...
local tmpFile = os.tmpname()

readCommand = function (command)
  os.execute(command .. ' >' .. tmpFile)
  local tf = io.open(tmpFile, 'r')
  local result = tf:read()
  tf:close()
  return result
end

function scanDir(directory)
  local i, t, popen = 0, {}, io.popen
--  for filename in popen('dir "'..directory..'" /b'):lines() do
  for filename in popen('ls -AF "'..directory..'"'):lines() do
    i = i + 1
    t[i] = filename
  end
  return t
end


--workingDir = readCommand('pwd')
--baseDir = workingDir
--baseDir = string.gsub(baseDir, '(.*)/.-$', '%1')

--bin_d    = baseDir .. ''
--lib_d    = baseDir .. '/lib'
--data_d   = baseDir .. '/media'
--locale_d = baseDir .. '/locale'
home_d   = readCommand('echo "$HOME"')


unpackers =
{
  iar		= function (src, dst)  return('tar -xzf ' .. src .. ' -C ' .. dst)  end,
  oar		= function (src, dst)  return('tar -xzf ' .. src .. ' -C ' .. dst)  end,
  rar		= function (src, dst)  return('unrar x '  .. src .. ' '    .. dst)  end,
  tar_bz2	= function (src, dst)  return('tar -xzf ' .. src .. ' -C ' .. dst)  end,
  tar_gz	= function (src, dst)  return('tar -xzf ' .. src .. ' -C ' .. dst)  end,
  tgz		= function (src, dst)  return('tar -xzf ' .. src .. ' -C ' .. dst)  end,
  zip		= function (src, dst)  return('unzip '    .. src .. ' -d ' .. dst)  end,
}

print('Searching for archives in ' .. home_d .. '/.SledjHamr', ' -> ', ' unpack into ' .. home_d .. '/.SledjHamr/.cache/unpacked/')

for k, v in pairs(scanDir(home_d .. '/.SledjHamr')) do

  -- First find if there's one of the special flags at the end, and strip it off.
  t = string.sub(v, -1, -1)
  v = string.sub(v, 1, -2)
  f = true
      if '@' == t then t = t
  elseif '*' == t then t = t
  elseif '/' == t then f = false
  elseif '=' == t then t = t
  elseif '>' == t then t = t
  elseif '|' == t then t = t
  else   v = v .. t;   t = ' ';
  end

  -- Figure out what sort of file it is.
  name, ext = string.match(v, "(.*)%.(.*)$")
  if f and nil ~= ext then
    name1, tar = string.match(name, "(.*)%.(.*)$")
    if 'tar' == tar then
      name = name1
      ext  = tar .. '_' .. ext
    end

    ext = string.lower(ext)
    u = unpackers[ext]
    if nil ~= u then
      src = home_d .. '/.SledjHamr/' .. v
      dst = home_d .. '/.SledjHamr/.cache/unpacked/' .. name

      os.execute('rm -fr ' .. dst)
      os.execute('mkdir -p ' .. dst)
      print('un' .. string.upper(ext) .. 'ing ', '"' .. v .. '"', ' -> ', '"' .. name .. '"')
          if '@' == t then print('  ' .. v, ' is a  soft link.')
      elseif '*' == t then print('  ' .. v, ' is an executable.')
      elseif '/' == t then print('  ' .. v, ' is a  directory.');
      elseif '=' == t then print('  ' .. v, ' is a  socket.')
      elseif '>' == t then print('  ' .. v, ' is a  door.')
      elseif '|' == t then print('  ' .. v, ' is a  FIFO.')
--      else                 print('  ' .. v, ' is an ordinary file.')
      end
      os.execute(u(src, dst) .. ' >/dev/null')
    end
  end
end