2 * install-mh.c -- initialize the nmh environment of a new user
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
11 #include <h/mh.h> /* mh internals */
13 #include <pwd.h> /* structure for getpwuid() results */
15 static struct swit switches[] = {
30 static char *geta(void);
34 main (int argc, char **argv)
37 char *cp, *pathname, buf[BUFSIZ];
38 char *dp, **arguments, **argp;
46 setlocale(LC_ALL, "");
48 invo_name = r1bindex (argv[0], '/');
49 arguments = getarguments (invo_name, argc, argv, 0);
54 while ((dp = *argp++)) {
56 switch (smatch (++dp, switches)) {
58 ambigsw (dp, switches);
61 adios (NULL, "-%s unknown\n", dp);
64 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
65 print_help (buf, switches, 0);
68 print_version(invo_name);
80 adios (NULL, "%s is invalid argument", dp);
85 * Find user's home directory. Try the HOME environment variable first,
86 * the home directory field in the password file if that's not found.
89 if ((mypath = getenv("HOME")) == (char *)0) {
90 if ((pw = getpwuid(getuid())) == (struct passwd *)0 || *pw->pw_dir == '\0')
91 adios(NULL, "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) {
119 adios (NULL, "invocation error");
121 adios (NULL, "You already have an nmh profile, use an editor to modify it");
127 if (!autof && gans ("Do you want help? ", anoyes)) {
130 "Prior to using nmh, it is necessary to have a file in your login\n"
131 "directory (%s) named %s which contains information\n"
132 "to direct certain nmh operations. The only item which is required\n"
133 "is the path to use for all nmh folder operations. The suggested nmh\n"
134 "path for you is %s/Mail...\n"
135 "\n", mypath, mh_profile, mypath);
138 cp = concat (mypath, "/", "Mail", NULL);
139 if (stat (cp, &st) != NOTOK) {
140 if (S_ISDIR(st.st_mode)) {
141 cp = concat ("You already have the standard nmh directory \"",
142 cp, "\".\nDo you want to use it for nmh? ", NULL);
143 if (gans (cp, anoyes))
152 printf ("I'm going to create the standard nmh path for you.\n");
154 cp = concat ("Do you want the standard nmh path \"",
155 mypath, "/", "Mail\"? ", NULL);
156 if (autof || gans (cp, anoyes))
160 if (gans ("Do you want a path below your login directory? ",
162 printf ("What is the path? %s/", mypath);
165 printf ("What is the whole path? /");
166 pathname = concat ("/", geta (), NULL);
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);
178 printf ("[Using existing directory]\n");
182 * Add some initial elements to the profile/context list
184 m_defs = (struct node *) mh_xmalloc (sizeof *np);
186 np->n_name = getcpy ("Path");
187 np->n_field = getcpy (pathname);
192 * If there is a default profile file in the
193 * nmh `etc' directory, then read it also.
195 if ((in = fopen (mh_defaults, "r"))) {
196 readconfig (&np->n_next, in, mh_defaults, 0);
200 ctxpath = getcpy (m_maildir (context = "context"));
202 /* Initialize current folder to default */
203 context_replace (pfolder, defaultfolder);
207 * Now write out the initial .mh_profile
209 if ((out = fopen (defpath, "w")) == NULL)
210 adios (defpath, "unable to write");
211 for (np = m_defs; np; np = np->n_next) {
213 fprintf (out, "%s: %s\n", np->n_name, np->n_field);
225 static char line[BUFSIZ];
228 if (fgets(line, sizeof(line), stdin) == NULL)
230 if ((cp = strchr(line, '\n')))