aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/bin/unpacker.lua
diff options
context:
space:
mode:
Diffstat (limited to 'bin/unpacker.lua')
-rwxr-xr-xbin/unpacker.lua105
1 files changed, 105 insertions, 0 deletions
diff --git a/bin/unpacker.lua b/bin/unpacker.lua
new file mode 100755
index 0000000..c60ef30
--- /dev/null
+++ b/bin/unpacker.lua
@@ -0,0 +1,105 @@
1#!/usr/bin/env luajit
2
3local args = ...
4local tmpFile = os.tmpname()
5
6readCommand = 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
12end
13
14function 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
22end
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'
33home_d = readCommand('echo "$HOME"')
34
35
36unpackers =
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--[[ TODO
48Make it accept arguments - file dir
49 file = archive file, or directory to scan
50 dir = output directory
51
52If it's an OAR, or other sim defining format, or a model format, then create .omg files, stuff into .cache/converted/ or inventory/converted.
53 Or should conversion be a separate tool?
54 The problem is that we need to link back to the original archive, or carry other info like the archive type with us from here.
55 So might as well start the process by creating basic .omg files, with the info we have here, before we lose that info.
56
57]]
58
59print('Searching for archives in ' .. home_d .. '/.SledjHamr', ' -> ', ' unpack into ' .. home_d .. '/.SledjHamr/.cache/unpacked/')
60
61for k, v in pairs(scanDir(home_d .. '/.SledjHamr')) do
62
63 -- First find if there's one of the special flags at the end, and strip it off.
64 t = string.sub(v, -1, -1)
65 v = string.sub(v, 1, -2)
66 f = true
67 if '@' == t then t = t
68 elseif '*' == t then t = t
69 elseif '/' == t then f = false
70 elseif '=' == t then t = t
71 elseif '>' == t then t = t
72 elseif '|' == t then t = t
73 else v = v .. t; t = ' ';
74 end
75
76 -- Figure out what sort of file it is.
77 name, ext = string.match(v, "(.*)%.(.*)$")
78 if f and nil ~= ext then
79 name1, tar = string.match(name, "(.*)%.(.*)$")
80 if 'tar' == tar then
81 name = name1
82 ext = tar .. '_' .. ext
83 end
84
85 ext = string.lower(ext)
86 u = unpackers[ext]
87 if nil ~= u then
88 src = home_d .. '/.SledjHamr/' .. v
89 dst = home_d .. '/.SledjHamr/.cache/unpacked/' .. name
90
91 os.execute('rm -fr ' .. dst)
92 os.execute('mkdir -p ' .. dst)
93 print('un' .. string.upper(ext) .. 'ing ', '"' .. v .. '"', ' -> ', '"' .. name .. '"')
94 if '@' == t then print(' ' .. v, ' is a soft link.')
95 elseif '*' == t then print(' ' .. v, ' is an executable.')
96 elseif '/' == t then print(' ' .. v, ' is a directory.');
97 elseif '=' == t then print(' ' .. v, ' is a socket.')
98 elseif '>' == t then print(' ' .. v, ' is a door.')
99 elseif '|' == t then print(' ' .. v, ' is a FIFO.')
100-- else print(' ' .. v, ' is an ordinary file.')
101 end
102 os.execute(u(src, dst) .. ' >/dev/null')
103 end
104 end
105end