main.cpp
1 /*
2  name: tools/pkg/main.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <iostream>
30 #include <cstdlib>
31 #include <QCoreApplication>
32 #include <QStringList>
33 #include "arcspackagemanager.h"
34 
158 typedef enum {
159  NONE,
160  BUILD,
161  INSTALL,
162  INSTALL_ALL
163 } Mode;
164 
165 Mode currentMode = NONE;
166 bool storeSettings=false;
167 QString libName;
168 
169 void usage(ARCSPackageManager &apm, char* prgname)
170 {
171  std::cout << " Usage: " << prgname << " -b|-i packageName|-I|-h [-s] [-Dsetting=value]" << std::endl;
172  std::cout << " -h: prints this help message and current settings" << std::endl;
173  std::cout << " -b: build a repository" << std::endl;
174  std::cout << " -i: install packageName" << std::endl;
175  std::cout << " -I: install all packages from repository" << std::endl;
176  std::cout << " -s: store settings" << std::endl;
177  std::cout << " -D: defines a setting with a value" << std::endl;
178  apm.checkSettings();
179  apm.displaySettings();
180 }
181 
182 QString readNext(int& idx,int argc,char* argv[])
183 {
184  idx++;
185  if (idx < argc)
186  return argv[idx];
187  return QString::null;
188 }
189 
190 
191 void requestParameter(ARCSPackageManager &apm,QString paramName, QString message)
192 {
193  if (!apm.hasSetting(paramName))
194  {
195  std::cout << qPrintable(message); //"Please give a path for repository: ";
196  std::string rp;
197  std::cin >> rp;
198  apm.setSetting(paramName,rp.data());
199  }
200 }
201 
202 void parseOptions(ARCSPackageManager &apm, int argc, char* argv[])
203 {
204  int i=1;
205  QString next;
206 
207  for(i=1; i < argc; i++)
208  {
209  QString current(argv[i]);
210 
211  if (current == "-b")
212  {
213  currentMode = BUILD;
214  requestParameter(apm,"repositoryPath","Please give a path for repository: ");
215  }
216  else {
217  if (current == "-i")
218  {
219  if ((next = readNext(i,argc,argv)).isEmpty())
220  {
221  std::cout << "packageName not given" << std::endl << std::endl;
222  usage(apm,argv[0]);
223  return ;
224  }
225  currentMode = INSTALL;
226  libName = next;
227  requestParameter(apm,"repositoryUrl","Please give a url for repository: ");
228  }
229  else
230  {
231  if (current == "-h")
232  {
233  usage(apm,argv[0]);
234  return;
235  }
236  else
237  {
238  if (current == "-I")
239  {
240  currentMode = INSTALL_ALL;
241  requestParameter(apm,"repositoryUrl","Please give a url for repository: ");
242  requestParameter(apm,"repositoryFile","Please give a repository file name: ");
243  }
244  else
245  {
246  if (current == "-s")
247  {
248  storeSettings=true;
249  }
250  else
251  {
252  if (current.startsWith("-D"))
253  {
254  QString set=current.remove(0,2);
255  QStringList sp = set.split('=');
256  apm.setSetting(sp[0],sp[1]);
257  }
258  }
259  }
260  }
261  }
262  }
263  }
264 }
265 
266 
267 int main(int argc, char* argv[])
268 {
269  QCoreApplication app(argc,argv);
270  QCoreApplication::setApplicationName("ARCS");
271  QCoreApplication::setOrganizationName("UEVE - Ibisc - IRA2 Team");
272  QCoreApplication::setOrganizationDomain("ibisc.fr");
273 
274  ARCSPackageManager apm;
275  if (argc < 2)
276  {
277  usage(apm,argv[0]);
278  return 1;
279  }
280 
281  if (!apm.checkSettings())
282  return 2;
283 
284  parseOptions(apm,argc,argv);
285 
286  if (currentMode == BUILD)
287  apm.buildRepository();
288  if (currentMode == INSTALL)
289  apm.installLibrary(libName);
290  if (currentMode == INSTALL_ALL)
291  apm.installAll();
292 
293  if (storeSettings)
294  apm.storeSettings();
295 
296  return 0;
297 }