3 * install-mh.c -- initialize the nmh environment of a new user
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 static struct swit switches[] = {
25 static char *message[] = {
26 "Prior to using nmh, it is necessary to have a file in your login",
27 "directory (%s) named %s which contains information",
28 "to direct certain nmh operations. The only item which is required",
29 "is the path to use for all nmh folder operations. The suggested nmh",
30 "path for you is %s/Mail...",
37 static char *geta(void);
41 main (int argc, char **argv)
44 char *cp, *path, buf[BUFSIZ];
45 char *dp, **arguments, **argp;
52 setlocale(LC_ALL, "");
54 invo_name = r1bindex (argv[0], '/');
55 arguments = getarguments (invo_name, argc, argv, 0);
58 while ((dp = *argp++)) {
60 switch (smatch (++dp, switches)) {
62 ambigsw (dp, switches);
65 adios (NULL, "-%s unknown\n", dp);
68 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
69 print_help (buf, switches, 0);
72 print_version(invo_name);
80 adios (NULL, "%s is invalid argument", dp);
84 /* straight from context_read ... */
86 if ((mypath = getenv ("HOME"))) {
87 mypath = getcpy (mypath);
89 if ((pw = getpwuid (getuid ())) == NULL
92 adios (NULL, "no HOME envariable");
94 mypath = getcpy (pw->pw_dir);
96 if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/')
99 defpath = concat (mypath, "/", mh_profile, NULL);
101 if (stat (defpath, &st) != NOTOK) {
103 adios (NULL, "invocation error");
106 "You already have an nmh profile, use an editor to modify it");
109 if (!autof && gans ("Do you want help? ", anoyes)) {
111 for (i = 0; message[i]; i++) {
112 printf (message[i], mypath, mh_profile);
118 cp = concat (mypath, "/", "Mail", NULL);
119 if (stat (cp, &st) != NOTOK) {
120 if (S_ISDIR(st.st_mode)) {
121 cp = concat ("You already have the standard nmh directory \"",
122 cp, "\".\nDo you want to use it for nmh? ", NULL);
123 if (gans (cp, anoyes))
132 printf ("I'm going to create the standard nmh path for you.\n");
134 cp = concat ("Do you want the standard nmh path \"",
135 mypath, "/", "Mail\"? ", NULL);
136 if (autof || gans (cp, anoyes))
140 if (gans ("Do you want a path below your login directory? ",
142 printf ("What is the path? %s/", mypath);
145 printf ("What is the whole path? /");
146 path = concat ("/", geta (), NULL);
152 if (chdir (path) == NOTOK) {
153 cp = concat ("\"", path, "\" doesn't exist; Create it? ", NULL);
154 if (autof || gans (cp, anoyes))
155 if (makedir (path) == 0)
156 adios (NULL, "unable to create %s", path);
158 printf ("[Using existing directory]\n");
162 * Add some initial elements to the profile/context list
164 if (!(m_defs = (struct node *) malloc (sizeof *np)))
165 adios (NULL, "unable to allocate profile storage");
167 np->n_name = getcpy ("Path");
168 np->n_field = getcpy (path);
173 * If there is a default profile file in the
174 * nmh `etc' directory, then read it also.
176 if ((in = fopen (mh_defaults, "r"))) {
177 readconfig (&np->n_next, in, mh_defaults, 0);
181 ctxpath = getcpy (m_maildir (context = "context"));
183 /* Initialize current folder to default */
184 context_replace (pfolder, defaultfolder);
188 * Now write out the initial .mh_profile
190 if ((out = fopen (defpath, "w")) == NULL)
191 adios (defpath, "unable to write");
192 for (np = m_defs; np; np = np->n_next) {
194 fprintf (out, "%s: %s\n", np->n_name, np->n_field);
205 static char line[BUFSIZ];
208 if (fgets(line, sizeof(line), stdin) == NULL)
210 if ((cp = strchr(line, '\n')))