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];
389 /* Determine the fully qualified path name of citserver */
390 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
392 /* If there's already an entry, then we have nothing left to do. */
393 if (strlen(init_entry) > 0) {
397 /* Otherwise, prompt the user to create an entry. */
398 snprintf(question, sizeof question,
399 "There is no '%s' entry in /etc/inittab.\n"
400 "Would you like to add one?",
402 if (yesno(question) == 0)
405 /* Generate a unique entry name for /etc/inittab */
406 snprintf(entryname, sizeof entryname, "c0");
409 if (entryname[1] > '9') {
412 if (entryname[0] > 'z') {
414 "Can't generate a unique entry name");
418 snprintf(buf, sizeof buf,
419 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
420 } while (system(buf) == 0);
422 /* Now write it out to /etc/inittab */
423 infp = fopen("/etc/inittab", "a");
425 display_error(strerror(errno));
427 fprintf(infp, "# Start the WebCit server...\n");
428 fprintf(infp, "%s:2345:respawn:%s -h%s\n",
429 entryname, looking_for, setup_directory);
431 strcpy(init_entry, entryname);
439 * Check to see if our server really works. Returns 0 on success.
441 int test_server(void) {
443 return 0; /* FIXME ... stubbed out */
451 * Figure out what type of user interface we're going to use
453 int discover_ui(void)
459 newtDrawRootText(0, 0, "WebCit Setup");
469 int main(int argc, char *argv[])
478 /* set an invalid setup type */
481 /* parse command line args */
482 for (a = 0; a < argc; ++a) {
483 if (!strncmp(argv[a], "-u", 2)) {
484 strcpy(aaa, argv[a]);
485 strcpy(aaa, &aaa[2]);
486 setup_type = atoi(aaa);
488 if (!strcmp(argv[a], "-i")) {
491 if (!strcmp(argv[a], "-q")) {
492 setup_type = UI_SILENT;
497 /* If a setup type was not specified, try to determine automatically
498 * the best one to use out of all available types.
500 if (setup_type < 0) {
501 setup_type = discover_ui();
503 if (info_only == 1) {
504 important_message("WebCit Setup", "Welcome to WebCit setup");
508 /* Get started in a valid setup directory. */
509 strcpy(setup_directory, WEBCITDIR);
510 set_value("FIXME enter your setup directory, dumbass.",
512 if (chdir(setup_directory) != 0) {
513 important_message("WebCit Setup",
514 "The directory you specified does not exist.");
518 /* See if we need to shut down the Citadel service. */
519 for (a=0; a<=3; ++a) {
520 progress("Shutting down the WebCit service...", a, 3);
521 if (a == 0) shutdown_service();
525 /* Make sure it's stopped. */
526 if (test_server() == 0) {
527 important_message("WebCit Setup",
528 "The Citadel service is still running.\n"
529 "Please stop the service manually and run "
535 switch (setup_type) {
539 " *** WebCit setup program ***\n\n");
544 check_inittab_entry(); /* Check /etc/inittab */
546 /* See if we can start the WebCit service. */
547 if (strlen(init_entry) > 0) {
548 for (a=0; a<=3; ++a) {
549 progress("Starting the WebCit service...", a, 3);
550 if (a == 0) start_the_service();
553 if (test_server() == 0) {
554 important_message("Setup finished",
555 "Setup is finished. You may now log in.");
558 important_message("Setup finished",
559 "Setup is finished, but the WebCit service "
561 "Go back and check your configuration.");
565 important_message("Setup finished",
566 "Setup is finished. You may now start the server.");