]>
diplodocus.org Git - nmh/blob - uip/install-mh.c
8fab5d86babc390ef783187f7ebbdadb6dad31e1
1 /* install-mh.c -- initialize the nmh environment of a new user
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/readconfig.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
17 #include "sbr/m_maildir.h"
18 #include "sbr/makedir.h"
20 #include "sbr/read_line.h"
22 #define INSTALLMH_SWITCHES \
23 X("auto", 0, AUTOSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
26 X("check", 1, CHECKSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(INSTALLMH
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(INSTALLMH
, switches
);
38 main (int argc
, char **argv
)
41 char *cp
, buf
[BUFSIZ
];
43 char *dp
, **arguments
, **argp
;
50 if (nmh_init(argv
[0], false, false)) { return 1; }
52 arguments
= getarguments (invo_name
, argc
, argv
, 0);
57 while ((dp
= *argp
++)) {
59 switch (smatch (++dp
, switches
)) {
61 ambigsw (dp
, switches
);
64 die("-%s unknown\n", dp
);
67 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
68 print_help (buf
, switches
, 0);
71 print_version(invo_name
);
83 die("%s is invalid argument", dp
);
88 * Find user's home directory. Try the HOME environment variable first,
89 * the home directory field in the password file if that's not found.
92 if ((mypath
= getenv("HOME")) == NULL
) {
93 if ((pw
= getpwuid(getuid())) == NULL
|| *pw
->pw_dir
== '\0')
94 die("cannot determine your home directory");
99 * Find the user's profile. Check for the existence of an MH environment
100 * variable first with non-empty contents. Convert any relative path name
101 * found there to an absolute one. Look for the profile in the user's home
102 * directory if the MH environment variable isn't set.
105 if ((cp
= getenv("MH")) && *cp
!= '\0')
106 defpath
= path(cp
, TFILE
);
108 defpath
= concat(mypath
, "/", mh_profile
, NULL
);
111 * Check for the existence of the profile file. It's an error if it exists and
112 * this isn't an installation check. An installation check fails if it does not
113 * exist, succeeds if it does.
116 if (stat (defpath
, &st
) != NOTOK
) {
120 die("invocation error");
121 die("You already have an nmh profile, use an editor to modify it");
126 if (!autof
&& read_switch ("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
))
141 cp
= concat ("You already have the standard nmh directory \"",
142 cp
, "\".\nDo you want to use it for nmh? ", NULL
);
143 if (!read_switch (cp
, anoyes
))
148 puts("I'm going to create the standard nmh path for you.");
150 cp
= concat ("Do you want the standard nmh path \"",
151 mypath
, "/", "Mail\"? ", NULL
);
152 if (autof
|| read_switch (cp
, anoyes
))
156 if (read_switch ("Do you want a path below your login directory? ",
158 printf ("What is the path? %s/", mypath
);
159 pathname
= read_line ();
160 if (pathname
== NULL
) done (1);
162 fputs("What is the whole path? /", stdout
);
163 pathname
= read_line ();
164 if (pathname
== NULL
) done (1);
165 pathname
= concat ("/", pathname
, NULL
);
170 if (chdir (mypath
) < 0) {
171 advise (mypath
, "chdir");
173 if (chdir (pathname
) == NOTOK
) {
174 cp
= concat ("\"", pathname
, "\" doesn't exist; Create it? ", NULL
);
175 if (autof
|| read_switch (cp
, anoyes
))
176 if (makedir (pathname
) == 0)
177 die("unable to create %s", pathname
);
179 puts("[Using existing directory]");
183 * Add some initial elements to the profile/context list
187 np
->n_name
= mh_xstrdup("Path");
188 np
->n_field
= mh_xstrdup(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
= mh_xstrdup(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");
213 * The main purpose of this first line is to fool file(1).
214 * Without it, if the first line of the profile is Path:,
215 * file 5.19 reports its type as message/news. With it,
216 * it reports the type as text/plain.
218 fprintf (out
, "MH-Profile-Version: 1.0\n");
219 for (np
= m_defs
; np
; np
= np
->n_next
) {
221 fprintf (out
, "%s: %s\n", np
->n_name
, np
->n_field
);
225 puts ("\nPlease see nmh(7) for an introduction to nmh.\n");
226 print_intro (stdout
, false);
228 /* Initialize the saved nmh version. The Path profile entry was added
229 above, that's all this needs. */
230 (void) nmh_version_changed (0);