summaryrefslogtreecommitdiff
path: root/zshrc
blob: d87187bf26901d9ddb515b283659930e63e43a0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
##
## THIS FILE IS NOT INTENDED TO BE MODIFIED ! READ ABOVE...
##
#
# Instead, add/edit your configuration files inside $ZDOTDIR.
#
# If you want to add a file, name it specifically in the form
#
#	$ZDOTDIR/??_*.zsh
#
# Where "??" should be a two-digit number.
# With that, file ``10_Toto.zsh'' would be parsed before
# file ``20_Tutu.zsh'', allowing you ordering your scripts.
#
# If you want to make user, host or network specific configurations,
# add your specific scripts to the folders
#  - "sys:$(uname -s)" for OS-specific conf,
#  - "user:$(whoami)" for user-specific conf,
#  - "host:$(hostname -s)" for host-specific conf,
#  - "net:$(domainname)" for network-specific conf,
# rename your scripts to the form mentionned above, minus the "??_"
# prefix. An original script prefixed by a two-digits number SHOULD
# be present on the $ZDOTDIR folder, even if empty.
#

ZDOTDIR=${ZDOTDIR:-~/.zsh}
mkdir -p $ZDOTDIR

trap "source ~/.zshrc" USR1

# Useful environment variables which may be used
# at any time - We compute them now to avoid calling
# the required processes each time we'll need.
OSNAME=`uname -s`
USER=${USER:-`whoami`}
UID=${UID:-`id -u`}
HOST=$HOST:r
HOST=${HOST:-$(hostname -s)}
DOMAIN=anevia.com # $(hostname -a 2>&-| sed 's/^[^\.]*\.\?//')
DOMAIN=${DOMAIN:-$(hostname -d 2>&-)}
DOMAIN=${DOMAIN:-$(hostname -y 2>&-)}
[ "$DOMAIN" = "" -o "$DOMAIN" = "localdomain" -o "$DOMAIN" = "(none)" ] && DOMAIN=$(grep "^search " /etc/resolv.conf | cut -d' ' -f2)

## Agent de clefs SSH/GPG
KEYCHAIN=~/.keychain/$(hostname)-sh
[ -r "${KEYCHAIN}"     ] && source ${KEYCHAIN}
[ -r "${KEYCHAIN}-gpg" ] && source ${KEYCHAIN}-gpg

__debug ()
{
    [ -n "$DEBUG" ] && echo >&2 $@
}

export USER HOST DOMAIN UID

if [ -d $ZDOTDIR ]; then

	recurse_load() {
		local script=$1
		local root=${2:-$ZDOTDIR}

		for f in \
		  user:$USER \
		  net:$DOMAIN \
		  host:$HOST \
		  sys:$OSNAME \
		  user:$SUDO_USER
		do
			if test -d $root/$f
			then
				local sub=$root/$f/$script
				test -f $sub && echo $sub
				recurse_load $script $root/$f
			fi
		done
	}

	for script in $ZDOTDIR/??_*.zsh
	do

        __debug -n "${${script:t:r}/??_/}... "
		source $script
		__debug

		for f in $(recurse_load ${${script:t}/??_/})
		do
			case ${f:e} in
				gpg)
					__debug -n "${f#$ZDOTDIR/} [CRYPTED]... "
					eval $(gpg --quiet --decrypt $f.gpg)
					__debug
					;;
				*)
					__debug -n "${f#$ZDOTDIR/}... "
					source $f
					__debug
					;;
			esac
		done
	done
fi

# For sudo shells
if [ ! -z "$SUDO_USER" ]
then
	export HOME=~$USER
	[ "`pwd`" = ~$SUDO_USER ] && cd
fi