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