-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-resources.zsh
executable file
·198 lines (167 loc) · 5.95 KB
/
install-resources.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env zsh
# Installs this directory's resource files, by creating symlinks to them.
function usage() {
explain "This script installs resource files from this directory, by creating symlinks to them." && echo
explain "Installing prompt_yazpt_setup is only useful if you use Prezto, or zsh's promptinit-based prompt theme system." \
"Installing Xresources is only useful if you use XTerm (and haven't already configured it yourself)."
echo "\nUsage: $script_name [options]"
echo "\nOptions:"
option "-f, --force" "Install without asking for confirmation. Without this option," \
"you'll be prompted before installing each resource file, and before replacing any existing files."
option "-h, --help" "Show this help and exit, regardless of any other options given."
exit
}
# Utilities
script_name="$(basename -- "$0")"
script_dir="$(dirname -- "$0")"
repo_dir="$(cd -- "$script_dir/.." && pwd -P)"
source "$repo_dir/functions/utils.zsh"
explanation_color='\e[38;5;152m'
question_color='\e[38;5;231m'
warning_color='\e[38;5;217m'
no_color='\e[0m'
function ask() {
echo -n "${question_color}${*}${no_color}"
read -rq REPLY
echo
[[ $REPLY == 'y' ]]
}
function ask_to_install() {
local text="$*"
[[ $force == true ]] && text+=" Installing..."
explain "$text"
if [[ $force == true ]]; then
echo
elif ! ask "\nInstall it? [y|n] "; then
echo "Okie doke, skipping installation."
return 1
fi
}
function ask_to_replace() {
local target_path="$1" description=""
if [[ -L "$target_path" ]]; then
description="Symlink"
elif [[ -e "$target_path" && ! -d "$target_path" ]]; then
description="File"
fi
if [[ -n "$description" && $force != true ]] && ! ask "${description} ${target_path} exists. Replace it? [y|n] "; then
echo "Okay then."
return 1
fi
}
function check_existing_directory() {
local target_path="$1"
if [[ -d "$target_path" ]]; then
.yazpt_print_wrapped "Error: Target $target_path already exists, as a directory"
return 1
fi
}
function explain() {
echo -n "$explanation_color"
.yazpt_print_wrapped "$*"
echo -n "$no_color"
}
function make_symbolic_link() {
local target_path="$1"
local link_path="$2"
local sudo="$3"
if [[ $OSTYPE == "msys" ]]; then
# See https://github.com/msys2/MSYS2-packages/issues/249
echo "MSYS2 doesn't support creating symbolic links with 'ln -s'."
echo "Creating a symbolic link with cmd's mklink command instead..."
# You might need to run as Administrator for this mklink command to succeed; try that
# if you get an error about "You do not have sufficient privilege to perform this operation",
# but still want a symlink instead of a copy of the file
if cmd //c mklink "$(cygpath -w "$link_path")" "$(cygpath -w "$target_path")"; then
return
elif [[ $force == true ]]; then
echo "Falling back to creating a copy..."
elif ! ask "Creating a symbolic link failed. Create a copy instead? [y|n] "; then
return 1
fi
fi
$sudo ln -sv "$target_path" "$link_path" # Creates a copy on MSYS2
}
function option() {
local opt="$1"; opt="${(r:14:)opt}"; shift
local desc="$*"
echo -n " "
.yazpt_print_wrapped "${opt}${desc}" 2
}
function warn() {
echo "${warning_color}${*}${no_color}"
}
function xrdb_merge() {
if ( [[ $OSTYPE != "darwin"* && $OS != "Windows"* && -z $WSL_DISTRO_NAME ]] ) || which xrdb > /dev/null; then
xrdb -merge "$1"
fi
}
# Parse the command line
force=false
for arg; do
if [[ $arg == "-f" || $arg == "--force" ]]; then
force=true
elif [[ $arg == "-h" || $arg == "--help" ]]; then
usage
else
warn "Invalid argument: ${arg}\n"
usage
fi
done
# Install prompt_yazpt_setup
target_dir='/usr/local/share/zsh/site-functions'
target="$target_dir/prompt_yazpt_setup"
msg="It's installed by creating a symlink in $target_dir."
if [[ ! -w $target_dir ]] && which sudo > /dev/null; then
msg+=" Doing so requires using sudo."
sudo="sudo" # Used below and in make_symbolic_link
fi
if ask_to_install "Yazpt's integration with zsh's prompt theme system (promptinit), and with the Prezto framework," \
"is enabled by the prompt_yazpt_setup file. $msg" && \
ask_to_replace "$target"
then
installed=false && \
check_existing_directory "$target" && \
attempted=true && \
$sudo rm -f "$target" && \
$sudo mkdir -pv "$target_dir" && \
make_symbolic_link "$repo_dir/resources/prompt_yazpt_setup" "$target" "$sudo" && \
installed=true || warn "An error occurred during installation."
if [[ $installed == true ]]; then
msg="Installed. Run 'autoload -U promptinit && promptinit' and then 'prompt' for more details on usage."
.yazpt_print_wrapped "$msg"
elif [[ $attempted == true && $sudo != "" ]]; then
msg="If sudo told you you're not in the sudoers file, you might try running 'su' and then running this script again."
.yazpt_print_wrapped "$msg"
fi
fi
# Install Xresources
target=~$USER/.Xresources # So it works while running under su
if [[ $OSTYPE == "linux-gnu" && -f /etc/os-release ]]; then
# ~/.Xresources doesn't work on Debian, but ~/.Xdefaults-$(hostname) does
# We match some Debian derivaties here, e.g. antiX and MX, but that's okay
if grep 'ID=debian' /etc/os-release &> /dev/null; then
target=~$USER/.Xdefaults-$(hostname)
fi
fi
echo
if ask_to_install "Using XTerm with its default settings can be a bit painful. Yazpt includes an Xresources file" \
"which makes XTerm look considerably nicer. It's installed by creating a symlink in your home directory." && \
ask_to_replace "$target"
then
installed=false && \
check_existing_directory "$target" && \
rm -f "$target" && \
make_symbolic_link "$repo_dir/resources/Xresources" "$target" "" && \
chown -h "$USER" "$target" && \
xrdb_merge "$target" && \
installed=true || warn "An error occurred during installation."
if [[ $installed == true ]]; then
msg="Installed. "
if [[ $target == *".Xresources" ]]; then
msg+="If ~/.Xresources doesn't have any effect when you start a new XTerm, "
msg+="it might help if you rename it to ~/.Xdefaults-$(hostname)."
fi
.yazpt_print_wrapped "$msg"
fi
fi