Updated Turkish translation
[navit-package] / navit / start.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <getopt.h>
24 #include "config.h"
25 #include "version.h"
26 #include "item.h"
27 #include "coord.h"
28 #include "main.h"
29 #include "route.h"
30 #include "navigation.h"
31 #include "track.h"
32 #include "debug.h"
33 #include "event.h"
34 #include "event_glib.h"
35 #include "xmlconfig.h"
36 #include "file.h"
37 #include "search.h"
38 #include "navit_nls.h"
39 #include "atom.h"
40 #ifdef HAVE_API_WIN32_CE
41 #include <windows.h>
42 #include <winbase.h>
43 #endif
44
45 char *version=PACKAGE_VERSION" "SVN_VERSION""NAVIT_VARIANT; 
46
47 static void
48 print_usage(void)
49 {
50         printf(_("navit usage:\nnavit [options] [configfile]\n\t-c <file>: use <file> as config file\n\t-d <n>: set the debug output level to <n>. (TODO)\n\t-h: print this usage info and exit.\n\t-v: Print the version and exit.\n"));
51 }
52
53
54 static gchar *
55 get_home_directory(void)
56 {
57         static gchar *homedir = NULL;
58
59         if (homedir) return homedir;
60         homedir = getenv("HOME");
61         if (!homedir)
62         {
63                 dbg(0,"Could not find home directory. Using current directory as home directory.\n");
64                 homedir = ".";
65         } else {
66                 homedir=g_strdup(homedir);
67         }
68         return homedir;
69 }
70
71 int main(int argc, char **argv)
72 {
73         xmlerror *error = NULL;
74         char *config_file = NULL;
75         int opt;
76         char *cp;
77
78         GList *list = NULL, *li;
79
80
81 #ifdef HAVE_GLIB
82         event_glib_init();
83 #endif
84         atom_init();
85         main_init(argv[0]);
86         main_init_nls();
87         debug_init(argv[0]);
88         
89         cp = getenv("NAVIT_LOGFILE");
90         if (cp)
91                 debug_set_logfile(cp);
92 #ifdef HAVE_API_WIN32_CE
93         else
94                 debug_set_logfile("/Storage Card/navit.log");
95 #endif
96         file_init();
97 #ifndef USE_PLUGINS
98         extern void builtin_init(void);
99         builtin_init();
100 #endif
101         route_init();
102         navigation_init();
103         tracking_init();
104         search_init();
105         linguistics_init();
106         config_file=NULL;
107         opterr=0;  //don't bomb out on errors.
108         if (argc > 1) {
109                 /* DEVELOPPERS : don't forget to update the manpage if you modify theses options */
110                 while((opt = getopt(argc, argv, ":hvc:d:")) != -1) {
111                         switch(opt) {
112                         case 'h':
113                                 print_usage();
114                                 exit(0);
115                                 break;
116                         case 'v':
117                                 printf("%s %s\n", "navit", version); 
118                                 exit(0);
119                                 break;
120                         case 'c':
121                                 printf("config file n is set to `%s'\n", optarg);
122                     config_file = optarg;
123                                 break;
124                         case 'd':
125                                 printf("TODO Verbose option is set to `%s'\n", optarg);
126                                 break;
127                         case ':':
128                                 fprintf(stderr, "navit: Error - Option `%c' needs a value\n", optopt);
129                                 print_usage();
130                                 exit(1);
131                                 break;
132                         case '?':
133                                 fprintf(stderr, "navit: Error - No such option: `%c'\n", optopt);
134                                 print_usage();
135                                 exit(1);
136                         }
137             }
138         }
139         // use 1st cmd line option that is left for the config file
140         if (optind < argc) config_file = argv[optind];
141
142     // if config file is explicitely given only look for it, otherwise try std paths
143         if (config_file) list = g_list_append(list,g_strdup(config_file));
144     else {
145                 list = g_list_append(list,g_strjoin(NULL,get_home_directory(), "/.navit/navit.xml" , NULL));
146                 list = g_list_append(list,g_strdup("navit.xml.local"));
147                 list = g_list_append(list,g_strdup("navit.xml"));
148                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml.local" , NULL));
149                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml" , NULL));
150                 #ifndef _WIN32
151                 list = g_list_append(list,g_strdup("/etc/navit/navit.xml"));
152                 #endif
153         }
154         li = list;
155         for (;;) {
156                 if (li == NULL) {
157                         // We have not found an existing config file from all possibilities
158                         printf(_("No config file navit.xml, navit.xml.local found\n"));
159                         exit(1);
160                 }
161         // Try the next config file possibility from the list
162                 config_file = li->data;
163                 if (file_exists(config_file))
164                         break;
165                 else
166                         g_free(config_file);
167                 li = g_list_next(li);
168         }
169
170         if (!config_load(config_file, &error)) {
171                 dbg(0, _("Error parsing '%s': %s\n"), config_file, error ? error->message : "");
172         } else {
173                 dbg(0, _("Using '%s'\n"), config_file);
174         }
175         while (li) {
176                 g_free(li->data);
177                 li = g_list_next(li);
178         }
179         g_list_free(list);
180         if (! main_get_navit(NULL)) {
181                 dbg(0, _("No instance has been created, exiting\n"));
182                 exit(1);
183         }
184         event_main_loop_run();
185
186         return 0;
187 }