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 */
12 #include <pwd.h> /* structure for getpwuid() results */
14 static struct swit switches[] = {
29 static char *geta(void);
33 main (int argc, char **argv)
36 char *cp, *pathname, buf[BUFSIZ];
37 char *dp, **arguments, **argp;
45 setlocale(LC_ALL, "");
47 invo_name = r1bindex (argv[0], '/');
48 arguments = getarguments (invo_name, argc, argv, 0);
53 while ((dp = *argp++)) {
55 switch (smatch (++dp, switches)) {
57 ambigsw (dp, switches);
60 adios (NULL, "-%s unknown\n", dp);
63 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
64 print_help (buf, switches, 0);
67 print_version(invo_name);
79 adios (NULL, "%s is invalid argument", dp);
84 * Find user's home directory. Try the HOME environment variable first,
85 * the home directory field in the password file if that's not found.
88 if ((mypath = getenv("HOME")) == (char *)0) {
89 if ((pw = getpwuid(getuid())) == (struct passwd *)0 || *pw->pw_dir == '\0')
90 adios(NULL, "cannot determine your home directory");
96 * Find the user's profile. Check for the existence of an MH environment
97 * variable first with non-empty contents. Convert any relative path name
98 * found there to an absolute one. Look for the profile in the user's home
99 * directory if the MH environment variable isn't set.
102 if ((cp = getenv("MH")) && *cp != '\0')
103 defpath = path(cp, TFILE);
105 defpath = concat(mypath, "/", mh_profile, NULL);
108 * Check for the existence of the profile file. It's an error if it exists and
109 * this isn't an installation check. An installation check fails if it does not
110 * exist, succeeds if it does.
113 if (stat (defpath, &st) != NOTOK) {
118 adios (NULL, "invocation error");
120 adios (NULL, "You already have an nmh profile, use an editor to modify it");
126 if (!autof && gans ("Do you want help? ", anoyes)) {
129 "Prior to using nmh, it is necessary to have a file in your login\n"
130 "directory (%s) named %s which contains information\n"
131 "to direct certain nmh operations. The only item which is required\n"
132 "is the path to use for all nmh folder operations. The suggested nmh\n"
133 "path for you is %s/Mail...\n"
134 "\n", mypath, mh_profile, mypath);
137 cp = concat (mypath, "/", "Mail", NULL);
138 if (stat (cp, &st) != NOTOK) {
139 if (S_ISDIR(st.st_mode)) {
140 cp = concat ("You already have the standard nmh directory \"",
141 cp, "\".\nDo you want to use it for nmh? ", NULL);
142 if (gans (cp, anoyes))
151 printf ("I'm going to create the standard nmh path for you.\n");
153 cp = concat ("Do you want the standard nmh path \"",
154 mypath, "/", "Mail\"? ", NULL);
155 if (autof || gans (cp, anoyes))
159 if (gans ("Do you want a path below your login directory? ",
161 printf ("What is the path? %s/", mypath);
164 printf ("What is the whole path? /");
165 pathname = concat ("/", geta (), NULL);
171 if (chdir (pathname) == NOTOK) {
172 cp = concat ("\"", pathname, "\" doesn't exist; Create it? ", NULL);
173 if (autof || gans (cp, anoyes))
174 if (makedir (pathname) == 0)
175 adios (NULL, "unable to create %s", pathname);
177 printf ("[Using existing directory]\n");
181 * Add some initial elements to the profile/context list
183 if (!(m_defs = (struct node *) malloc (sizeof *np)))
184 adios (NULL, "unable to allocate profile storage");
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);
224 static char line[BUFSIZ];
227 if (fgets(line, sizeof(line), stdin) == NULL)
229 if ((cp = strchr(line, '\n')))