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
84 ** variable first, the home directory field in the password file
85 ** if that's not found.
88 if ((mypath = getenv("HOME")) == (char *)0) {
89 if ((pw = getpwuid(getuid())) == (struct passwd *)0 ||
91 adios(NULL, "cannot determine your home directory");
97 ** Find the user's profile. Check for the existence of an
98 ** MH environment variable first with non-empty contents.
99 ** Convert any relative path name found there to an absolute one.
100 ** Look for the profile in the user's home directory if the MH
101 ** environment variable isn't set.
104 if ((cp = getenv("MH")) && *cp != '\0')
105 defpath = path(cp, TFILE);
107 defpath = concat(mypath, "/", mh_profile, NULL);
110 ** Check for the existence of the profile file. It's an
111 ** error if it exists and this isn't an installation check.
112 ** An installation check fails if it does not exist, succeeds
116 if (stat(defpath, &st) != NOTOK) {
121 adios(NULL, "invocation error");
123 adios(NULL, "You already have an nmh profile, use an editor to modify it");
128 if (!autof && gans("Do you want help? ", anoyes)) {
131 "Prior to using nmh, it is necessary to have a file in your login\n"
132 "directory (%s) named %s which contains information\n"
133 "to direct certain nmh operations. The only item which is required\n"
134 "is the path to use for all nmh folder operations. The suggested nmh\n"
135 "path for you is %s/Mail...\n"
136 "\n", mypath, mh_profile, mypath);
139 cp = concat(mypath, "/", "Mail", NULL);
140 if (stat(cp, &st) != NOTOK) {
141 if (S_ISDIR(st.st_mode)) {
142 cp = concat("You already have the standard nmh directory \"",
143 cp, "\".\nDo you want to use it for nmh? ", NULL);
144 if (gans(cp, anoyes))
153 printf("I'm going to create the standard nmh path for you.\n");
155 cp = concat("Do you want the standard nmh path \"",
156 mypath, "/", "Mail\"? ", NULL);
157 if (autof || gans(cp, anoyes))
161 if (gans("Do you want a path below your login directory? ", anoyes)) {
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? ",
175 if (autof || gans(cp, anoyes))
176 if (makedir(pathname) == 0)
177 adios(NULL, "unable to create %s", pathname);
179 printf("[Using existing directory]\n");
183 ** Add some initial elements to the profile/context list
185 m_defs = (struct node *) mh_xmalloc(sizeof *np);
187 np->n_name = getcpy("Path");
188 np->n_field = getcpy(pathname);
193 ** If there is a default profile file in the
194 ** nmh `etc' directory, then read it also.
196 if ((in = fopen(mh_defaults, "r"))) {
197 readconfig(&np->n_next, in, mh_defaults, 0);
201 ctxpath = getcpy(m_maildir(context = "context"));
203 /* Initialize current folder to default */
204 context_replace(pfolder, defaultfolder);
208 ** Now write out the initial .mh_profile
210 if ((out = fopen(defpath, "w")) == NULL)
211 adios(defpath, "unable to write");
212 for (np = m_defs; np; np = np->n_next) {
214 fprintf(out, "%s: %s\n", np->n_name, np->n_field);
226 static char line[BUFSIZ];
229 if (fgets(line, sizeof(line), stdin) == NULL)
231 if ((cp = strchr(line, '\n')))