* move to config-header similar to citserver.
[citadel.git] / webcit / setup.c
index 105b615194911c156b60893dcdb3e83285c732f1..f1166e78cb86eb12d4756d262394dae956b931af 100644 (file)
@@ -7,20 +7,14 @@
  *
  */
 
-#include "config.h"
+#include "sysdep.h"
 #include "webcit.h"
 #include "webserver.h"
 
 
-#ifdef HAVE_NEWT
-#include <newt.h>
-#endif
-
-
 #define UI_TEXT                0       /* Default setup type -- text only */
 #define UI_DIALOG      2       /* Use the 'dialog' program */
 #define UI_SILENT      3       /* Silent running, for use in scripts */
-#define UI_NEWT                4       /* Use the "newt" window library */
 
 int setup_type;
 char setup_directory[SIZ];
@@ -129,11 +123,6 @@ void delete_the_old_way(void) {
 
 void cleanup(int exitcode)
 {
-#ifdef HAVE_NEWT
-       newtCls();
-       newtRefresh();
-       newtFinished();
-#endif
        exit(exitcode);
 }
 
@@ -148,13 +137,9 @@ void title(char *text)
 
 
 
-int yesno(char *question)
+
+int yesno(char *question, int default_value)
 {
-#ifdef HAVE_NEWT
-       newtComponent form = NULL;
-       newtComponent yesbutton = NULL;
-       newtComponent nobutton = NULL;
-#endif
        int i = 0;
        int answer = 0;
        char buf[SIZ];
@@ -163,10 +148,15 @@ int yesno(char *question)
 
        case UI_TEXT:
                do {
-                       printf("%s\nYes/No --> ", question);
+                       printf("%s\nYes/No [%s] --> ",
+                               question,
+                               ( default_value ? "Yes" : "No" )
+                       );
                        fgets(buf, sizeof buf, stdin);
                        answer = tolower(buf[0]);
-                       if (answer == 'y')
+                       if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
+                               answer = default_value;
+                       else if (answer == 'y')
                                answer = 1;
                        else if (answer == 'n')
                                answer = 0;
@@ -174,8 +164,9 @@ int yesno(char *question)
                break;
 
        case UI_DIALOG:
-               sprintf(buf, "exec %s --yesno '%s' 10 72",
+               sprintf(buf, "exec %s %s --yesno '%s' 15 75",
                        getenv("CTDL_DIALOG"),
+                       ( default_value ? "" : "--defaultno" ),
                        question);
                i = system(buf);
                if (i == 0) {
@@ -186,41 +177,15 @@ int yesno(char *question)
                }
                break;
 
-#ifdef HAVE_NEWT
-       case UI_NEWT:
-               newtCenteredWindow(76, 10, "Question");
-               form = newtForm(NULL, NULL, 0);
-               for (i=0; i<num_tokens(question, '\n'); ++i) {
-                       extract_token(buf, question, i, '\n', sizeof buf);
-                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
-               }
-               yesbutton = newtButton(10, 5, "Yes");
-               nobutton = newtButton(60, 5, "No");
-               newtFormAddComponent(form, yesbutton);
-               newtFormAddComponent(form, nobutton);
-               if (newtRunForm(form) == yesbutton) {
-                       answer = 1;
-               }
-               else {
-                       answer = 0;
-               }
-               newtPopWindow();
-               newtFormDestroy(form);  
-
-               break;
-#endif
-
        }
        return (answer);
 }
 
+
+
+
 void set_value(char *prompt, char str[])
 {
-#ifdef HAVE_NEWT
-       newtComponent form;
-       char *result;
-       int i;
-#endif
        char buf[SIZ];
        char dialog_result[PATH_MAX];
        char setupmsg[SIZ];
@@ -259,34 +224,12 @@ void set_value(char *prompt, char str[])
                }
                break;
 
-#ifdef HAVE_NEWT
-       case UI_NEWT:
-
-               newtCenteredWindow(76, 10, "WebCit setup");
-               form = newtForm(NULL, NULL, 0);
-               for (i=0; i<num_tokens(prompt, '\n'); ++i) {
-                       extract_token(buf, prompt, i, '\n', sizeof buf);
-                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
-               }
-               newtFormAddComponent(form, newtEntry(1, 8, str, 74, (const char **) &result,
-                                       NEWT_FLAG_RETURNEXIT));
-               newtRunForm(form);
-               strcpy(str, result);
-
-               newtPopWindow();
-               newtFormDestroy(form);  
-
-#endif
        }
 }
 
 
 void important_message(char *title, char *msgtext)
 {
-#ifdef HAVE_NEWT
-       newtComponent form = NULL;
-       int i = 0;
-#endif
        char buf[SIZ];
 
        switch (setup_type) {
@@ -304,22 +247,6 @@ void important_message(char *title, char *msgtext)
                        msgtext);
                system(buf);
                break;
-
-#ifdef HAVE_NEWT
-       case UI_NEWT:
-               newtCenteredWindow(76, 10, title);
-               form = newtForm(NULL, NULL, 0);
-               for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
-                       extract_token(buf, msgtext, i, '\n', sizeof buf);
-                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
-               }
-               newtFormAddComponent(form, newtButton(35, 5, "OK"));
-               newtRunForm(form);
-               newtPopWindow();
-               newtFormDestroy(form);  
-               break;
-#endif
-
        }
 }
 
@@ -331,16 +258,6 @@ void display_error(char *error_message)
 
 void progress(char *text, long int curr, long int cmax)
 {
-#ifdef HAVE_NEWT
-
-       /* These variables are static because progress() gets called
-        * multiple times during the course of whatever operation is
-        * being performed.  This makes setup non-threadsafe, but who
-        * cares?
-        */
-       static newtComponent form = NULL;
-       static newtComponent scale = NULL;
-#endif
        static long dots_printed = 0L;
        long a = 0;
        char buf[SIZ];
@@ -396,29 +313,6 @@ void progress(char *text, long int curr, long int cmax)
                        }
                }
                break;
-
-#ifdef HAVE_NEWT
-       case UI_NEWT:
-               if (curr == 0) {
-                       newtCenteredWindow(76, 8, text);
-                       form = newtForm(NULL, NULL, 0);
-                       scale = newtScale(1, 3, 74, cmax);
-                       newtFormAddComponent(form, scale);
-                       newtDrawForm(form);
-                       newtRefresh();
-               }
-               if ((curr > 0) && (curr <= cmax)) {
-                       newtScaleSet(scale, curr);
-                       newtRefresh();
-               }
-               if (curr == cmax) {
-                       newtFormDestroy(form);  
-                       newtPopWindow();
-                       newtRefresh();
-               }
-               break;
-#endif
-
        }
 }
 
@@ -426,35 +320,94 @@ void progress(char *text, long int curr, long int cmax)
 
 
 /*
- * install_init_scripts()  -- Make sure "webserver" is in /etc/inittab
+ * install_init_scripts()  -- Create and deploy SysV init scripts.
  *
  */
 void install_init_scripts(void)
 {
-       char question[SIZ];
+       char question[1024];
+       char buf[256];
        char http_port[128];
 #ifdef HAVE_OPENSSL
        char https_port[128];
 #endif
        char hostname[128];
        char portname[128];
+       char command[SIZ];
        struct utsname my_utsname;
-
+       struct stat etcinitd;
        FILE *fp;
+       char *initfile = "/etc/init.d/webcit";
+
+       fp = fopen(initfile, "r");
+       if (fp != NULL) {
+               if (yesno("WebCit already appears to be configured to start at boot.\n"
+                  "Would you like to keep your boot configuration as is?\n", 1) == 1) {
+                       return;
+               }
+               fclose(fp);
+               
+       }
 
        /* Otherwise, prompt the user to create an entry. */
        snprintf(question, sizeof question,
                "Would you like to automatically start WebCit at boot?"
        );
-       if (yesno(question) == 0)
+       if (yesno(question, 1) == 0)
                return;
 
+       /* Defaults */
+       sprintf(http_port, "2000");
+#ifdef HAVE_OPENSSL
+       sprintf(https_port, "443");
+#endif
+       sprintf(hostname, "uds");
+       sprintf(portname, "/usr/local/citadel");
+
+       /* This is a very hackish way of learning the port numbers used
+        * in a previous install, if we are upgrading: read them out of
+        * the existing init script.
+        */
+       if ((stat("/etc/init.d/", &etcinitd) == -1) && 
+           (errno == ENOENT))
+       {
+               if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
+                   (errno == ENOENT))
+                       initfile = WEBCITDIR"/webcit.init";
+               else
+                       initfile = "/etc/rc.d/init.d/webcit";
+       }
+
+       fp = fopen(initfile, "r");
+       if (fp != NULL) {
+               while (fgets(buf, sizeof buf, fp) != NULL) {
+                       if (strlen(buf) > 0) {
+                               buf[strlen(buf)-1] = 0; /* strip trailing cr */
+                       }
+                       if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
+                               safestrncpy(http_port, &buf[10], sizeof http_port);
+                       }
+#ifdef HAVE_OPENSSL
+                       if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
+                               safestrncpy(https_port, &buf[11], sizeof https_port);
+                       }
+#endif
+                       if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
+                               safestrncpy(hostname, &buf[14], sizeof hostname);
+                       }
+                       if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
+                               safestrncpy(portname, &buf[14], sizeof portname);
+                       }
+               }
+               fclose(fp);
+       }
+
+       /* Now ask for the port numbers */
        snprintf(question, sizeof question,
                "On which port do you want WebCit to listen for HTTP "
                "requests?\n\nYou can use the standard port (80) if you are "
                "not running another\nweb server (such as Apache), otherwise "
                "select another port.");
-       sprintf(http_port, "80");
        set_value(question, http_port);
        uname(&my_utsname);
        sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
@@ -465,7 +418,6 @@ void install_init_scripts(void)
                "requests?\n\nYou can use the standard port (443) if you are "
                "not running another\nweb server (such as Apache), otherwise "
                "select another port.");
-       sprintf(https_port, "443");
        set_value(question, https_port);
 #endif
 
@@ -477,14 +429,14 @@ void install_init_scripts(void)
        else {
                snprintf(question, sizeof question,
                        "Is the Citadel service running on the same host as WebCit?");
-               if (yesno(question)) {
-                       sprintf(hostname, "uds");
-                       sprintf(portname, "/usr/local/citadel");
+               if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
+                       strcpy(hostname, "uds");
+                       if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
                        set_value("In what directory is Citadel installed?", portname);
                }
                else {
-                       sprintf(hostname, "127.0.0.1");
-                       sprintf(portname, "504");
+                       if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
+                       if (atoi(portname) == 0) strcpy(portname, "504");
                        set_value("Enter the host name or IP address of your "
                                "Citadel server.", hostname);
                        set_value("Enter the port number on which Citadel is "
@@ -493,11 +445,7 @@ void install_init_scripts(void)
        }
 
 
-       fp = fopen("/etc/init.d/webcit", "w");
-       if (fp == NULL) {
-               display_error("Cannot create /etc/init.d/webcit");
-               return;
-       }
+       fp = fopen(initfile, "w");
 
        fprintf(fp,     "#!/bin/sh\n"
                        "\n"
@@ -509,15 +457,12 @@ void install_init_scripts(void)
        fprintf(fp,     "CTDL_HOSTNAME=%s\n", hostname);
        fprintf(fp,     "CTDL_PORTNAME=%s\n", portname);
        fprintf(fp,     "\n"
-                       "test -x $WEBCIT_DIR/ctdlsvc || exit 0\n"
-                       "test -d /var/run || exit 0\n"
                        "\n"
                        "case \"$1\" in\n"
                        "\n"
                        "start)         echo -n \"Starting WebCit... \"\n"
-                       "               if $WEBCIT_DIR/ctdlsvc /var/run/webcit.pid "
-                                                       "$WEBCIT_DIR/webserver "
-                                                       "-t/dev/null "
+                       "               if   $WEBCIT_DIR/webserver "
+                                                       "-D/var/run/webcit.pid "
                                                        "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
                        "               then\n"
                        "                       echo \"ok\"\n"
@@ -526,9 +471,8 @@ void install_init_scripts(void)
                        "               fi\n");
 #ifdef HAVE_OPENSSL
        fprintf(fp,     "               echo -n \"Starting WebCit SSL... \"\n"
-                       "               if $WEBCIT_DIR/ctdlsvc /var/run/webcit-ssl.pid "
-                                                       "$WEBCIT_DIR/webserver "
-                                                       "-t/dev/null "
+                       "               if  $WEBCIT_DIR/webserver "
+                                                       "-D/var/run/webcit-ssl.pid "
                                                        "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
                        "               then\n"
                        "                       echo \"ok\"\n"
@@ -566,12 +510,14 @@ void install_init_scripts(void)
        );
 
        fclose(fp);
-       chmod("/etc/init.d/webcit", 0755);
+       chmod(initfile, 0755);
 
        /* Set up the run levels. */
        system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
-       system("for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/S84webcit ; done 2>/dev/null");
-       system("for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/K15webcit ; done 2>/dev/null");
+       snprintf(command, sizeof(command), "for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/S84webcit ; done 2>/dev/null", initfile);
+       system(command);
+       snprintf(command, sizeof(command), "for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/K15webcit ; done 2>/dev/null", initfile);
+       system(command);
 
 }
 
@@ -589,12 +535,6 @@ int discover_ui(void)
                return UI_DIALOG;
        }
                
-#ifdef HAVE_NEWT
-       newtInit();
-       newtCls();
-       newtDrawRootText(0, 0, "WebCit Setup");
-       return UI_NEWT;
-#endif
        return UI_TEXT;
 }
 
@@ -645,7 +585,7 @@ int main(int argc, char *argv[])
        }
 
        /* Get started in a valid setup directory. */
-       strcpy(setup_directory, PREFIX);
+       strcpy(setup_directory, WEBCITDIR);
        if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
                strcpy(setup_directory, getenv("WEBCIT"));
        }