[project @ 2002-07-07 02:33:51 by unknown_lamer]
[clinton/bobotpp.git] / source / Main.C
CommitLineData
cb21075d 1// Main.C -*- C++ -*-
2// Copyright (c) 1997, 1998 Etienne BERNARD
3// Copyright (C) 2002 Clinton Ebadi
4
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// any later version.
9
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <unistd.h>
24#include <iostream>
25#include <cstdio>
26#include <csignal>
27
28#ifdef USESCRIPTS
29#include "Interp.H"
30#endif
31
32#include "Bot.H"
33
34Bot *b;
35
36void sig_hup(int) {
37 if (b) {
38 b->userList->read();
39 b->userList->addUserFirst("bidon", "bidon", 0, 0, false, 0, "");
40 b->reconnect();
41 }
42}
43
44void printUsage(char *name)
45{
46 std::cout << "Usage: " << name << " [-h] [-b] [-f file] [-d dir] [-D]\n"
47 " -h Shows this help.\n"
48 " -b Do not run in background.\n"
49 " -f file Use file as config file.\n"
50 " -d dir Use dir as current dir.\n"
51 " -D Debug mode (input/output printing and no background mode.\n";
52}
53
54static void real_main(void* DONOTUSE, int argc, char **argv)
55{
56 int opt;
57 extern char *optarg;
58 bool background = true;
59 String configFile = "bot.conf";
60 String directory = "";
61 bool debug = false;
62
63 std::signal(SIGPIPE, SIG_IGN);
64 std::signal(SIGALRM, SIG_IGN);
65 std::signal(SIGHUP, sig_hup);
66
67 // We parse the command line options
68 while ((opt = getopt(argc,argv,"hbf:d:D")) != EOF)
69 switch (opt) {
70 case 'h':
71 printUsage(argv[0]); exit(0);
72 case 'b':
73 background = false;
74 break;
75 case 'f':
76 configFile = optarg;
77 break;
78 case 'd':
79 directory = optarg;
80 break;
81 case 'D':
82 debug = true;
83 break;
84 default:
85 printUsage(argv[0]); exit(1);
86 }
87
88 if ((directory != "") && (chdir((const char *)directory)<0))
89 std::perror("Warning: ");
90
91 std::cout << COPYRIGHT_STRING <<
92 "\n"PACKAGE" comes with ABSOLUTELY NO WARRANTY\n"
93 "This is free software, and you are welcome to redistribute it\n"
94 "under certain conditions; See the COPYING file for details.\n";
95
96 if (!debug) {
97 if (background)
98 switch (fork()) {
99 case -1:
100 std::cout << "Could not run in the background. Exiting...\n";
101 perror("fork");
102 exit(1);
103 case 0:
104 break;
105 default:
106 std::cout << "Running in the background...\n";
107 exit(0);
108 }
109 }
110
111#ifdef USESCRIPTS
112 Interp::Startup();
113#endif
114
115 b = new Bot(configFile, debug);
116 b->run();
117 delete b;
118
119#ifdef USESCRIPTS
120 Interp::Shutdown();
121#endif
122}
123
124int main(int argc, char **argv) {
125#ifdef USESCRIPTS
126 scm_boot_guile (argc, argv, real_main, 0);
127#else
128 real_main(0, argc, argv);
129#endif
130
131 return 0;
132}