2 * install-mh.c -- initialize the nmh environment of a new user
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.
9 #include <h/mh.h> /* mh internals */
11 #include <pwd.h> /* structure for getpwuid() results */
13 static struct swit switches[] = {
28 static char *geta(void);
32 main (int argc, char **argv)
35 char *cp, *pathname, buf[BUFSIZ];
36 char *dp, **arguments, **argp;
44 setlocale(LC_ALL, "");
46 invo_name = r1bindex (argv[0], '/');
47 arguments = getarguments (invo_name, argc, argv, 0);
52 while ((dp = *argp++)) {
54 switch (smatch (++dp, switches)) {
56 ambigsw (dp, switches);
59 adios (NULL, "-%s unknown\n", dp);
62 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
63 print_help (buf, switches, 0);
66 print_version(invo_name);
78 adios (NULL, "%s is invalid argument", dp);
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.
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");
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.
101 if ((cp = getenv("MH")) && *cp != '\0')
102 defpath = path(cp, TFILE);
104 defpath = concat(mypath, "/", mh_profile, NULL);
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.
112 if (stat (defpath, &st) != NOTOK) {
117 adios (NULL, "invocation error");
119 adios (NULL, "You already have an nmh profile, use an editor to modify it");
125 if (!autof && gans ("Do you want help? ", anoyes)) {
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);
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))
150 printf ("I'm going to create the standard nmh path for you.\n");
152 cp = concat ("Do you want the standard nmh path \"",
153 mypath, "/", "Mail\"? ", NULL);
154 if (autof || gans (cp, anoyes))
158 if (gans ("Do you want a path below your login directory? ",
160 printf ("What is the path? %s/", mypath);
163 printf ("What is the whole path? /");
164 pathname = concat ("/", geta (), NULL);
170 if (chdir (pathname) == NOTOK) {
171 cp = concat ("\"", pathname, "\" doesn't exist; Create it? ", NULL);
172 if (autof || gans (cp, anoyes))
173 if (makedir (pathname) == 0)
174 adios (NULL, "unable to create %s", pathname);
176 printf ("[Using existing directory]\n");
180 * Add some initial elements to the profile/context list
182 m_defs = (struct node *) mh_xmalloc (sizeof *np);
184 np->n_name = getcpy ("Path");
185 np->n_field = getcpy (pathname);
190 * If there is a default profile file in the
191 * nmh `etc' directory, then read it also.
193 if ((in = fopen (mh_defaults, "r"))) {
194 readconfig (&np->n_next, in, mh_defaults, 0);
198 ctxpath = getcpy (m_maildir (context = "context"));
200 /* Initialize current folder to default */
201 context_replace (pfolder, defaultfolder);
205 * Now write out the initial .mh_profile
207 if ((out = fopen (defpath, "w")) == NULL)
208 adios (defpath, "unable to write");
209 for (np = m_defs; np; np = np->n_next) {
211 fprintf (out, "%s: %s\n", np->n_name, np->n_field);
223 static char line[BUFSIZ];
226 if (fgets(line, sizeof(line), stdin) == NULL)
228 if ((cp = strchr(line, '\n')))