]>
diplodocus.org Git - nmh/blob - uip/install-mh.c
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.
8 #include <h/mh.h> /* mh internals */
10 #include <pwd.h> /* structure for getpwuid() results */
12 #define INSTALLMH_SWITCHES \
13 X("auto", 0, AUTOSW) \
14 X("version", 0, VERSIONSW) \
15 X("help", 0, HELPSW) \
16 X("check", 1, CHECKSW) \
18 #define X(sw, minchars, id) id,
19 DEFINE_SWITCH_ENUM(INSTALLMH
);
22 #define X(sw, minchars, id) { sw, minchars, id },
23 DEFINE_SWITCH_ARRAY(INSTALLMH
, switches
);
28 main (int argc
, char **argv
)
31 char *cp
, buf
[BUFSIZ
];
33 char *dp
, **arguments
, **argp
;
40 if (nmh_init(argv
[0], 0 /* use context_foil() */ )) { return 1; }
42 arguments
= getarguments (invo_name
, argc
, argv
, 0);
47 while ((dp
= *argp
++)) {
49 switch (smatch (++dp
, switches
)) {
51 ambigsw (dp
, switches
);
54 adios (NULL
, "-%s unknown\n", dp
);
57 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
58 print_help (buf
, switches
, 0);
61 print_version(invo_name
);
73 adios (NULL
, "%s is invalid argument", dp
);
78 * Find user's home directory. Try the HOME environment variable first,
79 * the home directory field in the password file if that's not found.
82 if ((mypath
= getenv("HOME")) == NULL
) {
83 if ((pw
= getpwuid(getuid())) == NULL
|| *pw
->pw_dir
== '\0')
84 adios(NULL
, "cannot determine your home directory");
90 * Find the user's profile. Check for the existence of an MH environment
91 * variable first with non-empty contents. Convert any relative path name
92 * found there to an absolute one. Look for the profile in the user's home
93 * directory if the MH environment variable isn't set.
96 if ((cp
= getenv("MH")) && *cp
!= '\0')
97 defpath
= path(cp
, TFILE
);
99 defpath
= concat(mypath
, "/", mh_profile
, NULL
);
102 * Check for the existence of the profile file. It's an error if it exists and
103 * this isn't an installation check. An installation check fails if it does not
104 * exist, succeeds if it does.
107 if (stat (defpath
, &st
) != NOTOK
) {
112 adios (NULL
, "invocation error");
114 adios (NULL
, "You already have an nmh profile, use an editor to modify it");
120 if (!autof
&& read_switch ("Do you want help? ", anoyes
)) {
123 "Prior to using nmh, it is necessary to have a file in your login\n"
124 "directory (%s) named %s which contains information\n"
125 "to direct certain nmh operations. The only item which is required\n"
126 "is the path to use for all nmh folder operations. The suggested nmh\n"
127 "path for you is %s/Mail...\n"
128 "\n", mypath
, mh_profile
, mypath
);
131 cp
= concat (mypath
, "/", "Mail", NULL
);
132 if (stat (cp
, &st
) != NOTOK
) {
133 if (S_ISDIR(st
.st_mode
)) {
134 cp
= concat ("You already have the standard nmh directory \"",
135 cp
, "\".\nDo you want to use it for nmh? ", NULL
);
136 if (read_switch (cp
, anoyes
))
145 puts("I'm going to create the standard nmh path for you.");
147 cp
= concat ("Do you want the standard nmh path \"",
148 mypath
, "/", "Mail\"? ", NULL
);
149 if (autof
|| read_switch (cp
, anoyes
))
153 if (read_switch ("Do you want a path below your login directory? ",
155 printf ("What is the path? %s/", mypath
);
156 pathname
= read_line ();
157 if (pathname
== NULL
) done (1);
159 printf ("What is the whole path? /");
160 pathname
= read_line ();
161 if (pathname
== NULL
) done (1);
162 pathname
= concat ("/", pathname
, NULL
);
167 if (chdir (mypath
) < 0) {
168 advise (mypath
, "chdir");
170 if (chdir (pathname
) == NOTOK
) {
171 cp
= concat ("\"", pathname
, "\" doesn't exist; Create it? ", NULL
);
172 if (autof
|| read_switch (cp
, anoyes
))
173 if (makedir (pathname
) == 0)
174 adios (NULL
, "unable to create %s", pathname
);
176 puts("[Using existing directory]");
180 * Add some initial elements to the profile/context list
184 np
->n_name
= mh_xstrdup("Path");
185 np
->n_field
= mh_xstrdup(pathname
);
190 * If there is a default profile file in the
191 * nmh `etc' directory, then read it also.
193 if ((in
= fopen (mh_defaults
, "r"))) {
194 readconfig (&np
->n_next
, in
, mh_defaults
, 0);
198 ctxpath
= getcpy (m_maildir (context
= "context"));
200 /* Initialize current folder to default */
201 context_replace (pfolder
, defaultfolder
);
205 * Now write out the initial .mh_profile
207 if ((out
= fopen (defpath
, "w")) == NULL
)
208 adios (defpath
, "unable to write");
210 * The main purpose of this first line is to fool file(1).
211 * Without it, if the first line of the profile is Path:,
212 * file 5.19 reports its type as message/news. With it,
213 * it reports the type as text/plain.
215 fprintf (out
, "MH-Profile-Version: 1.0\n");
216 for (np
= m_defs
; np
; np
= np
->n_next
) {
218 fprintf (out
, "%s: %s\n", np
->n_name
, np
->n_field
);
222 puts ("\nPlease see the nmh(7) man page for an introduction to nmh.\n");
223 print_intro (stdout
, FALSE
);
225 /* Initialize the saved nmh version. The Path profile entry was added
226 above, that's all this needs. */
227 (void) nmh_version_changed (0);