]> git.mxchange.org Git - simgear.git/blob - simgear/package/pkgutil.cxx
e8cd9bc24d56fd9846f0683b4ed5d674aa2e73af
[simgear.git] / simgear / package / pkgutil.cxx
1 #include <simgear/io/HTTPClient.hxx>
2 #include <simgear/package/Catalog.hxx>
3 #include <simgear/package/Package.hxx>
4 #include <simgear/package/Install.hxx>
5 #include <simgear/package/Root.hxx>
6 #include <simgear/misc/sg_dir.hxx>
7
8 #include <boost/foreach.hpp>
9 #include <iostream>
10 #include <cstring>
11
12 using namespace simgear; 
13 using namespace std;
14
15 bool keepRunning = true;
16
17 int main(int argc, char** argv)
18 {
19
20     HTTP::Client* http = new HTTP::Client();
21     pkg::Root* root = new pkg::Root(Dir::current().path());
22     
23     cout << "Package root is:" << Dir::current().path() << endl;
24     cout << "have " << pkg::Catalog::allCatalogs().size() << " catalog(s)" << endl;
25         
26     root->setHTTPClient(http);
27     
28     if (!strcmp(argv[1], "add")) {
29         std::string url(argv[2]);
30         pkg::Catalog::createFromUrl(root, url);
31     } else if (!strcmp(argv[1], "refresh")) {
32         root->refresh();
33     } else if (!strcmp(argv[1], "install")) {
34         pkg::Package* pkg = root->getPackageById(argv[2]);
35         if (!pkg) {
36             cerr << "unknown package:" << argv[2] << endl;
37             return EXIT_FAILURE;
38         }
39         
40         if (pkg->isInstalled()) {
41             cout << "package " << pkg->id() << " is already installed at " << pkg->install()->path() << endl;
42             return EXIT_SUCCESS;
43         }
44         
45         pkg::Catalog* catalog = pkg->catalog();
46         cout << "Will install:" << pkg->id() << " from " << catalog->id() <<
47                 "(" << catalog->description() << ")" << endl;
48         pkg->install();
49     } else if (!strcmp(argv[1], "uninstall") || !strcmp(argv[1], "remove")) {
50         pkg::Package* pkg = root->getPackageById(argv[2]);
51         if (!pkg) {
52             cerr << "unknown package:" << argv[2] << endl;
53             return EXIT_FAILURE;
54         }
55         
56         if (!pkg->isInstalled()) {
57             cerr << "package " << argv[2] << " not installed" << endl;
58             return EXIT_FAILURE;
59         }
60         
61         cout << "Will uninstall:" << pkg->id() << endl;
62         pkg->install()->uninstall();
63     } else if (!strcmp(argv[1], "update-all")) {
64         pkg::PackageList updates = root->packagesNeedingUpdate();
65         BOOST_FOREACH(pkg::Package* p, updates) {
66             root->scheduleToUpdate(p->install());
67         }
68     } else if (!strcmp(argv[1], "list-updated")) {
69         pkg::PackageList updates = root->packagesNeedingUpdate();
70         if (updates.empty()) {
71             cout << "no packages with updates" << endl;
72             return EXIT_SUCCESS;
73         }
74         
75         cout << updates.size() << " packages have updates" << endl;
76         BOOST_FOREACH(pkg::Package* p, updates) {
77             cout << "\t" << p->id() << " " << p->getLocalisedProp("name") << endl;
78         }
79     } else {
80         cerr << "unknown command:" << argv[1] << endl;
81         return EXIT_FAILURE;
82     }
83     
84     while (http->hasActiveRequests()) {
85         http->update();
86     }
87
88     return EXIT_SUCCESS;
89 }