1/*
2 * newuser.c - handler for easy setup for new zsh users
3 *
4 * This file is part of zsh, the Z shell.
5 *
6 * Copyright (c) 2005 Peter Stephenson
7 * All rights reserved.
8 *
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and to distribute modified versions of this software for any
12 * purpose, provided that the above copyright notice and the following
13 * two paragraphs appear in all copies of this software.
14 *
15 * In no event shall Peter Stephenson or the Zsh Development Group be liable
16 * to any party for direct, indirect, special, incidental, or consequential
17 * damages arising out of the use of this software and its documentation,
18 * even if Peter Stephenson and the Zsh Development Group have been advised of
19 * the possibility of such damage.
20 *
21 * Peter Stephenson and the Zsh Development Group specifically disclaim any
22 * warranties, including, but not limited to, the implied warranties of
23 * merchantability and fitness for a particular purpose.  The software
24 * provided hereunder is on an "as is" basis, and Peter Stephenson and the
25 * Zsh Development Group have no obligation to provide maintenance,
26 * support, updates, enhancements, or modifications.
27 *
28 */
29
30#include "newuser.mdh"
31#include "newuser.pro"
32
33#include "../zshpaths.h"
34
35/**/
36int
37setup_(UNUSED(Module m))
38{
39    return 0;
40}
41
42/**/
43int
44features_(Module m, char ***features)
45{
46    return 1;
47}
48
49/**/
50int
51enables_(Module m, int **enables)
52{
53    return 0;
54}
55
56/**/
57static int
58check_dotfile(const char *dotdir, const char *fname)
59{
60    VARARR(char, buf, strlen(dotdir) + strlen(fname) + 2);
61    sprintf(buf, "%s/%s", dotdir, fname);
62
63    return access(buf, F_OK);
64}
65
66/**/
67int
68boot_(UNUSED(Module m))
69{
70    const char *dotdir = getsparam("ZDOTDIR");
71    const char *spaths[] = {
72#ifdef SITESCRIPT_DIR
73	SITESCRIPT_DIR,
74#endif
75#ifdef SCRIPT_DIR
76	SCRIPT_DIR,
77#endif
78	0 };
79    const char **sp;
80
81    if (!EMULATION(EMULATE_ZSH))
82	return 0;
83
84    if (!dotdir) {
85	dotdir = home;
86	if (!dotdir)
87	    return 0;
88    }
89
90    if (check_dotfile(dotdir, ".zshenv") == 0 ||
91	check_dotfile(dotdir, ".zprofile") == 0 ||
92	check_dotfile(dotdir, ".zshrc") == 0 ||
93	check_dotfile(dotdir, ".zlogin") == 0)
94	return 0;
95
96    for (sp = spaths; *sp; sp++) {
97	VARARR(char, buf, strlen(*sp) + 9);
98	sprintf(buf, "%s/newuser", *sp);
99
100	if (source(buf) != SOURCE_NOT_FOUND)
101	    break;
102    }
103
104    return 0;
105}
106
107/**/
108int
109cleanup_(UNUSED(Module m))
110{
111    return 0;
112}
113
114/**/
115int
116finish_(UNUSED(Module m))
117{
118    return 0;
119}
120