tools/migration: Rename main.lua -> prosody-migrator.lua and update the Makefile
[prosody.git] / tools / migration / prosody-migrator.lua
1 #!/usr/bin/env lua
2
3 CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
4 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
5
6 local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
7
8 -- Command-line parsing
9 local options = {};
10 local handled_opts = 0;
11 for i = 1, #arg do
12         if arg[i]:sub(1,2) == "--" then
13                 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
14                 if opt then
15                         options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
16                 end
17                 handled_opts = i;
18         else
19                 break;
20         end
21 end
22 table.remove(arg, handled_opts);
23
24 -- Load config file
25 local function loadfilein(file, env)
26         if loadin then
27                 return loadin(env, io.open(file):read("*a"));
28         else
29                 local chunk, err = loadfile(file);
30                 if chunk then
31                         setfenv(chunk, env);
32                 end
33                 return chunk, err;
34         end
35 end
36
37 local config_file = options.config or default_config;
38 local from_store = arg[1] or "input";
39 local to_store = arg[2] or "output";
40
41 config = {};
42 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
43 local config_chunk, err = loadfilein(config_file, config_env);
44 if not config_chunk then
45         print("There was an error loading the config file, check the file exists");
46         print("and that the syntax is correct:");
47         print("", err);
48         os.exit(1);
49 end
50
51 config_chunk();
52
53 if CFG_SOURCEDIR then
54         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
55         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
56 elseif not package.loaded["util.json"] then
57         package.path = "../../?.lua;"..package.path
58         package.cpath = "../../?.so;"..package.cpath
59 end
60
61 local have_err;
62 if #arg > 0 and #arg ~= 2 then
63         have_err = true;
64         print("Error: Incorrect number of parameters supplied.");
65 end
66 if not config[from_store] then
67         have_err = true;
68         print("Error: Input store '"..from_store.."' not found in the config file.");
69 end
70 if not config[to_store] then
71         have_err = true;
72         print("Error: Output store '"..to_store.."' not found in the config file.");
73 end
74 if not config[from_store].type then
75         have_err = true;
76         print("Error: Input store type not specified in the config file");
77 elseif not pcall(require, "migrator."..config[from_store].type) then
78         have_err = true;
79         print("Error: Unrecognised store type for '"..from_store.."': "..config[from_store].type);
80 end
81 if not config[to_store].type then
82         have_err = true;
83         print("Error: Output store type not specified in the config file");
84 elseif not pcall(require, "migrator."..config[to_store].type) then
85         have_err = true;
86         print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type);
87 end
88
89 if have_err then
90         print("");
91         print("Usage: "..arg[0].." FROM_STORE TO_STORE");
92         print("If no stores are specified, 'input' and 'output' are used.");
93         print("");
94         print("The available stores in your migrator config are:");
95         print("");
96         for store in pairs(config) do
97                 print("", store);
98         end
99         print("");
100         os.exit(1);
101 end
102         
103 local itype = config[from_store].type;
104 local otype = config[to_store].type;
105 local reader = require("migrator."..itype).reader(config[from_store]);
106 local writer = require("migrator."..otype).writer(config[to_store]);
107
108 local json = require "util.json";
109
110 for x in reader do
111         --print(json.encode(x))
112         writer(x);
113 end
114 writer(nil); -- close
115