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