GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gem/main.c
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * main.c
4  *
5  * Mon Apr 18 15:19:04 2005
6  * Copyright 2005 Benjamin Ducke
7  ****************************************************************************/
8 
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 Library 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, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 
26 /* TODO:
27 
28  for 1.04:
29 
30  EASY STUFF:
31 
32 
33  - tried to install GeneralStatistics w/o first installing RasterTools or anything else: attempts to install, fails because
34  of missing raster tools headers! Even though line 379 in reg_deps.c should check for this case !!!
35 
36  - include $GISBASE/lib in linker path for compilation of extensions [NOT FIXED: see lines preceeding INSTALL action case: 693
37  -L is added to command line, but problems still exist]
38  MAYBE EXPORT LD_LIBRARY_PATH ??? (only add $GISBASE/lib if it does not already exist)
39 
40  - version number in HTML documentation index seems to not get updated when installing a newer version of an extension
41 
42  - remember to update version number in globals.h
43 
44 
45  for 1.2 (GRASS 6.4 ?):
46  - make GRASS store its ./configure command line options in a file in the GISBASE/etc directory, so that
47  it will be possible for GEM to automatically configure extensions according to the system setup
48  [this means that configure file needs to be kept in sync with GEM; --configure option can be used
49  to overwrite this behaviour]
50  - configure script should not fail but disable options and create config.msgs the
51  contents of this should be displayed and deleted afterwards
52  - check if it works with this Mac version of GRASS, as well: http://openosx.com/grass/
53  - before release: any problems expected with native Win32-Kompilation?:
54  - location for tmp-files?
55  - a simple wrapper g.install with GRASS style parameters. Should start an xterm and ask for su pwd,
56  if necessary
57  - action to check dependencies of installed extensions
58  - instead of aborting on each failed dependency: build list of all failed
59  dependencies before aborting
60  - mechanism to recursively download and install missing deps from a
61  provided list of URLs
62  - instead of calling external tar, switch to tarlib and zlib
63  - action --validate for checking an extension archive/dir
64  - add link to HTML help index into each extension's submenu in GIS Manager
65  - g.install with a real custom-made GUI
66  [VARIABLE GUI must be set appropriately in run_post() for post script]
67  this should be an independent Tcl/Tk GUI: install/uninstall/query extensions
68  - extension database on the internet that can be queried and used with either gem
69  or g.install
70  - entries for QGIS
71  - simplified scheme for installing just a single module
72  - new scheme for registering menu entries in GIS Manager with stable hooks in menu.tcl
73 
74 
75  NEED HELP
76  - GRASS' make install installs all files BENEATH top level dir with UID
77  set to benni !!! Is this intentional?
78  - provide gem6x as link in /usr/local/bin as part of grass 6.x base install
79  - description.html should not contain </body> or </html> as Rules.Make seems
80  to append those ?
81  - source install copies files COPYING README REQUIREMENTS.html to somewhere (but where?)
82  -> into <extension>/src but where else ?
83  - menu.tcl in GRASS 6.0.0 has "all options 1", but 6.1 will have "all options $tmenu".
84  How to cater for such things? Maybe in the future there should always be an
85  Xtns menu provided by GIS Manager and always delimited by reliable tags
86  - d.m window is a bit to small horizontally
87 
88  CAVEATS:
89  - does not allow installation of different versions of the same extension within
90  the same GRASS bin tree
91  - does not have a real upgrading mechanism: option --upgrade has been disabled for now
92  - restore cannot fix partially corrupted entries in HTML and TCL files
93  - Links to other extension's modules will only work if those extensions are installed
94 
95  DOCS:
96 
97  - skeleton contains GPL as default license. Creators of new extensions
98  need to be aware of this!
99 
100  - menu.tcl will be backed up as menu.tcl.gem.bak
101 
102  - HTML docs: users must prefix references to GRASS modules outside the Extension with
103  "../../html/" !!!
104  - description files: things inside "<" and ">" will be filtered out as HTML tags, even
105  if they are none!
106  - make clear that there are some files which will be rendered both as text and HTML
107  and therefore need things like <br> while others DO NOT (which ones?)!
108 
109  - configure script should not fail but disable options and create 'config.msgs'
110  - deletes config.msg after display
111 
112  - State clearly that it is recommended to uninstall an older
113  extension version before installing a new one. STATE CLEARLY that --upgrade is rather
114  another version of --force than a real updating mechanism (RENAME to --newer)
115  - document all env vars that gem sets
116  - STATE CLEARLY, that users should not meddle with stuff in index.html, menu.tcl,
117  gem-entries/ and docs/extensions/ !!!
118  - provide a unix man page, ASCII and HTML documentation (write HTML and convert)
119  - files in the skeleton should always be present, even if they do not containing
120  information
121  - make it clear, that uninstall and postinstall are run with su privileges! thus,
122  author must make sure, that only objects in the GRASS install tree are affected
123  - user may specify either file name or extension name for uninstall action
124  - user may list installed extensions by using -q w/o filename. Explain
125  what is meant by type (e.g. 'src')
126  - uninstallation works only by extension name, NOT file name !
127  - compiling extensions on a GRASS install with all but the most basic options
128  disabled should give very portable binaries! (statically linked binaries?)
129 
130  BUGS:
131  - remove cva, install again: WARNING: list item 'cva' exists in index.html (?)
132  - superfluous warning upon uninstall of extension w/o "entries-gisman"
133  - due to bad command line parsing, listing installed extensions only works like this:
134  ./gem64 --grass=/usr/local/grass-6.4.svn -q
135  NOT like this:
136  ./gem64 -q --grass=/usr/local/grass-6.4.svn
137 
138 
139  */
140 
141 #include <getopt.h>
142 #include <fcntl.h>
143 
144 #define LOCAL
145 #include "globals.h"
146 
147 
148 void show_help(void)
149 {
150  fprintf(stdout, "Usage: gem64 [OPTION] [ACTION] [FILE|DIR]\n");
151  fprintf(stdout, "Install a GRASS extension from FILE or DIR.\n");
152  fprintf(stdout, "Manage (installed) GRASS extension(s).\n");
153  fprintf(stdout, "\nPossible ACTIONs are:\n");
154  fprintf(stdout, " -i, --install=EXT\tinstall a GRASS extension\n");
155  fprintf(stdout,
156  " -u, --uninstall=EXT\tremove an extension from GRASS\n");
157  fprintf(stdout,
158  " -q, --query=EXT\tdisplay information about extension/list installed\n");
159  fprintf(stdout,
160  " -d, --details=EXT\tdisplay additional details about an extension\n");
161  fprintf(stdout,
162  " -c, --clean=EXT\tclean extension's source code directories\n");
163  fprintf(stdout,
164  " -t, --test=EXT\tconfigure and compile extension, but don't install\n");
165  fprintf(stdout,
166  " -l, --license=EXT\tshow copyright information for an extension\n");
167  fprintf(stdout,
168  " -r, --restore\t\trecreate HTML links and GIS Manager entries\n");
169  fprintf(stdout, " -h, --help\t\tdisplay this help and exit\n");
170  fprintf(stdout,
171  " -V, --version\t\toutput version information and exit\n\n");
172  fprintf(stdout, "\nPossible OPTIONs are:\n");
173  fprintf(stdout, " -g, --grass=PATH\tpath to GRASS installation dir\n");
174  fprintf(stdout,
175  " -b, --binary=NAME\tno compilation: use binary files for system NAME\n");
176  fprintf(stdout,
177  " -f, --force\t\tforce action, regardless of dependencies\n");
178  fprintf(stdout,
179  " -v, --verbose\t\tdisplay detailed status information\n");
180  fprintf(stdout, " -s, --skip-config\tskip configure script\n");
181  fprintf(stdout,
182  " -x, --config-opts=OPTS\tpass OPTS to configure script\n");
183  fprintf(stdout,
184  " -o, --options=OPTS\toptions to pass to the C compiler/linker\n");
185  fprintf(stdout,
186  " -C, --config-cmd=CMD\tDefine custom 'configure' command (default=configure)\n");
187  fprintf(stdout,
188  " -m, --make-cmd=CMD\tDefine custom 'make' command (default=make)\n");
189  fprintf(stdout,
190  "\nWhen run from within a GRASS session, locations of libs, header files\n");
191  fprintf(stdout,
192  "and installation target dir will be assumed to match those of the active\n");
193  fprintf(stdout, "GRASS version. ");
194  fprintf(stdout,
195  "Option -g can be used to override these or install extensions\nfrom outside");
196  fprintf(stdout, "of a GRASS session.\n");
197  fprintf(stdout,
198  "Per default, extensions will be compiled from source and then installed.\n");
199  fprintf(stdout,
200  "If the exension package contains binaries for the user's platform, they can\n");
201  fprintf(stdout, "be installed instead using the -b option. ");
202  fprintf(stdout,
203  "For installation from source code, a C compiler and make tools are needed.\n");
204  fprintf(stdout, "\nExample:\n");
205  fprintf(stdout,
206  "\tgem64 -b macosx --grass=/usr/local/grass-6.4.0 -i myExtension\n");
207  fprintf(stdout,
208  "Installs the MacOS X binaries for 'myExtension' in /usr/local/grass-6.4.0.\n");
209  exit(0);
210 }
211 
212 
213 void show_details(char *package)
214 {
215  int error;
216  char tmp[MAXSTR];
217 
218  sprintf(tmp, "%s", basename(package));
219  error = chdir(tmp);
220  if (error < 0) {
222  "extension '%s' not accessible: (%s)\n", package,
223  strerror(errno));
224  }
225 
226  dump_ascii("info", "Detailed information");
227 
228 
229  /* run post action script */
230  system("sh post");
231 
232  exit(0);
233 }
234 
235 
236 void show_license(char *package)
237 {
238  int error;
239  char tmp[MAXSTR];
240 
241  sprintf(tmp, "%s", basename(package));
242  error = chdir(tmp);
243  if (error < 0) {
245  "extension '%s' not accessible: (%s)\n", package,
246  strerror(errno));
247  }
248 
249  dump_ascii("license", "Detailed information");
250 
251  /* run post action script */
252  system("sh post");
253 
254  exit(0);
255 }
256 
257 
258 void show_version(void)
259 {
260  fprintf(stdout, "gem64 (GRASS extensions manager) %.2f\n", PROGVERSION);
261  fprintf(stdout, "Written by Benjamin Ducke\n");
262  fprintf(stdout, "\nCopyright (C) 2005 Benjamin Ducke\n");
263  fprintf(stdout,
264  "This is free software; see the source for copying conditions. There is NO\n");
265  fprintf(stdout,
266  "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
267  exit(0);
268 }
269 
270 
271 /* determine options to pass to extension's configure script */
272 /* TODO: check, if system configuration meets a set of requirements */
273 /* THIS FUNCTION IS CURRENTLY NOT USED */
275 {
276 
277  FILE *fp;
278  char str[MAXSTR];
279 
280  if (strcmp(CONFIG_OPTS, "")) {
281  /* if user has specified config options on the GEM command line: override anything else */
282  return;
283  }
284 
285  /* check if GISBASE/etc/config.system exists and if so, read options from it */
286  sprintf(str, "%s/etc/config.system", gisbase);
287  fp = fopen(str, "r");
288  if (fp == NULL) {
290  ("could not open %s for read access. Using default configure options.\n",
291  str);
292  return;
293  }
294 
295  /* config.system may also contain nothing, only comments and/or whitespace */
296  if (nc_fgets_nb(str, MAXSTR, fp) != NULL) {
297  strcpy(CONFIG_OPTS, str);
298  }
299 }
300 
301 
302 int main(int argc, char *argv[])
303 {
304  char *gisbase;
305  char *grass_version;
306  char *grass_major;
307  char *grass_minor;
308  char *grass_revision;
309  char *tmp;
310  char *url;
311  char *filepart;
312  char version_file[MAXSTR];
313  char package[MAXSTR];
314  char orgname[MAXSTR];
315  char pkg_name[MAXSTR];
316  char *bins;
317  int pkg_major, pkg_minor, pkg_revision;
318  FILE *f;
319  int fd;
320 
321  char pkg_short_name[MAXSTR];
322  char invocation[MAXSTR];
323 
324  char coptions[MAXSTR];
325 
326  int major, minor, revision;
327  int option;
328  int action;
329  int valid;
330 
331  struct stat buf;
332  struct stat buf2;
333  int error;
334  int is_directory = 0;
335  DIR *dir;
336  struct dirent *dir_entry;
337  int dir_found;
338 
339  int option_index = 0;
340  static struct option long_options[] = {
341  {"install", 1, NULL, 'i'},
342  {"uninstall", 1, NULL, 'u'},
343  {"query", 2, NULL, 'q'},
344  {"details", 1, NULL, 'd'},
345  {"clean", 1, NULL, 'c'},
346  {"test", 1, NULL, 't'},
347  {"license", 1, NULL, 'l'},
348  {"restore", 0, NULL, 'r'},
349  {"help", 0, NULL, 'h'},
350  {"version", 0, NULL, 'V'},
351 
352  {"grass", 1, NULL, 'g'},
353  {"options", 1, NULL, 'o'},
354  {"binary", 1, NULL, 'b'},
355  {"force", 0, NULL, 'f'},
356  {"verbose", 0, NULL, 'v'},
357  {"skip-config", 0, NULL, 's'},
358  {"config-opts", 1, NULL, 'x'},
359 
360  {"config-cmd", 1, NULL, 'C'},
361  {"make-cmd", 1, NULL, 'm'},
362 
363  {0, 0, 0, 0}
364  };
365 
366 
367  /* set global variables to defaults */
368  VERBOSE = 0;
369  TMPCLEAN = 0;
370  TMPDBCLEAN = 0;
371  FORCE = 0;
372  UPGRADE = 0;
373  ERROR = 0;
374  WARNINGS = 0;
375  SKIP_CFG = 0;
376 
377  strcpy(GISMAN_CMD, "");
378  strcpy(GISMAN2_CMD, "");
379  strcpy(QGIS_CMD, "");
380  strcpy(UNINSTALL_CMD, "");
381  strcpy(HTML_CMD, "");
382 
383  strcpy(TMPDIR, "");
384  strcpy(TMPDB, "");
385  strcpy(TMP_GISMAN, "");
386  strcpy(TMP_DESCR, "");
387  strcpy(TMP_INFO, "");
388  strcpy(TMP_DEPS, "");
389  strcpy(TMP_BUGS, "");
390  strcpy(TMP_AUTHORS, "");
391  strcpy(TMP_HTML, "");
392  strcpy(TMP_NULL, "");
393 
394  strcpy(CONFIG_OPTS, "");
395 
396  strcpy(CONFIG_CMD, "configure");
397  strcpy(MAKE_CMD, "make");
398 
399  getcwd(CWD, MAXSTR);
400 
401  /* reset terminal colors */
402  fprintf(stdout, "\033[0m");
403 
404  tmp = malloc(sizeof(char) * MAXSTR);
405  strcpy(invocation, argv[0]);
406 
407  /* all output should be unbuffered */
408  setvbuf(stdout, (char *)NULL, _IONBF, 0);
409 
410  /* if run with no arguments: show help */
411  if (argc == 1) {
412  /* show usage info and exit */
413  show_help();
414  exit(0);
415  }
416 
417  atexit(&exit_msg); /* show a message after program termination */
418 
419  valid = 0;
420  bins = NULL;
421  gisbase = NULL;
422 
423  opterr = 0;
424  option =
425  getopt_long(argc, argv, ":i:u:q:d:c:C:t:l:m:o:x:rhVg:b:fvs",
426  long_options, &option_index);
427  while (option != -1) {
428 
429  if (option == '?') {
431  "unknown option or action specified.\n");
432  }
433 
434 
435  /* check for missing arguments */
436  if (option == ':') {
437  if ((optopt == 'i') || (optopt == 'u') || (optopt == 'd') ||
438  (optopt == 'c') || (optopt == 't') || (optopt == 'l') ||
439  (optopt == 'r')) {
441  "missing file or directory name.\n");
442  }
443  if (optopt == 'g') {
444  print_error(ERR_INVOCATION, "missing path to GRASS 6.\n");
445  }
446  if (optopt == 'b') {
448  "missing name of binary architecture.\n");
449  }
450  if (optopt == 'b') {
451  print_error(ERR_INVOCATION, "missing configure options.\n");
452  }
453  if (optopt == 'q') {
454  /* '-q' w/o filename is list action */
455  action = LIST;
456  valid++;
457  break;
458  }
459  }
460 
461  if ((option == 'i') || (option == 'u') || (option == 'q') ||
462  (option == 'd') || (option == 'c') || (option == 't') ||
463  (option == 'l') || (option == 'r') || (option == 'h') ||
464  (option == 'V')) {
465  /* got a valid action specifier */
466  valid++;
467  /* set action accordingly */
468  switch (option) {
469  case 'i':
470  if (action != BIN_INSTALL) {
471  action = INSTALL;
472  }
473  break;
474  case 'u':
475  action = UNINSTALL;
476  break;
477  case 'q':
478  action = QUERY;
479  break;
480  case 'd':
481  action = DETAILS;
482  break;
483  case 'c':
484  action = CLEAN;
485  break;
486  case 't':
487  action = TEST_INSTALL;
488  break;
489  case 'l':
490  action = LICENSE;
491  break;
492  case 'r':
493  action = RESTORE;
494  break;
495  case 'h':
496  action = HELP;
497  break;
498  case 'V':
499  action = VERSION;
500  break;
501  }
502  if (optarg != NULL) {
503  /* save package name as given on command line */
504  strcpy(package, optarg);
505  /* orgname will always preserve the commandline option */
506  strcpy(orgname, optarg);
507  }
508  }
509 
510  /* set options */
511  if (option == 'g') {
512  gisbase = malloc(sizeof(char) * (strlen(optarg) + 1));
513  strcpy(gisbase, optarg);
514  }
515  if (option == 'b') {
516  bins = malloc(sizeof(char) * (strlen(optarg) + 1));
517  strcpy(bins, optarg);
518  action = BIN_INSTALL;
519  }
520  if (option == 'x') {
521  /* configure script options */
522  strcpy(&CONFIG_OPTS[0], optarg);
523  }
524 
525  if (option == 'f') {
526  FORCE = 1;
527  }
528  if (option == 'v') {
529  VERBOSE = 1;
530  }
531 
532  if (option == 's') {
533  SKIP_CFG = 1;
534  }
535  if (option == 'o') {
536  /* GEM_C_OPTS gets passed to the C compiler via the GRASS/GEM Makefiles:
537  <EXT>/src/include/Make/Grass.make.in:
538  CFLAGS = $(INC) $(COMPILE_FLAGS) $(USE_TERMIO) $(GEM_C_OPTS)
539  */
540  strcat(coptions, optarg);
541  }
542  /* define a custom configure command */
543  if (option == 'C') {
544  strcpy(CONFIG_CMD, optarg);
545  }
546  /* define a custom make command */
547  if (option == 'm') {
548  strcpy(MAKE_CMD, optarg);
549  }
550  /* get next option from command line */
551  option =
552  getopt_long(argc, argv, ":i:u:q:d:c:t:l:o:x:rhVg:b:fvs",
553  long_options, &option_index);
554  }
555 
556 
557  if (valid < 1) {
558  print_error(ERR_INVOCATION, "please specify a valid action.\n");
559  }
560  if (valid > 1) {
561  print_error(ERR_INVOCATION, "please specify only one action.\n");
562  }
563 
564  /* export compiler options for use by Makefiles */
565  sprintf(GEM_C_OPTS, "GEM_C_OPTS=%s", coptions);
566  putenv(GEM_C_OPTS);
567 
568  /* these actions can be done without any extension checking */
569  if (action == HELP) {
570  /* show usage info and exit */
571  show_help();
572  exit(0);
573  }
574 
575  if (action == VERSION) {
576  /* show version info and exit */
577  show_version();
578  exit(0);
579  }
580 
581  if (!VERBOSE) {
582  /* set temp file to pipe output to for silent operation */
583  /* TODO: Do not hardcode temp paths */
584  strcpy(TMP_NULL, "/tmp/grass.extension.log.XXXXXX"); /* TMP_NULL is a global variable */
585  mkstemp(TMP_NULL);
586  fd = open(TMP_NULL, O_CREAT, 0777);
587  if (fd == -1) {
588  print_error(ERR_TMPFILE, "could not create temp file: %s",
589  strerror(errno));
590  exit(ERR_TMPFILE);
591  }
592  }
593 
594 
595  /* these actions need a valid GRASS path but no extensions */
596  if (action == RESTORE) {
597  /* figure out path to GRASS installation */
598  /* GIS base not given? */
599  if (gisbase == NULL) {
600  /* try to read from GRASS environment */
601  gisbase = getenv("GISBASE");
602  if (gisbase == NULL) {
603  /* still NULL? Abort! */
605  "GISBASE environment variable not set and path to GRASS not given.\n");
606  }
607  }
608 
609  if (VERBOSE) {
610  fprintf(stdout, "Path to GRASS is %s.\n", gisbase);
611  }
612 
613  restore(gisbase, grass_version);
614  exit(0);
615  }
616 
617  if (action == LIST) {
618  /* figure out path to GRASS installation */
619  /* GIS base not given? */
620  if (gisbase == NULL) {
621  /* try to read from GRASS environment */
622  gisbase = getenv("GISBASE");
623  if (gisbase == NULL) {
624  /* still NULL? Abort! */
626  "GISBASE environment variable not set and path to GRASS not given.\n");
627  }
628  }
629 
630  if (VERBOSE) {
631  fprintf(stdout, "Path to GRASS is %s.\n", gisbase);
632  }
633  list_extensions(gisbase);
634  exit(0);
635  }
636 
637  /* check if extension is stored in a remote URL */
638  if ((strstr(package, "http://")) || (strstr(package, "ftp://"))) {
639  wget_extension(package); /* download into current dir using wget */
640  /* cut off the path specification */
641  url = malloc(sizeof(char) * MAXSTR);
642  strcpy(url, package);
643  filepart = strrchr(url, '/');
644  filepart++;
645  strcpy(package, filepart);
646  free(url);
647  }
648 
649  if (VERBOSE) {
650  fprintf(stdout, "Extension location is '%s'.\n", package);
651  }
652 
653  if (action != UNINSTALL) {
654  error = stat(package, &buf);
655  if (error < 0) {
657  "extension FILE or DIR '%s' invalid: %s\n", package,
658  strerror(errno));
659  }
660  if (S_ISDIR(buf.st_mode)) {
661  is_directory = 1;
662  if (VERBOSE) {
663  fprintf(stdout, "Extension files stored in a directory.\n");
664  }
665  }
666  else {
667  if (VERBOSE) {
668  fprintf(stdout,
669  "Extension files stored in a package file.\n");
670  }
671  /* DECOMPRESS INTO TEMP DIR, CHANGE NAME OF package TO THAT DIR */
672  unpack_extension(package);
673  /* find name of directory containing extension files */
674  /* very primitive: just picks the first directory */
675  dir = opendir(TMPDIR);
676  dir_entry = readdir(dir);
677  dir_found = 0;
678  while (dir_entry != NULL) {
679  if ((strcmp(dir_entry->d_name, ".")) &&
680  (strcmp(dir_entry->d_name, ".."))
681  ) {
682  /* check if it is a directory */
683  sprintf(tmp, "%s/%s", TMPDIR, dir_entry->d_name);
684  stat(tmp, &buf2);
685  if (S_ISDIR(buf2.st_mode)) {
686  dir_found = 1;
687  break;
688  }
689  }
690  dir_entry = readdir(dir);
691  }
692  strcpy(package, tmp);
693 
694  if (dir_found == 0) {
696  "no top-level directory found in extension package.\n");
697  }
698  }
699  }
700 
701  /* copy package name into this maliciously named variable */
702  /* (sorry about the mess ...) */
703  /* This name will be used for all registration actions and */
704  /* for creating files and directories that store extension */
705  /* information for uninstall and restore actions */
706  /* For uninstall, we take the last argument as package, NOT */
707  /* file name */
708  if (action == UNINSTALL) {
709  strcpy(pkg_short_name, package);
710  }
711  else {
712  get_package_name(package, pkg_short_name);
713  }
714 
715  /* export relevant VARS for use by post script */
716  if (valid > 0) {
717  /* export all relevant env vars for the post script */
718  if (gisbase == NULL) {
719  /* try to read from GRASS environment */
720  gisbase = getenv("GISBASE");
721  }
722  run_post(package, action, bins, gisbase);
723  }
724 
725  if (VERBOSE) {
726  fprintf(stdout, "Extension will be installed from '%s'\n", package);
727  }
728 
729  /* CHECK PACKAGE FOR VALIDITY */
730  if (action != UNINSTALL) {
731  check_extension(package, pkg_name, &pkg_major, &pkg_minor,
732  &pkg_revision);
733  }
734 
735  /* these actions can be done without GRASS checking */
736  if (action == QUERY) {
737  query_extension(package, pkg_name, pkg_major, pkg_minor, pkg_revision,
738  pkg_short_name, invocation, orgname);
739  exit(0);
740  }
741 
742  if (action == DETAILS) {
743  show_details(package);
744  exit(0);
745  }
746 
747  if (action == LICENSE) {
748  show_license(package);
749  exit(0);
750  }
751 
752  if (action == CLEAN) {
753  source_clean(package);
754  exit(0);
755  }
756 
757 
758  /* The following checks need to be done for all other actions! */
759 
760  /* figure out path to GRASS installation */
761  /* GIS base not given? */
762  if (gisbase == NULL) {
763  /* try to read from GRASS environment */
764  gisbase = getenv("GISBASE");
765  if (gisbase == NULL) {
766  /* still NULL? Abort! */
768  "GISBASE environment variable not set and path to GRASS not given.\n");
769  }
770  }
771 
772  if (VERBOSE) {
773  fprintf(stdout, "Path to GRASS is %s.\n", gisbase);
774  }
775 
776  /* figure out GRASS version */
777  grass_version = getenv("GRASS_VERSION");
778  if (grass_version == NULL) {
779  /* GRASS version can be read from gisbase/etc/VERSIONNUMBER */
780  sprintf(version_file, "%s/etc/VERSIONNUMBER", gisbase);
781  f = fopen(version_file, "r");
782  if (f == NULL) {
783  /* still NULL? Abort! */
785  "Could not read GRASS version. Did you specify the right path?\n");
786  }
787  else {
788  grass_version = malloc(sizeof(char) * 16);
789  error = fscanf(f, "%s", grass_version);
790  fclose(f);
791  if (error < 1) {
793  "Could not read GRASS version. Did you specify the right path?\n");
794  }
795  }
796  }
797 
798  if (grass_version != NULL) {
799  /* extract major and minor version numbers */
800  tmp = strdup(grass_version);
801 
802  grass_major = strtok(tmp, ".");
803  grass_minor = strtok(NULL, ".");
804  grass_revision = strtok(NULL, ".");
805 
806  major = strtol(grass_major, NULL, 10);
807  minor = strtol(grass_minor, NULL, 10);
808  revision = strtol(grass_revision, NULL, 10);
809 
810  grass_version = malloc(sizeof(char) * MAXSTR);
811  sprintf(grass_version, "%i.%i.%i", major, minor, revision);
812  if (VERBOSE) {
813  fprintf(stdout, "GRASS version is %s.\n", grass_version);
814  }
815 
816  if (major < 6) {
818  "extensions only work with GRASS version 6 and above.\n");
819  }
820  }
821 
822  /* for GDAL compatibility and
823  for the sake of people using Lorenzo Moretti's GRASS for MacOS, we always export the following
824  compiler options */
825  sprintf(coptions, "-L%s/lib -I/usr/local/grasslib/include/ ", gisbase);
826 
827  /* these actions can only be done after everything has been checked */
828  if (action == INSTALL) {
829  source_install(package, gisbase, pkg_short_name, pkg_major, pkg_minor,
830  pkg_revision, grass_version);
831  exit(0);
832  }
833 
834  if (action == UNINSTALL) {
835  uninstall(package, pkg_short_name, gisbase, grass_version);
836  exit(0);
837  }
838 
839  if (action == TEST_INSTALL) {
840  test_install(package, gisbase, pkg_short_name, pkg_major, pkg_minor,
841  pkg_revision, grass_version);
842  exit(0);
843  }
844 
845  if (action == BIN_INSTALL) {
846  if (binaries_exist(package, bins)) {
847  bin_install(package, gisbase, bins, pkg_short_name, pkg_major,
848  pkg_minor, pkg_revision, grass_version);
849  exit(0);
850  }
851  else {
852  print_error(ERR_MISSING_BINS, "no binaries for system '%s'\n",
853  bins);
854  }
855  }
856 
857  exit(0);
858 }