]> diplodocus.org Git - nmh/blob - uip/install-mh.c
Another pass at cleaning up (some of) the manpages.
[nmh] / uip / install-mh.c
1 /*
2 * install-mh.c -- initialize the nmh environment of a new user
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h> /* mh internals */
10 #include <h/utils.h>
11 #include <pwd.h> /* structure for getpwuid() results */
12
13 #define INSTALLMH_SWITCHES \
14 X("auto", 0, AUTOSW) \
15 X("version", 0, VERSIONSW) \
16 X("help", 0, HELPSW) \
17 X("check", 1, CHECKSW) \
18
19 #define X(sw, minchars, id) id,
20 DEFINE_SWITCH_ENUM(INSTALLMH);
21 #undef X
22
23 #define X(sw, minchars, id) { sw, minchars, id },
24 DEFINE_SWITCH_ARRAY(INSTALLMH, switches);
25 #undef X
26
27 /*
28 * static prototypes
29 */
30 static char *geta(void);
31
32
33 int
34 main (int argc, char **argv)
35 {
36 int autof = 0;
37 char *cp, *pathname, buf[BUFSIZ];
38 char *dp, **arguments, **argp;
39 struct node *np;
40 struct passwd *pw;
41 struct stat st;
42 FILE *in, *out;
43 int check;
44
45 if (nmh_init(argv[0], 0 /* use context_foil() */ )) { return 1; }
46
47 arguments = getarguments (invo_name, argc, argv, 0);
48 argp = arguments;
49
50 check = 0;
51
52 while ((dp = *argp++)) {
53 if (*dp == '-') {
54 switch (smatch (++dp, switches)) {
55 case AMBIGSW:
56 ambigsw (dp, switches);
57 done (1);
58 case UNKWNSW:
59 adios (NULL, "-%s unknown\n", dp);
60
61 case HELPSW:
62 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
63 print_help (buf, switches, 0);
64 done (0);
65 case VERSIONSW:
66 print_version(invo_name);
67 done (0);
68
69 case AUTOSW:
70 autof++;
71 continue;
72
73 case CHECKSW:
74 check = 1;
75 continue;
76 }
77 } else {
78 adios (NULL, "%s is invalid argument", dp);
79 }
80 }
81
82 /*
83 * Find user's home directory. Try the HOME environment variable first,
84 * the home directory field in the password file if that's not found.
85 */
86
87 if ((mypath = getenv("HOME")) == (char *)0) {
88 if ((pw = getpwuid(getuid())) == (struct passwd *)0 || *pw->pw_dir == '\0')
89 adios(NULL, "cannot determine your home directory");
90 else
91 mypath = pw->pw_dir;
92 }
93
94 /*
95 * Find the user's profile. Check for the existence of an MH environment
96 * variable first with non-empty contents. Convert any relative path name
97 * found there to an absolute one. Look for the profile in the user's home
98 * directory if the MH environment variable isn't set.
99 */
100
101 if ((cp = getenv("MH")) && *cp != '\0')
102 defpath = path(cp, TFILE);
103 else
104 defpath = concat(mypath, "/", mh_profile, NULL);
105
106 /*
107 * Check for the existence of the profile file. It's an error if it exists and
108 * this isn't an installation check. An installation check fails if it does not
109 * exist, succeeds if it does.
110 */
111
112 if (stat (defpath, &st) != NOTOK) {
113 if (check)
114 done(0);
115
116 else if (autof)
117 adios (NULL, "invocation error");
118 else
119 adios (NULL, "You already have an nmh profile, use an editor to modify it");
120 }
121 else if (check) {
122 done(1);
123 }
124
125 if (!autof && gans ("Do you want help? ", anoyes)) {
126 (void)printf(
127 "\n"
128 "Prior to using nmh, it is necessary to have a file in your login\n"
129 "directory (%s) named %s which contains information\n"
130 "to direct certain nmh operations. The only item which is required\n"
131 "is the path to use for all nmh folder operations. The suggested nmh\n"
132 "path for you is %s/Mail...\n"
133 "\n", mypath, mh_profile, mypath);
134 }
135
136 cp = concat (mypath, "/", "Mail", NULL);
137 if (stat (cp, &st) != NOTOK) {
138 if (S_ISDIR(st.st_mode)) {
139 cp = concat ("You already have the standard nmh directory \"",
140 cp, "\".\nDo you want to use it for nmh? ", NULL);
141 if (gans (cp, anoyes))
142 pathname = "Mail";
143 else
144 goto query;
145 } else {
146 goto query;
147 }
148 } else {
149 if (autof)
150 printf ("I'm going to create the standard nmh path for you.\n");
151 else
152 cp = concat ("Do you want the standard nmh path \"",
153 mypath, "/", "Mail\"? ", NULL);
154 if (autof || gans (cp, anoyes))
155 pathname = "Mail";
156 else {
157 query:
158 if (gans ("Do you want a path below your login directory? ",
159 anoyes)) {
160 printf ("What is the path? %s/", mypath);
161 pathname = geta ();
162 } else {
163 printf ("What is the whole path? /");
164 pathname = concat ("/", geta (), NULL);
165 }
166 }
167 }
168
169 if (chdir (mypath) < 0) {
170 advise (mypath, "chdir");
171 }
172 if (chdir (pathname) == NOTOK) {
173 cp = concat ("\"", pathname, "\" doesn't exist; Create it? ", NULL);
174 if (autof || gans (cp, anoyes))
175 if (makedir (pathname) == 0)
176 adios (NULL, "unable to create %s", pathname);
177 } else {
178 printf ("[Using existing directory]\n");
179 }
180
181 /*
182 * Add some initial elements to the profile/context list
183 */
184 m_defs = (struct node *) mh_xmalloc (sizeof *np);
185 np = m_defs;
186 np->n_name = getcpy ("Path");
187 np->n_field = getcpy (pathname);
188 np->n_context = 0;
189 np->n_next = NULL;
190
191 /*
192 * If there is a default profile file in the
193 * nmh `etc' directory, then read it also.
194 */
195 if ((in = fopen (mh_defaults, "r"))) {
196 readconfig (&np->n_next, in, mh_defaults, 0);
197 fclose (in);
198 }
199
200 ctxpath = getcpy (m_maildir (context = "context"));
201
202 /* Initialize current folder to default */
203 context_replace (pfolder, defaultfolder);
204 context_save ();
205
206 /*
207 * Now write out the initial .mh_profile
208 */
209 if ((out = fopen (defpath, "w")) == NULL)
210 adios (defpath, "unable to write");
211 /*
212 * The main purpose of this first line is to fool file(1).
213 * Without it, if the first line of the profile is Path:,
214 * file 5.19 reports its type as message/news. With it,
215 * it reports the type as text/plain.
216 */
217 fprintf (out, "MH-Profile-Version: 1.0\n");
218 for (np = m_defs; np; np = np->n_next) {
219 if (!np->n_context)
220 fprintf (out, "%s: %s\n", np->n_name, np->n_field);
221 }
222 fclose (out);
223 done (0);
224 return 1;
225 }
226
227
228 static char *
229 geta (void)
230 {
231 char *cp;
232 static char line[BUFSIZ];
233
234 fflush(stdout);
235 if (fgets(line, sizeof(line), stdin) == NULL)
236 done (1);
237 if ((cp = strchr(line, '\n')))
238 *cp = 0;
239 return line;
240 }