Add LICENSE file and Refer to it from the source. Include license material
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* $Id: metaproxy_prog.cpp,v 1.6 2006-06-10 14:29:12 adam Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8
9 #include <boost/program_options.hpp>
10 namespace po = boost::program_options;
11
12 #include <iostream>
13 #include <stdexcept>
14
15 #include "filter.hpp"
16 #include "package.hpp"
17 #include "router_flexml.hpp"
18 #include "factory_static.hpp"
19
20 namespace mp = metaproxy_1;
21
22 int main(int argc, char **argv)
23 {
24     try 
25     {
26         po::options_description desc("Allowed options");
27         desc.add_options()
28             ("help,h", "produce help message")
29             ("version,V", "show version")
30             ("config", po::value< std::vector<std::string> >(), "xml config")
31             ;
32         
33         po::positional_options_description p;
34         p.add("config", -1);
35         
36         po::variables_map vm;        
37         po::store(po::command_line_parser(argc, argv).
38                   options(desc).positional(p).run(), vm);
39         po::notify(vm);
40         
41         if (vm.count("help")) {
42             std::cout << desc << "\n";
43             return 1;
44         }
45         if (vm.count("version")) {
46             std::cout << "Metaproxy " VERSION "\n";
47             return 0;
48         }
49         xmlDocPtr doc = 0;
50         if (vm.count("config"))
51         {
52             std::vector<std::string> config_fnames = 
53                 vm["config"].as< std::vector<std::string> >();
54
55             if (config_fnames.size() != 1)
56             {
57                 std::cerr << "Only one configuration must be given\n";
58                 std::exit(1);
59             }
60             
61             doc = xmlParseFile(config_fnames[0].c_str());
62             if (!doc)
63             {
64                 std::cerr << "xmlParseFile failed\n";
65                 std::exit(1);
66             }
67         }
68         else
69         {
70             std::cerr << "No configuration given\n";
71             std::exit(1);
72         }
73         if (doc)
74         {
75             try {
76                 mp::FactoryStatic factory;
77                 mp::RouterFleXML router(doc, factory);
78                 mp::Package pack;
79                 pack.router(router).move();
80             }
81             catch (std::runtime_error &e) {
82                 std::cerr << "std::runtime error: " << e.what() << "\n";
83                 std::exit(1);
84             }
85             xmlFreeDoc(doc);
86         }
87     }
88     catch (po::unknown_option &e) {
89         std::cerr << e.what() << "; use --help for list of options\n";
90         std::exit(1);
91     }
92     catch (std::logic_error &e) {
93         std::cerr << "std::logic error: " << e.what() << "\n";
94         std::exit(1);
95     }
96     catch (std::runtime_error &e) {
97         std::cerr << "std::runtime error: " << e.what() << "\n";
98         std::exit(1);
99     }
100     catch ( ... ) {
101         std::cerr << "Unknown Exception" << std::endl;
102         std::exit(1);
103     }
104     std::exit(0);
105 }
106
107
108 /*
109  * Local variables:
110  * c-basic-offset: 4
111  * indent-tabs-mode: nil
112  * c-file-style: "stroustrup"
113  * End:
114  * vim: shiftwidth=4 tabstop=8 expandtab
115  */