Option -n works again
[idzebra-moved-to-github.git] / index / main.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  *
5  * $Id: main.c,v 1.86 2002-04-15 13:48:10 adam Exp $
6  */
7 #include <stdio.h>
8 #include <string.h>
9 #include <assert.h>
10 #ifdef WIN32
11 #include <io.h>
12 #else
13 #include <unistd.h>
14 #endif
15
16 #include <yaz/data1.h>
17 #include "zebraapi.h"
18
19 char *prog;
20
21 int main (int argc, char **argv)
22 {
23     int ret;
24     int cmd = 0;
25     char *arg;
26     char *configName = 0;
27     int nsections = 0;
28     int disableCommit = 0;
29     size_t mem_max = 0;
30 #ifndef WIN32
31     char nbuf[100];
32 #endif
33     struct recordGroup rGroupDef;
34     ZebraService zs = 0;
35     ZebraHandle zh = 0;
36
37     nmem_init ();
38
39 #ifdef WIN32
40 #else
41     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
42     yaz_log_init_prefix (nbuf);
43 #endif
44
45     rGroupDef.groupName = NULL;
46     rGroupDef.databaseName = NULL;
47     rGroupDef.path = NULL;
48     rGroupDef.recordId = NULL;
49     rGroupDef.recordType = NULL;
50     rGroupDef.flagStoreData = -1;
51     rGroupDef.flagStoreKeys = -1;
52     rGroupDef.flagRw = 1;
53     rGroupDef.databaseNamePath = 0;
54     rGroupDef.explainDatabase = 0;
55     rGroupDef.fileVerboseLimit = 100000;
56
57     prog = *argv;
58     if (argc < 2)
59     {
60         fprintf (stderr, "%s [options] command <dir> ...\n"
61         "Commands:\n"
62         " update <dir>  Update index with files below <dir>.\n"
63         "               If <dir> is empty filenames are read from stdin.\n"
64         " delete <dir>  Delete index with files below <dir>.\n"
65         " commit        Commit changes\n"
66         " clean         Clean shadow files\n"
67         "Options:\n"
68         " -t <type>     Index files as <type> (grs or text).\n"
69         " -c <config>   Read configuration file <config>.\n"
70         " -g <group>    Index files according to group settings.\n"
71         " -d <database> Records belong to Z39.50 database <database>.\n"
72         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
73         " -n            Don't use shadow system.\n"
74         " -s            Show analysis on stdout, but do no work.\n"
75         " -v <level>    Set logging to <level>.\n"
76         " -l <file>     Write log to <file>.\n"
77         " -f <n>        Display information for the first <n> records.\n"
78         " -V            Show version.\n", *argv
79                  );
80         exit (1);
81     }
82     while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
83                            , argv, argc, &arg)) != -2)
84     {
85         if (ret == 0)
86         {
87             if(cmd == 0) /* command */
88             {
89                 if (!zs)
90                 {
91 #if ZMBOL
92                     logf (LOG_LOG, "Z'mbol version %s %s",
93                           ZEBRAVER, ZEBRADATE);
94 #else
95                     logf (LOG_LOG, "Zebra version %s %s",
96                           ZEBRAVER, ZEBRADATE);
97 #endif
98                     zs = zebra_start (configName ? configName : "zebra.cfg");
99                     if (!zs)
100                         exit (1);
101                     zh = zebra_open (zs);
102                     if (disableCommit)
103                         zebra_shadow_enable (zh, 0);
104                 }
105                 if (rGroupDef.databaseName)
106                 {
107                     if (zebra_select_database (zh, rGroupDef.databaseName))
108                         exit (1);
109                 }
110                 else
111                 {
112                     if (zebra_select_database (zh, "Default"))
113                         exit (1);
114                 }
115
116                 if (!strcmp (arg, "update"))
117                     cmd = 'u';
118                 else if (!strcmp (arg, "update1"))
119                     cmd = 'U';
120                 else if (!strcmp (arg, "update2"))
121                     cmd = 'm';
122                 else if (!strcmp (arg, "dump"))
123                     cmd = 's';
124                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
125                     cmd = 'd';
126                 else if (!strcmp (arg, "init"))
127                 {
128                     zebra_init (zh);
129                 }
130                 else if (!strcmp (arg, "commit"))
131                 {
132                     zebra_commit (zh);
133                 }
134                 else if (!strcmp (arg, "clean"))
135                 {
136                     assert (!"todo");
137                 }
138                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
139                 {
140                     assert (!"todo");
141                 }
142                 else if (!strcmp (arg, "compact"))
143                 {
144                     zebra_compact (zh);
145                 }
146                 else
147                 {
148                     logf (LOG_FATAL, "unknown command: %s", arg);
149                     exit (1);
150                 }
151             }
152             else
153             {
154                 rGroupDef.path = arg;
155                 zebra_set_group (zh, &rGroupDef);
156                 zebra_begin_trans (zh);
157
158                 switch (cmd)
159                 {
160                 case 'u':
161                     zebra_repository_update (zh);
162                     break;
163                 case 'd':
164                     zebra_repository_delete (zh);
165                     break;
166                 case 's':
167                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
168                     zebra_repository_show (zh);
169                     nsections = 0;
170                     break;
171                 default:
172                     nsections = 0;
173                 }
174                 cmd = 0;
175                 zebra_end_trans (zh);
176                 log_event_end (NULL, NULL);
177             }
178         }
179         else if (ret == 'V')
180         {
181 #if ZMBOL
182             fprintf (stderr, "Z'mbol %s %s\n", ZEBRAVER, ZEBRADATE);
183 #else
184             fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
185 #endif
186             fprintf (stderr, " (C) 1994-2002, Index Data ApS\n");
187 #ifdef WIN32
188 #ifdef _DEBUG
189             fprintf (stderr, " WIN32 Debug\n");
190 #else
191             fprintf (stderr, " WIN32 Release\n");
192 #endif
193 #endif
194 #if HAVE_BZLIB_H
195             fprintf (stderr, "libbzip2\n"
196                      " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
197 #endif
198         }
199         else if (ret == 'v')
200             yaz_log_init_level (yaz_log_mask_str(arg));
201         else if (ret == 'l')
202             yaz_log_init_file (arg);
203         else if (ret == 'm')
204             mem_max = 1024*1024*atoi(arg);
205         else if (ret == 'd')
206             rGroupDef.databaseName = arg;
207         else if (ret == 's')
208             rGroupDef.flagRw = 0;
209         else if (ret == 'g')
210             rGroupDef.groupName = arg;
211         else if (ret == 'f')
212             rGroupDef.fileVerboseLimit = atoi(arg);
213         else if (ret == 'c')
214             configName = arg;
215         else if (ret == 't')
216             rGroupDef.recordType = arg;
217         else if (ret == 'n')
218             disableCommit = 1;
219         else
220             logf (LOG_WARN, "unknown option '-%s'", arg);
221     }
222     zebra_close (zh);
223     zebra_stop (zs);
224     exit (0);
225     return 0;
226 }
227