-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
init.zsh
executable file
·144 lines (121 loc) · 3.36 KB
/
init.zsh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env -S zsh -df
#
# Symlinks files in this directory into $HOME directory
zmodload zsh/stat
# Base dir as path relative to $HOME directory
DIR="${0:a:h}"
function msg() {
case "$1" in
ERROR) print -Pn '%F{red}' ;;
SKIP) print -Pn '%F{yellow}' ;;
OK) print -Pn '%F{green}' ;;
esac
[[ -n "$2" ]] && print -n "[$1: $2]" || print -n "[$1]"
print -P %f
}
function symlink() {
local error symlink lnflags
local src dest
# ln flags change on different platforms
[[ "$OSTYPE" = darwin* ]] && lnflags="-nsfh" || lnflags="-nsfT"
src="$1"
dest="$2"
if [[ -h "$dest" ]]; then
symlink="$(zstat +link "$dest")"
if [[ "$symlink" == "$src" ]]; then
msg SKIP "file already symlinked"
continue
else
echo -n "(old@ -> $symlink)"
fi
fi
mkdir -p "${dest:h}"
if error="$(ln $lnflags "$src" "$dest" 2>&1)"; then
msg OK
elif [[ -f "$dest" ]]; then
msg ERROR "destination already exists (file)"
elif [[ -d "$dest" ]]; then
msg ERROR "destination already exists (dir)"
else
msg ERROR "$error"
fi
}
local ZDOT=".zsh"
# Files to be symlinked to home directory
local -A dotfiles
dotfiles=(
aliases "${ZDOT}/aliases"
dircolors "${ZDOT}/dircolors"
functions "${ZDOT}/functions"
fxrc.js ".fxrc.js"
git ".config/git"
ohmyzsh "${ZDOT}/ohmyzsh"
ohmyzsh-custom "${ZDOT}/ohmyzsh-custom"
tigrc ".config/tig/config"
tmux.conf ".tmux.conf"
toprc ".config/procps/toprc"
vimrc ".vimrc"
zprofile "${ZDOT}/.zprofile"
zshenv "${ZDOT}/.zshenv"
zshrc "${ZDOT}/.zshrc"
)
local file src dest
for file (${(ko)dotfiles}); do
src="$DIR/$file"
dest="$HOME/${dotfiles[$file]}"
echo -n "Linking $file... "
symlink "$src" "$dest"
done
# Files specific to the platform
local system
case "$OSTYPE" in
darwin*) system=darwin ;;
linux-android) [[ "$PREFIX" = *com.termux* ]] && system=termux ;;
linux*) system=linux ;;
esac
local -A platform
platform=(
nano/darwin/nanorc ".nanorc"
nano/termux/nanorc ".nanorc"
nano/others/nanorc ".config/nano/nanorc"
)
local -aU areas files
areas=(${(ko)platform%%/*})
local area
for area (${(o)areas}); do
# Select the files based on the system
files=(${(k)platform[(I)$area/$system/*]})
# If no match for $system, select others
(( $#files )) || files=(${(k)platform[(I)$area/others/*]})
# If others not found, go to next area
(( $#files )) || continue
echo "Setting up $area:"
for file (${(o)files}); do
src="$DIR/$file"
dest="$HOME/${platform[$file]}"
echo -n "Linking $file... "
symlink "$src" "$dest"
done
done
# Add ZDOTDIR change to ~/.zshenv
grep -q "ZDOTDIR=\"\$HOME/${ZDOT}\"" "$HOME/.zshenv" 2>/dev/null || {
echo -n "setting up ZDOTDIR in ~/.zshenv... "
# .zshenv might be a symlink, so remove it first
rm -f ~/.zshenv
cat > ~/.zshenv <<EOF
ZDOTDIR="\$HOME/${ZDOT}"
. "\$ZDOTDIR/.zshenv"
EOF
msg OK
}
# Add sourcing of .zshenv to .profile
grep -q "\. \"\$HOME/${dotfiles[zshenv]}\"" ~/.profile 2>/dev/null || {
[[ -s ~/.profile ]] && echo >> ~/.profile || touch ~/.profile
cat >> ~/.profile <<EOF
# load posix-compatible .zshenv
if [ -r "\$HOME/${dotfiles[zshenv]}" ]; then
. "\$HOME/${dotfiles[zshenv]}"
fi
EOF
}
mkdir -p "$HOME/.zsh/completions"