]>
diplodocus.org Git - nmh/blob - uip/install-mh.c
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 #define INSTALLMH_SWITCHES \
14 X("auto", 0, AUTOSW) \
15 X("version", 0, VERSIONSW) \
16 X("help", 0, HELPSW) \
17 X("check", 1, CHECKSW) \
19 #define X(sw, minchars, id) id,
20 DEFINE_SWITCH_ENUM(INSTALLMH
);
23 #define X(sw, minchars, id) { sw, minchars, id },
24 DEFINE_SWITCH_ARRAY(INSTALLMH
, switches
);
29 main (int argc
, char **argv
)
32 char *cp
, buf
[BUFSIZ
];
34 char *dp
, **arguments
, **argp
;
41 if (nmh_init(argv
[0], 0 /* use context_foil() */ )) { return 1; }
43 arguments
= getarguments (invo_name
, argc
, argv
, 0);
48 while ((dp
= *argp
++)) {
50 switch (smatch (++dp
, switches
)) {
52 ambigsw (dp
, switches
);
55 adios (NULL
, "-%s unknown\n", dp
);
58 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
59 print_help (buf
, switches
, 0);
62 print_version(invo_name
);
74 adios (NULL
, "%s is invalid argument", dp
);
79 * Find user's home directory. Try the HOME environment variable first,
80 * the home directory field in the password file if that's not found.
83 if ((mypath
= getenv("HOME")) == NULL
) {
84 if ((pw
= getpwuid(getuid())) == NULL
|| *pw
->pw_dir
== '\0')
85 adios(NULL
, "cannot determine your home directory");
91 * Find the user's profile. Check for the existence of an MH environment
92 * variable first with non-empty contents. Convert any relative path name
93 * found there to an absolute one. Look for the profile in the user's home
94 * directory if the MH environment variable isn't set.
97 if ((cp
= getenv("MH")) && *cp
!= '\0')
98 defpath
= path(cp
, TFILE
);
100 defpath
= concat(mypath
, "/", mh_profile
, NULL
);
103 * Check for the existence of the profile file. It's an error if it exists and
104 * this isn't an installation check. An installation check fails if it does not
105 * exist, succeeds if it does.
108 if (stat (defpath
, &st
) != NOTOK
) {
113 adios (NULL
, "invocation error");
115 adios (NULL
, "You already have an nmh profile, use an editor to modify it");
121 if (!autof
&& read_switch ("Do you want help? ", anoyes
)) {
124 "Prior to using nmh, it is necessary to have a file in your login\n"
125 "directory (%s) named %s which contains information\n"
126 "to direct certain nmh operations. The only item which is required\n"
127 "is the path to use for all nmh folder operations. The suggested nmh\n"
128 "path for you is %s/Mail...\n"
129 "\n", mypath
, mh_profile
, mypath
);
132 cp
= concat (mypath
, "/", "Mail", NULL
);
133 if (stat (cp
, &st
) != NOTOK
) {
134 if (S_ISDIR(st
.st_mode
)) {
135 cp
= concat ("You already have the standard nmh directory \"",
136 cp
, "\".\nDo you want to use it for nmh? ", NULL
);
137 if (read_switch (cp
, anoyes
))
146 puts("I'm going to create the standard nmh path for you.");
148 cp
= concat ("Do you want the standard nmh path \"",
149 mypath
, "/", "Mail\"? ", NULL
);
150 if (autof
|| read_switch (cp
, anoyes
))
154 if (read_switch ("Do you want a path below your login directory? ",
156 printf ("What is the path? %s/", mypath
);
157 pathname
= read_line ();
158 if (pathname
== NULL
) done (1);
160 printf ("What is the whole path? /");
161 pathname
= read_line ();
162 if (pathname
== NULL
) done (1);
163 pathname
= concat ("/", pathname
, NULL
);
168 if (chdir (mypath
) < 0) {
169 advise (mypath
, "chdir");
171 if (chdir (pathname
) == NOTOK
) {
172 cp
= concat ("\"", pathname
, "\" doesn't exist; Create it? ", NULL
);
173 if (autof
|| read_switch (cp
, anoyes
))
174 if (makedir (pathname
) == 0)
175 adios (NULL
, "unable to create %s", pathname
);
177 puts("[Using existing directory]");
181 * Add some initial elements to the profile/context list
185 np
->n_name
= mh_xstrdup("Path");
186 np
->n_field
= mh_xstrdup(pathname
);
191 * If there is a default profile file in the
192 * nmh `etc' directory, then read it also.
194 if ((in
= fopen (mh_defaults
, "r"))) {
195 readconfig (&np
->n_next
, in
, mh_defaults
, 0);
199 ctxpath
= getcpy (m_maildir (context
= "context"));
201 /* Initialize current folder to default */
202 context_replace (pfolder
, defaultfolder
);
206 * Now write out the initial .mh_profile
208 if ((out
= fopen (defpath
, "w")) == NULL
)
209 adios (defpath
, "unable to write");
211 * The main purpose of this first line is to fool file(1).
212 * Without it, if the first line of the profile is Path:,
213 * file 5.19 reports its type as message/news. With it,
214 * it reports the type as text/plain.
216 fprintf (out
, "MH-Profile-Version: 1.0\n");
217 for (np
= m_defs
; np
; np
= np
->n_next
) {
219 fprintf (out
, "%s: %s\n", np
->n_name
, np
->n_field
);
223 puts ("\nPlease see the nmh(7) man page for an introduction to nmh.\n");
224 print_intro (stdout
, FALSE
);
226 /* Initialize the saved nmh version. The Path profile entry was added
227 above, that's all this needs. */
228 (void) nmh_version_changed (0);