]>
diplodocus.org Git - nmh/blob - uip/install-mh.c
1 /* install-mh.c -- initialize the nmh environment of a new user
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
15 #include "sbr/m_maildir.h"
16 #include "sbr/makedir.h"
18 #include "sbr/read_line.h"
20 #define INSTALLMH_SWITCHES \
21 X("auto", 0, AUTOSW) \
22 X("version", 0, VERSIONSW) \
23 X("help", 0, HELPSW) \
24 X("check", 1, CHECKSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(INSTALLMH
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(INSTALLMH
, switches
);
36 main (int argc
, char **argv
)
39 char *cp
, buf
[BUFSIZ
];
41 char *dp
, **arguments
, **argp
;
48 if (nmh_init(argv
[0], false, false)) { return 1; }
50 arguments
= getarguments (invo_name
, argc
, argv
, 0);
55 while ((dp
= *argp
++)) {
57 switch (smatch (++dp
, switches
)) {
59 ambigsw (dp
, switches
);
62 die("-%s unknown\n", dp
);
65 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
66 print_help (buf
, switches
, 0);
69 print_version(invo_name
);
81 die("%s is invalid argument", dp
);
86 * Find user's home directory. Try the HOME environment variable first,
87 * the home directory field in the password file if that's not found.
90 if ((mypath
= getenv("HOME")) == NULL
) {
91 if ((pw
= getpwuid(getuid())) == NULL
|| *pw
->pw_dir
== '\0')
92 die("cannot determine your home directory");
97 * Find the user's profile. Check for the existence of an MH environment
98 * variable first with non-empty contents. Convert any relative path name
99 * found there to an absolute one. Look for the profile in the user's home
100 * directory if the MH environment variable isn't set.
103 if ((cp
= getenv("MH")) && *cp
!= '\0')
104 defpath
= path(cp
, TFILE
);
106 defpath
= concat(mypath
, "/", mh_profile
, NULL
);
109 * Check for the existence of the profile file. It's an error if it exists and
110 * this isn't an installation check. An installation check fails if it does not
111 * exist, succeeds if it does.
114 if (stat (defpath
, &st
) != NOTOK
) {
118 die("invocation error");
119 die("You already have an nmh profile, use an editor to modify it");
124 if (!autof
&& read_switch ("Do you want help? ", anoyes
)) {
127 "Prior to using nmh, it is necessary to have a file in your login\n"
128 "directory (%s) named %s which contains information\n"
129 "to direct certain nmh operations. The only item which is required\n"
130 "is the path to use for all nmh folder operations. The suggested nmh\n"
131 "path for you is %s/Mail...\n"
132 "\n", mypath
, mh_profile
, mypath
);
135 cp
= concat (mypath
, "/", "Mail", NULL
);
136 if (stat (cp
, &st
) != NOTOK
) {
137 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 (!read_switch (cp
, anoyes
))
146 puts("I'm going to create the standard nmh path for you.");
148 cp
= concat ("Do you want the standard nmh path \"",
149 mypath
, "/", "Mail\"? ", NULL
);
150 if (autof
|| read_switch (cp
, anoyes
))
154 if (read_switch ("Do you want a path below your login directory? ",
156 printf ("What is the path? %s/", mypath
);
157 pathname
= read_line ();
158 if (pathname
== NULL
) done (1);
160 fputs("What is the whole path? /", stdout
);
161 pathname
= read_line ();
162 if (pathname
== NULL
) done (1);
163 pathname
= concat ("/", pathname
, NULL
);
168 if (chdir (mypath
) < 0) {
169 advise (mypath
, "chdir");
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 die("unable to create %s", pathname
);
177 puts("[Using existing directory]");
181 * Add some initial elements to the profile/context list
185 np
->n_name
= mh_xstrdup("Path");
186 np
->n_field
= mh_xstrdup(pathname
);
191 * If there is a default profile file in the
192 * nmh `etc' directory, then read it also.
194 if ((in
= fopen (mh_defaults
, "r"))) {
195 readconfig (&np
->n_next
, in
, mh_defaults
, 0);
199 ctxpath
= mh_xstrdup(m_maildir(context
= "context"));
201 /* Initialize current folder to default */
202 context_replace (pfolder
, defaultfolder
);
206 * Now write out the initial .mh_profile
208 if ((out
= fopen (defpath
, "w")) == NULL
)
209 adios (defpath
, "unable to write");
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.
216 fprintf (out
, "MH-Profile-Version: 1.0\n");
217 for (np
= m_defs
; np
; np
= np
->n_next
) {
219 fprintf (out
, "%s: %s\n", np
->n_name
, np
->n_field
);
223 puts ("\nPlease see nmh(7) for an introduction to nmh.\n");
224 print_intro (stdout
, false);
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);