1 // Copyright (C) 2013 James Turner - zakalawe@mac.com
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.
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.
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.
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>
25 #include <boost/foreach.hpp>
29 using namespace simgear;
32 bool keepRunning = true;
34 class MyDelegate : public pkg::Delegate
37 virtual void refreshComplete()
41 virtual void failedRefresh(pkg::Catalog* aCatalog, FailureCode aReason)
43 cerr << "failed refresh of " << aCatalog->description() << ":" << aReason << endl;
46 virtual void startInstall(pkg::Install* aInstall)
49 cout << "starting install of " << aInstall->package()->description() << endl;
52 virtual void installProgress(pkg::Install* aInstall, unsigned int bytes, unsigned int total)
54 unsigned int percent = (bytes * 100) / total;
55 if (percent == _lastPercent) {
59 _lastPercent = percent;
60 cout << percent << "%" << endl;
63 virtual void finishInstall(pkg::Install* aInstall)
65 cout << "done install of " << aInstall->package()->description() << endl;
68 virtual void failedInstall(pkg::Install* aInstall, FailureCode aReason)
70 cerr << "failed install of " << aInstall->package()->description() << endl;
73 unsigned int _lastPercent;
77 int main(int argc, char** argv)
80 HTTP::Client* http = new HTTP::Client();
81 pkg::Root* root = new pkg::Root(Dir::current().path(), "");
84 root->setDelegate(&dlg);
86 cout << "Package root is:" << Dir::current().path() << endl;
87 cout << "have " << pkg::Catalog::allCatalogs().size() << " catalog(s)" << endl;
89 root->setHTTPClient(http);
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")) {
96 } else if (!strcmp(argv[1], "install")) {
97 pkg::Package* pkg = root->getPackageById(argv[2]);
99 cerr << "unknown package:" << argv[2] << endl;
103 if (pkg->isInstalled()) {
104 cout << "package " << pkg->id() << " is already installed at " << pkg->install()->path() << endl;
108 pkg::Catalog* catalog = pkg->catalog();
109 cout << "Will install:" << pkg->id() << " from " << catalog->id() <<
110 "(" << catalog->description() << ")" << endl;
112 } else if (!strcmp(argv[1], "uninstall") || !strcmp(argv[1], "remove")) {
113 pkg::Package* pkg = root->getPackageById(argv[2]);
115 cerr << "unknown package:" << argv[2] << endl;
119 if (!pkg->isInstalled()) {
120 cerr << "package " << argv[2] << " not installed" << endl;
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());
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;
138 cout << updates.size() << " packages have updates" << endl;
139 BOOST_FOREACH(pkg::Package* p, updates) {
140 cout << "\t" << p->id() << " " << p->getLocalisedProp("name") << endl;
143 cerr << "unknown command:" << argv[1] << endl;
147 while (http->hasActiveRequests()) {