diff options
Diffstat (limited to '')
-rwxr-xr-x | tools/unpacker.lua | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/tools/unpacker.lua b/tools/unpacker.lua new file mode 100755 index 0000000..60da25d --- /dev/null +++ b/tools/unpacker.lua | |||
@@ -0,0 +1,93 @@ | |||
1 | #!/usr/bin/env luajit | ||
2 | |||
3 | local args = ... | ||
4 | local tmpFile = os.tmpname() | ||
5 | |||
6 | readCommand = function (command) | ||
7 | os.execute(command .. ' >' .. tmpFile) | ||
8 | local tf = io.open(tmpFile, 'r') | ||
9 | local result = tf:read() | ||
10 | tf:close() | ||
11 | return result | ||
12 | end | ||
13 | |||
14 | function scanDir(directory) | ||
15 | local i, t, popen = 0, {}, io.popen | ||
16 | -- for filename in popen('dir "'..directory..'" /b'):lines() do | ||
17 | for filename in popen('ls -AF "'..directory..'"'):lines() do | ||
18 | i = i + 1 | ||
19 | t[i] = filename | ||
20 | end | ||
21 | return t | ||
22 | end | ||
23 | |||
24 | |||
25 | --workingDir = readCommand('pwd') | ||
26 | --baseDir = workingDir | ||
27 | --baseDir = string.gsub(baseDir, '(.*)/.-$', '%1') | ||
28 | |||
29 | --bin_d = baseDir .. '' | ||
30 | --lib_d = baseDir .. '/lib' | ||
31 | --data_d = baseDir .. '/media' | ||
32 | --locale_d = baseDir .. '/locale' | ||
33 | home_d = readCommand('echo "$HOME"') | ||
34 | |||
35 | |||
36 | unpackers = | ||
37 | { | ||
38 | iar = function (src, dst) return('tar -xzf ' .. src .. ' -C ' .. dst) end, | ||
39 | oar = function (src, dst) return('tar -xzf ' .. src .. ' -C ' .. dst) end, | ||
40 | rar = function (src, dst) return('unrar x ' .. src .. ' ' .. dst) end, | ||
41 | tar_bz2 = function (src, dst) return('tar -xzf ' .. src .. ' -C ' .. dst) end, | ||
42 | tar_gz = function (src, dst) return('tar -xzf ' .. src .. ' -C ' .. dst) end, | ||
43 | tgz = function (src, dst) return('tar -xzf ' .. src .. ' -C ' .. dst) end, | ||
44 | zip = function (src, dst) return('unzip ' .. src .. ' -d ' .. dst) end, | ||
45 | } | ||
46 | |||
47 | print('Searching for archives in ' .. home_d .. '/.SledjHamr', ' -> ', ' unpack into ' .. home_d .. '/.SledjHamr/.cache/unpacked/') | ||
48 | |||
49 | for k, v in pairs(scanDir(home_d .. '/.SledjHamr')) do | ||
50 | |||
51 | -- First find if there's one of the special flags at the end, and strip it off. | ||
52 | t = string.sub(v, -1, -1) | ||
53 | v = string.sub(v, 1, -2) | ||
54 | f = true | ||
55 | if '@' == t then t = t | ||
56 | elseif '*' == t then t = t | ||
57 | elseif '/' == t then f = false | ||
58 | elseif '=' == t then t = t | ||
59 | elseif '>' == t then t = t | ||
60 | elseif '|' == t then t = t | ||
61 | else v = v .. t; t = ' '; | ||
62 | end | ||
63 | |||
64 | -- Figure out what sort of file it is. | ||
65 | name, ext = string.match(v, "(.*)%.(.*)$") | ||
66 | if f and nil ~= ext then | ||
67 | name1, tar = string.match(name, "(.*)%.(.*)$") | ||
68 | if 'tar' == tar then | ||
69 | name = name1 | ||
70 | ext = tar .. '_' .. ext | ||
71 | end | ||
72 | |||
73 | ext = string.lower(ext) | ||
74 | u = unpackers[ext] | ||
75 | if nil ~= u then | ||
76 | src = home_d .. '/.SledjHamr/' .. v | ||
77 | dst = home_d .. '/.SledjHamr/.cache/unpacked/' .. name | ||
78 | |||
79 | os.execute('rm -fr ' .. dst) | ||
80 | os.execute('mkdir -p ' .. dst) | ||
81 | print('un' .. string.upper(ext) .. 'ing ', '"' .. v .. '"', ' -> ', '"' .. name .. '"') | ||
82 | if '@' == t then print(' ' .. v, ' is a soft link.') | ||
83 | elseif '*' == t then print(' ' .. v, ' is an executable.') | ||
84 | elseif '/' == t then print(' ' .. v, ' is a directory.'); | ||
85 | elseif '=' == t then print(' ' .. v, ' is a socket.') | ||
86 | elseif '>' == t then print(' ' .. v, ' is a door.') | ||
87 | elseif '|' == t then print(' ' .. v, ' is a FIFO.') | ||
88 | -- else print(' ' .. v, ' is an ordinary file.') | ||
89 | end | ||
90 | os.execute(u(src, dst) .. ' >/dev/null') | ||
91 | end | ||
92 | end | ||
93 | end | ||