]> git.mxchange.org Git - simgear.git/blob - simgear/package/pkgutil.cxx
LGPL license on package files.
[simgear.git] / simgear / package / pkgutil.cxx
1 // Copyright (C) 2013  James Turner - zakalawe@mac.com
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #include <simgear/io/HTTPClient.hxx>
19 #include <simgear/package/Catalog.hxx>
20 #include <simgear/package/Package.hxx>
21 #include <simgear/package/Install.hxx>
22 #include <simgear/package/Root.hxx>
23 #include <simgear/misc/sg_dir.hxx>
24
25 #include <boost/foreach.hpp>
26 #include <iostream>
27 #include <cstring>
28
29 using namespace simgear; 
30 using namespace std;
31
32 bool keepRunning = true;
33
34 int main(int argc, char** argv)
35 {
36
37     HTTP::Client* http = new HTTP::Client();
38     pkg::Root* root = new pkg::Root(Dir::current().path());
39     
40     cout << "Package root is:" << Dir::current().path() << endl;
41     cout << "have " << pkg::Catalog::allCatalogs().size() << " catalog(s)" << endl;
42         
43     root->setHTTPClient(http);
44     
45     if (!strcmp(argv[1], "add")) {
46         std::string url(argv[2]);
47         pkg::Catalog::createFromUrl(root, url);
48     } else if (!strcmp(argv[1], "refresh")) {
49         root->refresh();
50     } else if (!strcmp(argv[1], "install")) {
51         pkg::Package* pkg = root->getPackageById(argv[2]);
52         if (!pkg) {
53             cerr << "unknown package:" << argv[2] << endl;
54             return EXIT_FAILURE;
55         }
56         
57         if (pkg->isInstalled()) {
58             cout << "package " << pkg->id() << " is already installed at " << pkg->install()->path() << endl;
59             return EXIT_SUCCESS;
60         }
61         
62         pkg::Catalog* catalog = pkg->catalog();
63         cout << "Will install:" << pkg->id() << " from " << catalog->id() <<
64                 "(" << catalog->description() << ")" << endl;
65         pkg->install();
66     } else if (!strcmp(argv[1], "uninstall") || !strcmp(argv[1], "remove")) {
67         pkg::Package* pkg = root->getPackageById(argv[2]);
68         if (!pkg) {
69             cerr << "unknown package:" << argv[2] << endl;
70             return EXIT_FAILURE;
71         }
72         
73         if (!pkg->isInstalled()) {
74             cerr << "package " << argv[2] << " not installed" << endl;
75             return EXIT_FAILURE;
76         }
77         
78         cout << "Will uninstall:" << pkg->id() << endl;
79         pkg->install()->uninstall();
80     } else if (!strcmp(argv[1], "update-all")) {
81         pkg::PackageList updates = root->packagesNeedingUpdate();
82         BOOST_FOREACH(pkg::Package* p, updates) {
83             root->scheduleToUpdate(p->install());
84         }
85     } else if (!strcmp(argv[1], "list-updated")) {
86         pkg::PackageList updates = root->packagesNeedingUpdate();
87         if (updates.empty()) {
88             cout << "no packages with updates" << endl;
89             return EXIT_SUCCESS;
90         }
91         
92         cout << updates.size() << " packages have updates" << endl;
93         BOOST_FOREACH(pkg::Package* p, updates) {
94             cout << "\t" << p->id() << " " << p->getLocalisedProp("name") << endl;
95         }
96     } else {
97         cerr << "unknown command:" << argv[1] << endl;
98         return EXIT_FAILURE;
99     }
100     
101     while (http->hasActiveRequests()) {
102         http->update();
103     }
104
105     return EXIT_SUCCESS;
106 }