summaryrefslogtreecommitdiff
path: root/server/src/main.cpp
blob: 08fe5b7346c9b4854f67d4df483a8090e9dadf43 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
	Copyright © 2015 Gluzskiy Alexandr (sss)

    This file is part of Unknown Download Manager (UDM).

    UDM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    UDM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with UDM.  If not, see <http://www.gnu.org/licenses/>.

*/
#include <boost/program_options.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/filesystem.hpp>
#include <iostream>

#include <api_module_metadata_storage.h>
#include <modules_handler.h>


core_api *module_api = nullptr;

modules_handler *modules = nullptr;

namespace bpo = boost::program_options;
namespace bpt = boost::property_tree;

bpt::ptree config;



int main(int argc, char *argv[])
{
	bpo::options_description desc("Available commands and options");
	desc.add_options()
		("help", "this message")
		("daemon", "fork to background")
		("config", bpo::value<std::string>(), "use specified config file instead of \"~/.config/udm/udm.conf\"")
		("list-modules", "list all installed modules")
		("run", "start UDM server")
	;

	bool daemon = false, run = false;
	std::string config_path = "~/.config/udm/udm.conf";
	try{

		bpo::variables_map vm;
		bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
		bpo::notify(vm);

		//load config first, as it may be needed for other commands
		if(vm.count("config"))
			config_path = vm["config"].as<std::string>();

		{
			std::string tmp_path = config_path, home = getenv("HOME");
			for(std::string::size_type p1 = tmp_path.find("~"); p1 != std::string::npos; p1 = tmp_path.find("~", p1))
				tmp_path.replace(p1, 1, home);
			if(boost::filesystem::exists(tmp_path) && boost::filesystem::is_regular(tmp_path))
				bpt::read_info(tmp_path, config); //TODO: finish this
			else
				std::cerr<<"failed to load config: \"" + config_path + "\", file does not exists or is not regular file\n";
		}


		if(vm.count("help"))
		{
			std::cout<<desc<<std::endl;
			return 0;
		}
		if(vm.count("daemon"))
		{
			daemon = true;
		}
		if(vm.count("list-modules"))
		{
			if(!module_api)
				module_api = new core_api;
			if(!modules)
				modules = new modules_handler;
			std::cout<<modules->list_modules();
			return 0;
		}
		if(vm.count("run"))
		{
			run = true;
		}
	}
	catch(std::exception &e)
	{
		std::cerr<<"error: "<<e.what()<<std::endl;
		std::cout<<desc<<std::endl;
		return -1;
	}
	catch(...)
	{
		//noop
		return -1;
	}
	if(run)
	{
		if(daemon)
		{
			//TODO: fork here
		}
	}
	//module_api must be created first
	std::cerr<<"error: no command specified"<<std::endl;
	std::cout<<desc<<std::endl;

	return 0;
}