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