File: config.cpp

package info (click to toggle)
bayonne 2.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 11,180 kB
  • ctags: 3,662
  • sloc: cpp: 38,791; sh: 9,323; ansic: 1,391; makefile: 485; perl: 471; java: 405; cs: 402; php: 354; python: 293
file content (967 lines) | stat: -rw-r--r-- 23,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
// Copyright (C) 2005 Open Source Telecom Corp.
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "server.h"

#ifndef	WIN32
#include <sys/utsname.h>
#endif

namespace server {
using namespace ost;
using namespace std;

#ifdef	WIN32
#define	PROMPT_FILES	"C:\\Program Files\\GNU Telephony\\Phrasebook Audio"
#define	SCRIPT_FILES	"C:\\Program Files\\GNU Telephony\\Bayonne Scripts"
#define	MACRO_FILES	"C:\\Program Files\\GNU Telephony\\Bayonne Macros"
#define	VAR_FILES	"C:\\Program Files\\GNU Telephony\\Bayonne Files"
#define	LOG_FILES	"C:\\Program Files\\GNU Telephony\\Bayonne Logs"
#define	CONFIG_FILES	"C:\\Program Files\\GNU Telephony\\Bayonne Config"
#define	RUN_FILES	"C:\\Program Files\\GNU Telephony\\Bayonne Logs"
#define	LIBEXEC_FILES	"C:\\Program Files\\GNU Telephony\\Bayonne Libxec"

#ifdef	_DEBUG
#define	LIBDIR_FILES	"C:\\Program Files\\GNU Telephony\\CAPE Framework\\debug"
#define	MODULE_FILES	LIBDIR_FILES
#define	DRIVER_FILES	LIBDIR_FILES
#define	PHRASE_FILES	LIBDIR_FILES	
#else	
#define	LIBDIR_FILES	"C:\\Program Files\\Common Files\\GNU Telephony\\Runtime"
#define	MODULE_FILES	"C:\\Program Files\\Common Files\\GNU Telephony\\Bayonne Plugins"
#define	DRIVER_FILES	"C:\\Program Files\\Common Files\\GNU Telephony\\Bayonne Drivers"
#define	PHRASE_FILES	"C:\\Program Files\\Common Files\\GNU Telephony\\Bayonne Languages"
#endif

#else
#define	MODULE_FILES	LIBDIR_FILES
#define	DRIVER_FILES	LIBDIR_FILES
#define	PHRASE_FILES	LIBDIR_FILES
#endif

static Keydata::Define keypad[] = {
	{"2", "abc"},
	{"3", "def"},
	{"4", "ghi"},
	{"5", "jkl"},
	{"6", "mno"},
	{"7", "pqrs"},
	{"8", "tuv"},
	{"9", "wxyz"},
	{NULL, NULL}}; 

static Keydata::Define paths[] = {
	{"libexec", LIBEXEC_FILES},
	{"datafiles", VAR_FILES},
	{"logfiles", LOG_FILES},
	{"runfiles", RUN_FILES},
	{"config", CONFIG_FILES},
	{"scripts", SCRIPT_FILES},
	{"macros", MACRO_FILES},
	{"prompts", PROMPT_FILES},
#ifndef	WIN32
	{"btsexec", BIN_FILES "/btsexec"},
#endif
	{NULL, NULL}};

static Keydata::Define server[] = {
	{"user", "bayonne"},
	{"group", "bayonne"},
	{"language", "none"},
	{"location", "us"},
	{"voice", "none/prompts"},
	{"state", "init"},
	{"logging", "warn"},
	{"monitoring", "tcp"},
	{"binding", "default"},
	{"definitions", "url"},
	{NULL, NULL}};

static Keydata::Define engine[] = {
	{"priority", "0"},
	{"scheduler", "default"},
	{"events", "32"},
	{"server", "localhost"},
	{"userid", ""},
	{"secret", ""},
	{"timeslots", "32"},
	{"driver", "autodetect"},
	{"protocol", "none"},
	{NULL, NULL}};

static Keydata::Define libkeys[] = {
	{"filesystem", VAR_FILES},
	{"interface", "eth0"},
	{"limit", "0"},
	{"tts", "theta"},
	{"voice", "female"},
	{"ttslimit", "0"},
	{NULL, NULL}};

static Keydata::Define localkeys[] = {
	{"encoding", "ulaw"},
	{"framing", "20"},
	{"ringing", "20s"},
	{"reconnect", "ulaw,alaw,g721,gsm"},
	{NULL, NULL}};

static Keydata::Define remotekeys[] = {
    {"encoding", "gsm"},
    {"framing", "20"},
    {"ringing", "20s"},
    {"reconnect", "ulaw,alaw,g721,gsm"},
    {NULL, NULL}};

static Keydata::Define msgkeys[] = {
	{"extension", ".au"},
	{"encoding", "ulaw"},
	{"framing", "20"},
	{"maxlimit", "600s"},
	{"greeting", "60s"},
	{"new", "7d"},
	{"read", "4w"},
	{"saved", "never"},
    {NULL, NULL}};

static Keydata::Define smtpkeys[] = {
	{"limit", "0"},
	{"sendmail", "/usr/sbin/sendmail"},
	{"sender", "bayonne@localhost.localdomain"},
	{"from", "Bayonne Server <bayonne@localhost.localdomain>"},
	{"errors", "postmaster@localhost.localdomain"}, 
	{NULL, NULL}};

static Keydata::Define urlkeys[] = {
	{"limit", "0"},
	{"prefix", "http://localhost"},
	{"agent", "bayonne/" VERSION},
	{NULL, NULL}};

static class KeymapConfig : public ReconfigKeydata, public Bayonne
{
private:
	virtual void updateConfig(Keydata *cfg);

public:
	KeymapConfig();

}	keymap;

bool flag_daemon = false;
bool flag_check = false;

StaticKeydata keypaths("/bayonne/server/paths", paths);
StaticKeydata keyserver("/bayonne/server/server", server);
StaticKeydata keyengine("/bayonne/server/engine", engine);
BayonneConfig keylibexec("libexec", libkeys, "/bayonne/server/libexec");
BayonneConfig keysmtp("smtp", smtpkeys, "/bayonne/server/smtp");
BayonneConfig keyurl("url", urlkeys, "/bayonne/server/url");
BayonneConfig keymessage("message", msgkeys, "/bayonne/server/message");
BayonneConfig keylocal("local", localkeys, "/bayonne/server/local");
BayonneConfig keyremote("remote", remotekeys, "/bayonne/server/remote");

Keydata keyoptions;

KeymapConfig::KeymapConfig() :
ReconfigKeydata("/bayonne/server/keymap", keypad)
{
	KeymapConfig::updateConfig(NULL);
}

void KeymapConfig::updateConfig(Keydata *cfg)
{
	char localmap[256];
	char keycode[2];
	unsigned char c, code;
	const char *cp;

	keycode[1] = 0;
	memset(localmap, 0, sizeof(localmap));
	for(c = '0'; c <= '9'; ++c)
	{
		code = (unsigned char)c;
		localmap[code] = c;
		keycode[0] = c;
		cp = updatedString(keycode);
		if(!cp || !*cp)
			continue;
		while(*cp)
		{
			if(isalpha(*cp))
			{
				code = (unsigned char)toupper(*cp);
				localmap[code] = c;
				code = (unsigned char)tolower(*cp);
				localmap[code] = c;
			}
			else
			{
				code = (unsigned char)*cp;
				localmap[code] = c;
			}
			++cp;
		}
	}
	code = (unsigned char)'*';
	localmap[code] = '*';
	code = (unsigned char)'#';
	localmap[code] = '#';
	memcpy(dtmf_keymap, localmap, 256);
}

void liveConfig(void)
{
	const char *cfgid = Process::getEnv("CONFIG");

	char path[256];
	char lang[32];
	const char *env;
	char *p, *sp, *tok;
	const char *cp = cfgid;
	bool ttsdef = false, libdef = false;
	unsigned timeslots = atoi(keyengine.getLast("timeslots"));
	Bayonne::timeslot_t overdraft = 1;

	while(cp && *cp && isalnum(*cp))
		++cp;

	if(cp && *cp)
	{
		slog.critical("invalid config in environment");
		exit(-1);
	}

#ifdef	WIN32
        char nodename[MAX_COMPUTERNAME_LENGTH + 1];
        DWORD namelen = MAX_COMPUTERNAME_LENGTH + 1;
        SYSTEM_INFO sysinfo;
        OSVERSIONINFO osver;
	const char *cpu = "x86";
	const char *tmp = Process::getEnv("TEMP");
	if(!tmp)
		tmp = Process::getEnv("TMP");
	if(!tmp)
		tmp = "C:\\TEMP";
	if(!keypaths.getLast("tmp"))
		keypaths.setValue("tmp", tmp);
	if(!keypaths.getLast("tmpfs"))
		keypaths.setValue("tmpfs", tmp);
	Dir::create(tmp);

        osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
        GetVersionEx(&osver);
        GetComputerName(nodename, &namelen);
        keyserver.setValue("node", nodename);
		if(cfgid)
			keyserver.setValue("node", cfgid);

        GetSystemInfo(&sysinfo);
        switch(sysinfo.wProcessorArchitecture)
        {
        case PROCESSOR_ARCHITECTURE_INTEL:
		cpu="x86";
                break;
        case PROCESSOR_ARCHITECTURE_PPC:
                cpu = "ppc";
                break;
        case PROCESSOR_ARCHITECTURE_MIPS:
                cpu = "mips";
                break;
        case PROCESSOR_ARCHITECTURE_ALPHA:
                cpu = "alpha";
                break;
        }
	keyserver.setValue("node", nodename);
	keyserver.setValue("platform", "w32");
	keyserver.setValue("cpu", cpu);
#else
	struct utsname uts;
	char hostname[128];
	char *userid;
	uname(&uts);
	memset(hostname, 0, sizeof(hostname));
	gethostname(hostname, sizeof(hostname) - 1);
	hostname[sizeof(hostname) - 1] = 0;
	char *hp = strchr(hostname, '.');
	if(hp)
		*hp = 0;

	if(!keyserver.getLast("node"))
	{
		if(cfgid)
			keyserver.setValue("node", cfgid);
		else
			keyserver.setValue("node", hostname);
	}

	keyserver.setValue("platform", uts.sysname);
	keyserver.setValue("cpu", uts.machine);
	userid = getenv("USER");

	if(!userid || !stricmp(userid, "root"))
		userid="bayonne";	

	if(!cfgid)
		cfgid = userid;

	snprintf(path, sizeof(path), "/tmp/bayonne-%s", cfgid);
	keypaths.setValue("tmp", path);
	purgedir(path);
	Dir::create(path);

	if(canModify("/dev/shm"))
	{
		snprintf(path, sizeof(path), "/dev/shm/bayonne-%s", cfgid);
		keypaths.setValue("tmpfs", path);
		purgedir(path);
		Dir::create(path);
	}
	else
		keypaths.setValue("tmpfs", path);

	if(cfgid == userid)
		cfgid = NULL;

#endif
        sp = (char *)keyserver.getLast("definitions");

	if(sp && !stricmp(sp, "default"))
	{
		sp = NULL;
		cp = Process::getEnv("DEFINES");
		if(cp)
		{
			setString(path, sizeof(path), cp);
			sp = path;
		}
	}

        if(sp && !stricmp(sp, "none"))
                sp = NULL;

	if(sp && !stricmp(sp, "off"))
	{
		libdef = ttsdef = true;
		sp = NULL;
	}

        if(sp && !*sp)
                sp = NULL;
        if(sp)
                cp = strtok_r(sp, " ,;:\t", &tok);
        else
                cp = NULL;

        while(cp)
        {
                if(strrchr(cp, '.'))
                        setString(path, sizeof(path), cp);
                else
                        snprintf(path, sizeof(path), "%s.def", cp);
		if(!stricmp(path, "libexec.def"))
			libdef = true;
		if(!stricmp(path, "tts.def"))
			ttsdef = true;
                runtime.setValue("definitions", path);
                cp = strtok_r(NULL, " ,;:\t", &tok);
        }

#ifdef	HAVE_LIBEXEC
	cp = Process::getEnv("LIBEXEC");
	if(!cp || strnicmp(cp, "no", 2))
	{
		if(!libdef)
			runtime.setValue("definitions", "libexec.def");

		if(!ttsdef)
			runtime.setValue("definitions", "tts.def");
	}
#endif

	cp = keyoptions.getLast("prefix");
	if(cp && *cp)
		chdir(cp);

	env = Process::getEnv("LANG");
	if(env)
	{
		snprintf(lang, sizeof(lang), "%s", env);
		p = strchr(lang, '.');
		if(p)
			*p = 0;
		p = strchr(lang, '_');
		if(p)
		{
			p[1] = tolower(p[1]);
			p[2] = tolower(p[2]);
			if(isalpha(p[1]) && isalpha(p[2]) && !p[3])
				keyserver.setValue("location", ++p);
		}
		lang[0] = tolower(lang[0]);
		lang[1] = tolower(lang[1]);
		if(isalpha(lang[0]) && isalpha(lang[1]))
			keyserver.setValue("language", lang);
	}
#ifdef	WIN32
	snprintf(path, sizeof(path), "%s/tones.ini", keypaths.getLast("config"));
#else
	snprintf(path, sizeof(path), "%s/tones.conf", keypaths.getLast("config"));
#endif
	TelTone::load(path);
	keypaths.setValue("tones", path);

#ifdef	WIN32
#define	EVENTS	"%s/%s.evt"
#define	ERRORS	"%s/%s.err"
#define	OUTPUT	"%s/%s.out"
#define	CALLER	"%s/%s.log"
#define	CALLS   "%s/%s.cdr"
#define	STATS	"%s/%s.sta"
#else
#define	EVENTS	"%s/%s.events"
#define	ERRORS	"%s/%s.errors"
#define	OUTPUT	"%s/%s.output"
#define	CALLER	"%s/%s.sessions"
#define CALLS	"%s/%s.calls"
#define	STATS	"%s/%s.stats"
#endif

	runtime.setValue("node", keyserver.getLast("node"));

	snprintf(path, sizeof(path), ERRORS,
		keypaths.getLast("logfiles"), keyserver.getLast("node"));
	runtime.setValue("errlog", path);

        snprintf(path, sizeof(path), EVENTS,
                keypaths.getLast("logfiles"), keyserver.getLast("node"));
        runtime.setValue("evtlog", path); 

        snprintf(path, sizeof(path), OUTPUT,
                keypaths.getLast("logfiles"), keyserver.getLast("node"));
       	runtime.setValue("output", path);

        snprintf(path, sizeof(path), CALLER,
                keypaths.getLast("logfiles"), keyserver.getLast("node"));
        runtime.setValue("calls", path);

        snprintf(path, sizeof(path), CALLS, 
		keypaths.getLast("logfiles"), keyserver.getLast("node"));
        runtime.setValue("cdr", path);

        snprintf(path, sizeof(path), STATS,
                keypaths.getLast("logfiles"), keyserver.getLast("node"));
        runtime.setValue("stats", path);

	runtime.setValue("include", keypaths.getLast("macros"));
	keypaths.setValue("include", keypaths.getLast("macros"));
	runtime.setValue("tmp", keypaths.getLast("tmp"));
	runtime.setValue("tmpfs", keypaths.getLast("tmpfs"));
	runtime.setValue("prompts", keypaths.getLast("prompts"));
	runtime.setValue("prefix", keypaths.getLast("datafiles"));
	runtime.setValue("config", keypaths.getLast("config"));
	runtime.setValue("voice", keyserver.getLast("voice"));
	runtime.setValue("logfiles", keypaths.getLast("logfiles"));
	runtime.setValue("runfiles", keypaths.getLast("runfiles"));
	runtime.setValue("servername", "bayonne2");
	runtime.setValue("serverversion", VERSION);
	
	cp = keyengine.getLast("server");
	if(cp)
		runtime.setValue("server", cp);
	cp = keyengine.getLast("proxy");
	if(cp)
		runtime.setValue("proxy", cp);

	runtime.setValue("userid", keyengine.getLast("userid"));
	runtime.setValue("secret", keyengine.getLast("secret"));
	runtime.setValue("location", keyserver.getLast("location"));

	cp = keyengine.getLast("autostack");
	if(cp)
		Thread::setStack(atoi(cp) * 1024);

	cp = keyengine.getLast("stepping");
	if(cp)
		Bayonne::step_timer = atoi(cp);

	cp = keyengine.getLast("reset");
	if(cp)
		Bayonne::reset_timer = atoi(cp);

	cp = keyengine.getLast("exec");
	if(cp)
		Bayonne::exec_timer = atoi(cp) * 1000;

	cp = keyengine.getLast("autostep");
	if(cp)
		Script::autoStepping = atoi(cp);

	cp = keyengine.getLast("faststep");
	if(cp)
		Script::fastStepping = atoi(cp);

	cp = keyengine.getLast("paging");
	if(!cp)
		cp = keyengine.getLast("pagesize");
	if(cp)
	{
		Script::pagesize = atoi(cp);
		Script::symlimit = Script::pagesize - sizeof(Script::Symbol) - 32;
	}

	cp = keyengine.getLast("symsize");
	if(cp)
		Script::symsize = atoi(cp);	

	Script::fastStart = false;

#ifdef	WIN32
	Script::exec_extensions = SCRIPT_EXTENSIONS;
#else
	Script::exec_extensions = SCRIPT_EXTENSIONS;
#endif
	Script::exec_token = "bayonne:";

	cp = Process::getEnv("LIBEXEC");
	if(!cp || !*cp)
	{
		cp = keyengine.getLast("libexec");
		if(cp && *cp)
			Process::setEnv("LIBEXEC", cp, true);
	}

	cp = keyengine.getLast("virtual");
	if(cp)
		overdraft = atoi(cp);

        Bayonne::allocate(timeslots, dynamic_cast<ScriptCommand *>(&runtime), overdraft); 
}

void loadConfig(void)
{
	const char *cfgid = Process::getEnv("CONFIG");
	char path[128];

    if(cfgid)
    {
        snprintf(path, sizeof(path), "%s-%s", CONFIG_FILES, cfgid);
        keypaths.setValue("config", path);
        snprintf(path, sizeof(path), "%s-%s", RUN_FILES, cfgid);
        keypaths.setValue("runfiles", path);
    }

#ifdef	WIN32
	char pbuf[256];
	const char *config = keypaths.getLast("config");

//	Process::setGroup(keyserver.getLast("group"));
        Dir::create(keypaths.getLast("datafiles"));
        Dir::create(keypaths.getLast("runfiles"));
        Dir::create(keypaths.getLast("logfiles"));
#else
	char homepath[256];
	const char *home = NULL;
	bool homed = false;

	if(getuid())
		home = Process::getEnv("HOME");

	if(!isDir(keypaths.getLast("config")))
		keypaths.setValue("config", "/bayonne");

	if(home)
		snprintf(homepath, sizeof(homepath), "%s/.bayonne", home);
	else
		strcpy(homepath, "tempfiles");

	umask(007);
	Process::setGroup(keyserver.getLast("group"));
	Dir::create(keypaths.getLast("datafiles"));
	Dir::create(keypaths.getLast("runfiles"));
	Dir::create(keypaths.getLast("logfiles"));

	keypaths.setValue("home", keypaths.getLast("datafiles"));

	if(!getuid())
		goto live;

	if(!canModify(keypaths.getLast("datafiles")))
	{
		homed = true;
		keypaths.setValue("datafiles", homepath);
	}

	keypaths.setValue("control", keypaths.getLast("runfiles"));
	if(!canModify(keypaths.getLast("runfiles")))
	{
		homed = true;
		keypaths.setValue("runfiles", homepath);
	}

	if(!canModify(keypaths.getLast("logfiles")))
	{
		homed = true;
		keypaths.setValue("logfiles", homepath);
	}

	if(homed)
	{
		umask(077);
	        Dir::create(homepath, File::attrPrivate);
	}
live:
#endif
	keyoptions.setValue("prefix", keypaths.getLast("datafiles"));
	keyserver.setValue("state", "live");

#ifdef	SCRIPT_SERVER_PREFIX
	Script::var_prefix = keypaths.getLast("datafiles");
	Script::etc_prefix = keypaths.getLast("config");
	Script::log_prefix = keypaths.getLast("logfiles");
#endif

	// only for live server...
}

bool parseConfig(char **argv)
{
	char buffer[64];
	char path[256];
	const char *prefix;
	char *option;
	char *script = NULL;
	char *ext;
	unsigned scrcount = 0;

#ifndef	WIN32
	const char *cp;
	cp = keyengine.getLast("driver");
	if(cp && !stricmp(cp, "autodetect"))
		cp = Process::getEnv("TIMESLOTS");
	else if(cp && !stricmp(cp, "default"))
		cp = Process::getEnv("TIMESLOTS");

	if(cp && atoi(cp) > 0)
		keyengine.setValue("timeslots", cp);
#endif

	++argv;

retry:
	if(!*argv)
		goto skip;

	option = *argv;


        if(!strcmp("--", option))
        {
                ++argv;
                goto skip;
        }

        if(!strnicmp("--", option, 2))
                ++option;

	if(!strnicmp(option, "-config=", 8))
	{
		runtime.setValue("serverconfig", option + 8);
		++argv;
		goto retry;
	}

	if(!strnicmp(option, "-driver=", 8))
	{
		keyengine.setValue("driver", option + 8);
		++argv;
		goto retry;
	} 

	if(!strnicmp(option, "-protocol=", 10))
	{
		keyengine.setValue("protocol", option + 10);
		++argv;
		goto retry;
	}

        if(!strnicmp(option, "-bind=", 6))
        {
                keyserver.setValue("binding", option + 6);    
                ++argv;
                goto retry;
        }  


	if(!stricmp(option, "-check"))
	{
		keyserver.setValue("logging", "debug");
		flag_check = true;
		++argv;
		goto retry;
	}

	if(!strnicmp(option, "-vvv", 4))
	{
		flag_daemon = false;
		keyserver.setValue("logging", "debug");
		++argv;
		goto retry;
	}

        if(!stricmp(option, "-vv"))
        {
		flag_daemon = false;
                keyserver.setValue("logging", "notice");
                ++argv;
                goto retry;
        }

        if(!stricmp(option, "-v"))
        {
		flag_daemon = false;
                keyserver.setValue("logging", "error");
                ++argv;
                goto retry;
        }

	if(!stricmp(option, "-p"))
	{
		keyengine.setValue("priority", "2");
		keyengine.setValue("scheduler", "fifo");
		++argv;
		goto retry;
	}

	if(!strnicmp(option, "-voice=", 7))
	{
		keyserver.setValue("voice", option + 7);
		++argv;
		goto retry;
	} 

	if(!strnicmp(option, "-location=", 10))
	{ 
		Process::setEnv("LANG", option + 10, true);
		keyserver.setValue("location", option + 10);
		++argv;
		goto retry;
	}

        if(!strnicmp(option, "-timeslots=", 11))
        {
		keyengine.setValue("timeslots", option + 11);
                ++argv;
                goto retry;
        }

        if(!stricmp(option, "-location"))  
        {
                ++argv;
                if(!argv) 
                {
        		cerr << "bayonne: -location: missing argument" << endl;
                        exit(-1);
                }      
		Process::setEnv("LANG", *argv, true);
                keyserver.setValue("location", *(argv++));
                goto retry;
        }


	if(!stricmp(option, "-voice"))
	{
		++argv;
		if(!argv)
		{
			cerr << "bayonne: -voice: missing argument" << endl;
			exit(-1);
		}
		keyserver.setValue("voice", *(argv++));
		goto retry;
	}

        if(!stricmp(option, "-driver"))
        {
                ++argv;
                if(!*argv)
                {
			cerr << "bayonne: -driver: missing argument" << endl;
                        exit(-1);
                }
                keyengine.setValue("driver", *(argv++));
                goto retry;
        }

        if(!stricmp(option, "-config"))
        {
                ++argv;
                if(!*argv)
                {
                        cerr << "bayonne: -config: missing argument" << endl;
                        exit(-1);
                }
                runtime.setValue("serverconfig", *(argv++));
                goto retry;
        }

        if(!stricmp(option, "-protocol"))
        {
                ++argv;
                if(!*argv)
                {
                        cerr << "bayonne: -protocol: missing argument" << endl;
                        exit(-1);
                }
                keyengine.setValue("protocol", *(argv++));
                goto retry;
        }

        if(!stricmp(option, "-bind"))
        {
                ++argv;    
                if(!*argv)
                {
                        cerr << "bayonne: -binding: missing argument" << endl;
                        exit(-1);
                }
                keyserver.setValue("binding", *argv);
		++argv;
                goto retry;
        } 

        if(!stricmp(option, "-timeslots"))
        {
                ++argv;
                if(!*argv)
                {
			cerr << "bayonne: -timeslots: missing argument" << endl;
                        exit(-1);
                }
		keyengine.setValue("timeslots", *argv);
		++argv;
                goto retry;
	}

	if(**argv == '-')
	{
		cerr << "bayonne: " << *argv << ": unknown option" << endl;
		exit(-1);
	}

skip:
	while(*argv)
	{
		ext = strrchr(*argv, '.');
		if(!ext)
			keyoptions.setValue("modules", *argv);
		else if(!stricmp(ext, ".conf") || !stricmp(ext, ".ini"))
			slog.error("bayonne: %s: invalid script file", *argv);
		else if(!stricmp(ext, ".mac") || !stricmp(ext, ".scr") || strstr(Script::exec_extensions, ext) || !stricmp(ext, ".def"))
		{
			prefix = strrchr(*argv, '/');
			if(!prefix)
				prefix = strrchr(*argv, '\\');
			if(!prefix)
				prefix = strrchr(*argv, ':');

			if(prefix)
				++prefix;

			if(!prefix)
			{
				getcwd(path, sizeof(path));
				addString(path, sizeof(path), "/");
				addString(path, sizeof(path), *argv);
				prefix = path;
			}

			if(!canAccess(*argv))
				cerr << "bayonne: " << *argv << ": cannot access" << endl;
			else
			{
				if(!scrcount++)
					script = *argv;
				else
					script = NULL;

				if(!stricmp(ext, ".def"))
					runtime.setValue("definitions", prefix);
				else
					runtime.setValue(*argv, prefix);
			}
		}
		else
		{
			if(canAccess(*argv))
				keyoptions.setValue("configs", *argv);
			else
				cerr << "bayonne: " << *argv << ": cannot access" << endl;
		}
		++argv;
	}

	if(!script) 
	{
		if(!runtime.getLast("scripts"))
			runtime.setValue("scripts", keypaths.getLast("scripts"));
		if(keypaths.getLast("addons"))
			runtime.setValue("addons", keypaths.getLast("addons"));
		return false;
	}

	snprintf(path, sizeof(path), "%s", script);
	ext = strrchr(path, '/');
	if(!ext)
		ext = strrchr(path, '\\');
	if(ext)
		*ext = 0;
	else
		getcwd(path, sizeof(path));
	Process::setEnv("SERVER_SYSEXEC", path, true);
	option = strrchr(script, '/');
	if(!option)
		option = strrchr(script, '\\');
	if(!option)
		option = strrchr(script, ':');
	if(option)
		script = ++option;
	ext = strrchr(script, '.');
	if(ext && strstr(Script::exec_extensions, ext))
		snprintf(buffer, sizeof(buffer), "exec::%s", script);
	else
		setString(buffer, sizeof(buffer), script);
	script = buffer;
	ext = strrchr(script, '.');
	if(ext && !strstr(Script::exec_extensions, ext))
		*ext = 0;

	runtime.setValue("startup", script);
	return true;
}		

void purgedir(const char *dir)
{
	const char *cp;
	if(isDir(dir) && canAccess(dir))
	{
		DirTree dt(dir, 16);
		while(NULL != (cp = dt.getPath()))
			remove(cp);
		remove(dir);
	}
}

} // namespace