1: /* install-mh.c - initialize the MH environment */ 2: 3: #include "../h/mh.h" 4: #include <pwd.h> 5: #include <stdio.h> 6: #include <sys/types.h> 7: #include <sys/stat.h> 8: 9: /* */ 10: 11: static char *message[] = { 12: "Prior to using MH, it is necessary to have a file in your login", 13: "directory (%s) named %s which contains information", 14: "to direct certain MH operations. The only item which is required", 15: "is the path to use for all MH folder operations. The suggested MH", 16: "path for you is %s/Mail...", 17: NULL 18: }; 19: 20: 21: char *geta (); 22: 23: struct passwd *getpwuid (); 24: 25: /* */ 26: 27: /* ARGSUSED */ 28: 29: main (argc, argv) 30: int argc; 31: char **argv; 32: { 33: int autof, 34: i; 35: char *cp, 36: *path; 37: struct node *np; 38: struct passwd *pw; 39: struct stat st; 40: FILE *in, 41: *out; 42: 43: invo_name = r1bindex (argv[0], '/'); 44: 45: #ifdef COMPAT 46: if (argc == 2 && strcmp (argv[1], "-compat") == 0) { 47: context = "/dev/null"; /* hack past m_getdefs() */ 48: 49: m_getdefs (); 50: for (np = m_defs; np; np = np -> n_next) 51: if (uleq (pfolder, np -> n_name) 52: || ssequal ("atr-", np -> n_name) 53: || ssequal ("cur-", np -> n_name)) 54: np -> n_context = 1; 55: 56: ctxpath = getcpy (m_maildir (context = "context")); 57: ctxflags |= CTXMOD; 58: m_update (); 59: 60: if ((out = fopen (defpath, "w")) == NULL) 61: adios (defpath, "unable to write"); 62: for (np = m_defs; np; np = np -> n_next) 63: if (!np -> n_context) 64: fprintf (out, "%s: %s\n", np -> n_name, np -> n_field); 65: (void) fclose (out); 66: 67: done (0); 68: } 69: #endif COMPAT 70: 71: autof = (argc == 2 && strcmp (argv[1], "-auto") == 0); 72: if (mypath == NULL) { /* straight from m_getdefs... */ 73: if (mypath = getenv ("HOME")) 74: mypath = getcpy (mypath); 75: else 76: if ((pw = getpwuid (getuid ())) == NULL 77: || pw -> pw_dir == NULL 78: || *pw -> pw_dir == NULL) 79: adios (NULLCP, "no HOME envariable"); 80: else 81: mypath = getcpy (pw -> pw_dir); 82: if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/') 83: *cp = NULL; 84: } 85: defpath = concat (mypath, "/", mh_profile, NULLCP); 86: 87: if (stat (defpath, &st) != NOTOK) 88: if (autof) 89: adios (NULLCP, "invocation error"); 90: else 91: adios (NULLCP, 92: "You already have an MH profile, use an editor to modify it"); 93: 94: if (!autof && gans ("Do you want help? ", anoyes)) { 95: (void) putchar ('\n'); 96: for (i = 0; message[i]; i++) { 97: printf (message[i], mypath, mh_profile); 98: (void) putchar ('\n'); 99: } 100: (void) putchar ('\n'); 101: } 102: 103: /* */ 104: 105: cp = concat (mypath, "/", "Mail", NULLCP); 106: if (stat (cp, &st) != NOTOK) { 107: if ((st.st_mode & S_IFMT) == S_IFDIR) { 108: cp = concat ("You already have the standard MH directory \"", 109: cp, "\".\nDo you want to use it for MH? ", NULLCP); 110: if (gans (cp, anoyes)) 111: path = "Mail"; 112: else 113: goto query; 114: } 115: else 116: goto query; 117: } 118: else { 119: if (autof) 120: printf ("I'm going to create the standard MH path for you.\n"); 121: else 122: cp = concat ("Do you want the standard MH path \"", 123: mypath, "/", "Mail\"? ", NULLCP); 124: if (autof || gans (cp, anoyes)) 125: path = "Mail"; 126: else { 127: query: ; 128: if (gans ("Do you want a path below your login directory? ", 129: anoyes)) { 130: printf ("What is the path? %s/", mypath); 131: path = geta (); 132: } 133: else { 134: printf ("What is the whole path? /"); 135: path = concat ("/", geta (), NULLCP); 136: } 137: } 138: } 139: 140: (void) chdir (mypath); 141: if (chdir (path) == NOTOK) { 142: cp = concat ("\"", path, "\" doesn't exist; Create it? ", NULLCP); 143: if (autof || gans (cp, anoyes)) 144: if (makedir (path) == 0) 145: adios (NULLCP, "unable to create %s", path); 146: } 147: else 148: printf ("[Using existing directory]\n"); 149: 150: /* */ 151: 152: np = m_defs = (struct node *) malloc (sizeof *np); 153: if (np == NULL) 154: adios (NULLCP, "unable to allocate profile storage"); 155: np -> n_name = getcpy ("Path"); 156: np -> n_field = getcpy (path); 157: np -> n_context = 0; 158: np -> n_next = NULL; 159: 160: if (in = fopen (mh_defaults, "r")) { 161: m_readefs (&np -> n_next, in, mh_defaults, 0); 162: (void) fclose (in); 163: } 164: 165: ctxpath = getcpy (m_maildir (context = "context")); 166: m_replace (pfolder, defalt); 167: m_update (); 168: 169: if ((out = fopen (defpath, "w")) == NULL) 170: adios (defpath, "unable to write"); 171: for (np = m_defs; np; np = np -> n_next) 172: if (!np -> n_context) 173: fprintf (out, "%s: %s\n", np -> n_name, np -> n_field); 174: (void) fclose (out); 175: 176: done (0); 177: } 178: 179: /* */ 180: 181: static char *geta () { 182: register char *cp; 183: static char line[BUFSIZ]; 184: 185: (void) fflush (stdout); 186: if (fgets (line, sizeof line, stdin) == NULL) 187: done (1); 188: if (cp = index (line, '\n')) 189: *cp = NULL; 190: return line; 191: }