]> git.mxchange.org Git - simgear.git/blob - simgear/package/pkgutil.cxx
Lots more work on package support.
[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 class MyDelegate : public pkg::Delegate
35 {
36 public:
37     virtual void refreshComplete()
38     {
39     }
40     
41     virtual void failedRefresh(pkg::Catalog* aCatalog, FailureCode aReason)
42     {
43         cerr << "failed refresh of " << aCatalog->description() << ":" << aReason << endl;
44     }
45     
46     virtual void startInstall(pkg::Install* aInstall)
47     {
48         _lastPercent = 999;
49         cout << "starting install of " << aInstall->package()->description() << endl;
50     }
51     
52     virtual void installProgress(pkg::Install* aInstall, unsigned int bytes, unsigned int total)
53     {
54         unsigned int percent = (bytes * 100) / total;
55         if (percent == _lastPercent) {
56             return;
57         }
58         
59         _lastPercent = percent;
60         cout << percent << "%" << endl;
61     }
62     
63     virtual void finishInstall(pkg::Install* aInstall)
64     {
65         cout << "done install of " << aInstall->package()->description() << endl;
66     }
67
68     virtual void failedInstall(pkg::Install* aInstall, FailureCode aReason)
69     {
70         cerr << "failed install of " << aInstall->package()->description() << endl;
71     }
72 private:
73     unsigned int _lastPercent;
74     
75 };
76
77 int main(int argc, char** argv)
78 {
79
80     HTTP::Client* http = new HTTP::Client();
81     pkg::Root* root = new pkg::Root(Dir::current().path(), "");
82     
83     MyDelegate dlg;
84     root->setDelegate(&dlg);
85     
86     cout << "Package root is:" << Dir::current().path() << endl;
87     cout << "have " << pkg::Catalog::allCatalogs().size() << " catalog(s)" << endl;
88         
89     root->setHTTPClient(http);
90     
91     if (!strcmp(argv[1], "add")) {
92         std::string url(argv[2]);
93         pkg::Catalog::createFromUrl(root, url);
94     } else if (!strcmp(argv[1], "refresh")) {
95         root->refresh();
96     } else if (!strcmp(argv[1], "install")) {
97         pkg::Package* pkg = root->getPackageById(argv[2]);
98         if (!pkg) {
99             cerr << "unknown package:" << argv[2] << endl;
100             return EXIT_FAILURE;
101         }
102         
103         if (pkg->isInstalled()) {
104             cout << "package " << pkg->id() << " is already installed at " << pkg->install()->path() << endl;
105             return EXIT_SUCCESS;
106         }
107         
108         pkg::Catalog* catalog = pkg->catalog();
109         cout << "Will install:" << pkg->id() << " from " << catalog->id() <<
110                 "(" << catalog->description() << ")" << endl;
111         pkg->install();
112     } else if (!strcmp(argv[1], "uninstall") || !strcmp(argv[1], "remove")) {
113         pkg::Package* pkg = root->getPackageById(argv[2]);
114         if (!pkg) {
115             cerr << "unknown package:" << argv[2] << endl;
116             return EXIT_FAILURE;
117         }
118         
119         if (!pkg->isInstalled()) {
120             cerr << "package " << argv[2] << " not installed" << endl;
121             return EXIT_FAILURE;
122         }
123         
124         cout << "Will uninstall:" << pkg->id() << endl;
125         pkg->install()->uninstall();
126     } else if (!strcmp(argv[1], "update-all")) {
127         pkg::PackageList updates = root->packagesNeedingUpdate();
128         BOOST_FOREACH(pkg::Package* p, updates) {
129             root->scheduleToUpdate(p->install());
130         }
131     } else if (!strcmp(argv[1], "list-updated")) {
132         pkg::PackageList updates = root->packagesNeedingUpdate();
133         if (updates.empty()) {
134             cout << "no packages with updates" << endl;
135             return EXIT_SUCCESS;
136         }
137         
138         cout << updates.size() << " packages have updates" << endl;
139         BOOST_FOREACH(pkg::Package* p, updates) {
140             cout << "\t" << p->id() << " " << p->getLocalisedProp("name") << endl;
141         }
142     } else {
143         cerr << "unknown command:" << argv[1] << endl;
144         return EXIT_FAILURE;
145     }
146     
147     while (http->hasActiveRequests()) {
148         http->update();
149     }
150
151     return EXIT_SUCCESS;
152 }