14 #include <sys/types.h>
16 #include <sys/utsname.h>
31 #define UI_TEXT 0 /* Default setup type -- text only */
32 #define UI_SILENT 3 /* Silent running, for use in scripts */
33 #define UI_NEWT 4 /* Use the "newt" window library */
36 char setup_directory[SIZ];
41 * Set an entry in inittab to the desired state
43 void set_init_entry(char *which_entry, char *new_state) {
53 if (inittab == NULL) return;
55 fp = fopen("/etc/inittab", "r");
56 if (fp == NULL) return;
58 while(fgets(buf, sizeof buf, fp) != NULL) {
60 if (num_tokens(buf, ':') == 4) {
61 extract_token(entry, buf, 0, ':');
62 extract_token(levels, buf, 1, ':');
63 extract_token(state, buf, 2, ':');
64 extract_token(prog, buf, 3, ':'); /* includes 0x0a LF */
66 if (!strcmp(entry, which_entry)) {
67 strcpy(state, new_state);
68 sprintf(buf, "%s:%s:%s:%s",
69 entry, levels, state, prog);
73 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
74 if (inittab == NULL) {
82 fp = fopen("/etc/inittab", "w");
84 fwrite(inittab, strlen(inittab), 1, fp);
86 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
95 * Shut down the Citadel service if necessary, during setup.
97 void shutdown_service(void) {
100 char looking_for[SIZ];
105 strcpy(init_entry, "");
107 /* Determine the fully qualified path name of citserver */
108 snprintf(looking_for, sizeof looking_for, "%s/citserver ", WEBCITDIR);
110 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
111 * an entry is found which we believe starts citserver.
113 infp = fopen("/etc/inittab", "r");
117 while (fgets(buf, sizeof buf, infp) != NULL) {
118 buf[strlen(buf) - 1] = 0;
119 extract_token(entry, buf, 0, ':');
120 extract_token(prog, buf, 3, ':');
121 if (!strncasecmp(prog, looking_for,
122 strlen(looking_for))) {
124 strcpy(init_entry, entry);
130 /* Bail out if there's nothing to do. */
131 if (!have_entry) return;
133 set_init_entry(init_entry, "off");
138 * Start the Citadel service.
140 void start_the_service(void) {
141 if (strlen(init_entry) > 0) {
142 set_init_entry(init_entry, "respawn");
148 void cleanup(int exitcode)
160 void title(char *text)
162 if (setup_type == UI_TEXT) {
163 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
169 int yesno(char *question)
172 newtComponent form = NULL;
173 newtComponent yesbutton = NULL;
174 newtComponent nobutton = NULL;
180 switch (setup_type) {
184 printf("%s\nYes/No --> ", question);
185 fgets(buf, sizeof buf, stdin);
186 answer = tolower(buf[0]);
189 else if (answer == 'n')
191 } while ((answer < 0) || (answer > 1));
196 newtCenteredWindow(76, 10, "Question");
197 form = newtForm(NULL, NULL, 0);
198 for (i=0; i<num_tokens(question, '\n'); ++i) {
199 extract_token(buf, question, i, '\n');
200 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
202 yesbutton = newtButton(10, 5, "Yes");
203 nobutton = newtButton(60, 5, "No");
204 newtFormAddComponent(form, yesbutton);
205 newtFormAddComponent(form, nobutton);
206 if (newtRunForm(form) == yesbutton) {
213 newtFormDestroy(form);
222 void set_value(char *prompt, char str[])
230 char tempfile[PATH_MAX];
233 strcpy(tempfile, tmpnam(NULL));
234 strcpy(setupmsg, "");
236 switch (setup_type) {
238 title("WebCit setup");
239 printf("\n%s\n", prompt);
240 printf("This is currently set to:\n%s\n", str);
241 printf("Enter new value or press return to leave unchanged:\n");
242 fgets(buf, sizeof buf, stdin);
243 buf[strlen(buf) - 1] = 0;
244 if (strlen(buf) != 0)
250 newtCenteredWindow(76, 10, "WebCit setup");
251 form = newtForm(NULL, NULL, 0);
252 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
253 extract_token(buf, prompt, i, '\n');
254 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
256 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
257 NEWT_FLAG_RETURNEXIT));
262 newtFormDestroy(form);
269 void important_message(char *title, char *msgtext)
272 newtComponent form = NULL;
277 switch (setup_type) {
280 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
281 printf(" %s \n\n%s\n\n", title, msgtext);
282 printf("Press return to continue...");
283 fgets(buf, sizeof buf, stdin);
288 newtCenteredWindow(76, 10, title);
289 form = newtForm(NULL, NULL, 0);
290 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
291 extract_token(buf, msgtext, i, '\n');
292 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
294 newtFormAddComponent(form, newtButton(35, 5, "OK"));
297 newtFormDestroy(form);
305 void display_error(char *error_message)
307 important_message("Error", error_message);
310 void progress(char *text, long int curr, long int cmax)
314 /* These variables are static because progress() gets called
315 * multiple times during the course of whatever operation is
316 * being performed. This makes setup non-threadsafe, but who
319 static newtComponent form = NULL;
320 static newtComponent scale = NULL;
322 static long dots_printed = 0L;
325 switch (setup_type) {
329 printf("%s\n", text);
330 printf("..........................");
331 printf("..........................");
332 printf("..........................\r");
335 } else if (curr == cmax) {
336 printf("\r%79s\n", "");
338 a = (curr * 100) / cmax;
341 while (dots_printed < a) {
352 newtCenteredWindow(76, 8, text);
353 form = newtForm(NULL, NULL, 0);
354 scale = newtScale(1, 3, 74, cmax);
355 newtFormAddComponent(form, scale);
359 if ((curr > 0) && (curr <= cmax)) {
360 newtScaleSet(scale, curr);
364 newtFormDestroy(form);
378 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
381 void check_inittab_entry(void)
385 char looking_for[SIZ];
388 char listenport[128];
392 /* Determine the fully qualified path name of citserver */
393 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
395 /* If there's already an entry, then we have nothing left to do. */
396 if (strlen(init_entry) > 0) {
400 /* Otherwise, prompt the user to create an entry. */
401 snprintf(question, sizeof question,
402 "There is no '%s' entry in /etc/inittab.\n"
403 "Would you like to add one?",
405 if (yesno(question) == 0)
408 snprintf(question, sizeof question,
409 "On which port do you want WebCit to listen for HTTP "
410 "requests?\n\nYou can use the standard port (80) if you are "
411 "not running another web server\n(such as Apache), otherwise "
412 "select another port.");
413 sprintf(listenport, "2000");
414 set_value(question, listenport);
416 /* Find out where Citadel is. */
417 snprintf(question, sizeof question,
418 "Is the Citadel service running on the same host as WebCit?");
419 if (yesno(question)) {
420 sprintf(hostname, "uds");
421 sprintf(portname, "/usr/local/citadel");
422 set_value("In what directory is Citadel installed?", portname);
425 sprintf(hostname, "127.0.0.1");
426 sprintf(portname, "504");
427 set_value("Enter the host name or IP address of your "
428 "Citadel server.", hostname);
429 set_value("Enter the port number on which Citadel is "
430 "running (usually 504)", portname);
433 /* Generate a unique entry name for /etc/inittab */
434 snprintf(entryname, sizeof entryname, "c0");
437 if (entryname[1] > '9') {
440 if (entryname[0] > 'z') {
442 "Can't generate a unique entry name");
446 snprintf(buf, sizeof buf,
447 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
448 } while (system(buf) == 0);
450 /* Now write it out to /etc/inittab */
451 infp = fopen("/etc/inittab", "a");
453 display_error(strerror(errno));
455 fprintf(infp, "# Start the WebCit server...\n");
456 fprintf(infp, "%s:2345:respawn:%s %s %s\n",
457 entryname, looking_for, hostname, portname);
459 strcpy(init_entry, entryname);
467 * Check to see if our server really works. Returns 0 on success.
469 int test_server(void) {
471 return 0; /* FIXME ... stubbed out */
479 * Figure out what type of user interface we're going to use
481 int discover_ui(void)
487 newtDrawRootText(0, 0, "WebCit Setup");
497 int main(int argc, char *argv[])
503 /* set an invalid setup type */
506 /* parse command line args */
507 for (a = 0; a < argc; ++a) {
508 if (!strncmp(argv[a], "-u", 2)) {
509 strcpy(aaa, argv[a]);
510 strcpy(aaa, &aaa[2]);
511 setup_type = atoi(aaa);
513 if (!strcmp(argv[a], "-i")) {
516 if (!strcmp(argv[a], "-q")) {
517 setup_type = UI_SILENT;
522 /* If a setup type was not specified, try to determine automatically
523 * the best one to use out of all available types.
525 if (setup_type < 0) {
526 setup_type = discover_ui();
528 if (info_only == 1) {
529 important_message("WebCit Setup", "Welcome to WebCit setup");
533 /* Get started in a valid setup directory. */
534 strcpy(setup_directory, WEBCITDIR);
535 set_value("FIXME enter your setup directory, dumbass.",
537 if (chdir(setup_directory) != 0) {
538 important_message("WebCit Setup",
539 "The directory you specified does not exist.");
543 /* See if we need to shut down the WebCit service. */
544 for (a=0; a<=3; ++a) {
545 progress("Shutting down the WebCit service...", a, 3);
546 if (a == 0) shutdown_service();
550 /* Make sure it's stopped. FIXME put back in
551 if (test_server() == 0) {
552 important_message("WebCit Setup",
553 "The WebCit service is still running.\n"
554 "Please stop the service manually and run "
561 switch (setup_type) {
565 " *** WebCit setup program ***\n\n");
570 check_inittab_entry(); /* Check /etc/inittab */
572 /* See if we can start the WebCit service. */
573 if (strlen(init_entry) > 0) {
574 for (a=0; a<=3; ++a) {
575 progress("Starting the WebCit service...", a, 3);
576 if (a == 0) start_the_service();
579 if (test_server() == 0) {
580 important_message("Setup finished",
581 "Setup is finished. You may now log in.");
584 important_message("Setup finished",
585 "Setup is finished, but the WebCit service "
587 "Go back and check your configuration.");
591 important_message("Setup finished",
592 "Setup is finished. You may now start the server.");