]>
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
);
30 static char *geta(void);
34 main (int argc
, char **argv
)
37 char *cp
, *pathname
, buf
[BUFSIZ
];
38 char *dp
, **arguments
, **argp
;
45 if (nmh_init(argv
[0], 0 /* use context_foil() */ )) { return 1; }
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 variable first,
84 * the home directory field in the password file if that's not found.
87 if ((mypath
= getenv("HOME")) == (char *)0) {
88 if ((pw
= getpwuid(getuid())) == (struct passwd
*)0 || *pw
->pw_dir
== '\0')
89 adios(NULL
, "cannot determine your home directory");
95 * Find the user's profile. Check for the existence of an MH environment
96 * variable first with non-empty contents. Convert any relative path name
97 * found there to an absolute one. Look for the profile in the user's home
98 * directory if the MH environment variable isn't set.
101 if ((cp
= getenv("MH")) && *cp
!= '\0')
102 defpath
= path(cp
, TFILE
);
104 defpath
= concat(mypath
, "/", mh_profile
, NULL
);
107 * Check for the existence of the profile file. It's an error if it exists and
108 * this isn't an installation check. An installation check fails if it does not
109 * exist, succeeds if it does.
112 if (stat (defpath
, &st
) != NOTOK
) {
117 adios (NULL
, "invocation error");
119 adios (NULL
, "You already have an nmh profile, use an editor to modify it");
125 if (!autof
&& gans ("Do you want help? ", anoyes
)) {
128 "Prior to using nmh, it is necessary to have a file in your login\n"
129 "directory (%s) named %s which contains information\n"
130 "to direct certain nmh operations. The only item which is required\n"
131 "is the path to use for all nmh folder operations. The suggested nmh\n"
132 "path for you is %s/Mail...\n"
133 "\n", mypath
, mh_profile
, mypath
);
136 cp
= concat (mypath
, "/", "Mail", NULL
);
137 if (stat (cp
, &st
) != NOTOK
) {
138 if (S_ISDIR(st
.st_mode
)) {
139 cp
= concat ("You already have the standard nmh directory \"",
140 cp
, "\".\nDo you want to use it for nmh? ", NULL
);
141 if (gans (cp
, anoyes
))
150 printf ("I'm going to create the standard nmh path for you.\n");
152 cp
= concat ("Do you want the standard nmh path \"",
153 mypath
, "/", "Mail\"? ", NULL
);
154 if (autof
|| gans (cp
, anoyes
))
158 if (gans ("Do you want a path below your login directory? ",
160 printf ("What is the path? %s/", mypath
);
163 printf ("What is the whole path? /");
164 pathname
= concat ("/", geta (), NULL
);
169 if (chdir (mypath
) < 0) {
170 advise (mypath
, "chdir");
172 if (chdir (pathname
) == NOTOK
) {
173 cp
= concat ("\"", pathname
, "\" doesn't exist; Create it? ", NULL
);
174 if (autof
|| gans (cp
, anoyes
))
175 if (makedir (pathname
) == 0)
176 adios (NULL
, "unable to create %s", pathname
);
178 printf ("[Using existing directory]\n");
182 * Add some initial elements to the profile/context list
184 m_defs
= (struct node
*) mh_xmalloc (sizeof *np
);
186 np
->n_name
= getcpy ("Path");
187 np
->n_field
= getcpy (pathname
);
192 * If there is a default profile file in the
193 * nmh `etc' directory, then read it also.
195 if ((in
= fopen (mh_defaults
, "r"))) {
196 readconfig (&np
->n_next
, in
, mh_defaults
, 0);
200 ctxpath
= getcpy (m_maildir (context
= "context"));
202 /* Initialize current folder to default */
203 context_replace (pfolder
, defaultfolder
);
207 * Now write out the initial .mh_profile
209 if ((out
= fopen (defpath
, "w")) == NULL
)
210 adios (defpath
, "unable to write");
212 * The main purpose of this first line is to fool file(1).
213 * Without it, if the first line of the profile is Path:,
214 * file 5.19 reports its type as message/news. With it,
215 * it reports the type as text/plain.
217 fprintf (out
, "MH-Profile-Version: 1.0\n");
218 for (np
= m_defs
; np
; np
= np
->n_next
) {
220 fprintf (out
, "%s: %s\n", np
->n_name
, np
->n_field
);
232 static char line
[BUFSIZ
];
235 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
237 if ((cp
= strchr(line
, '\n')))