Repository navigation
Expand file tree
/
Copy pathsetup
More file actions
executable file
·216 lines (173 loc) · 4.98 KB
/
Copy pathsetup
File metadata and controls
executable file
·216 lines (173 loc) · 4.98 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
declare -r EXECUTABLE=$(basename ${BASH_SOURCE[0]})
declare -r BASE_DIR=$(dirname ${BASH_SOURCE[0]})
declare -r BACKUP_DIR="$BASE_DIR/_backup"
declare -r OS_NAME=$(uname -s)
. utility
######## Parse args
uninstall_mode=0
no_script_mode=0
packages=()
for arg in $@; do
case $arg in
--uninstall | -u)
uninstall_mode=1
;;
--no-scripts)
no_script_mode=1
;;
--help | -h)
usage
exit 0
;;
--* | -*)
unrecognized_option $arg
exit 1
;;
*)
packages+=($arg)
;;
esac
done
######## Prepare list of packages to (un)install
# Directories starting with _ are reserved for script usage
# If no package names were supplied, use all packages by default
if [[ -z ${packages[@]} ]]; then
packages=($(find $BASE_DIR -maxdepth 1 -mindepth 1 -type d -not -path '*/[\._]*'))
else
for package in ${packages[@]}; do
package=$(basename $package)
if [[ ! -d "$BASE_DIR/$package" ]] || [[ $package == _* ]]; then
unrecognized_option $package
exit 1
fi
done
fi
for i in ${!packages[@]}; do
packages[$i]=$(basename ${packages[$i]})
done
declare -r packages
######## Attempt to pull all package submodules in case they haven't already
echo_info "Loading any submodules in the dotfiles repo"
$(cd $BASE_DIR; git submodule init)
$(cd $BASE_DIR; git submodule update)
######## Ensures brew is installed on OSX
if [[ "$OS_NAME" == Darwin ]] && ! which brew > /dev/null; then
echo_info "Installing Brew"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
######## Framework for installing packages
auto_detect_pkg_mgr() {
if which brew > /dev/null; then
eval "$1=brew"
elif which apt > /dev/null; then
eval "$1=apt"
elif which apt-get > /dev/null; then
eval "$1=apt-get"
elif which dnf > /dev/null; then
eval "$1=dnf"
elif which yum > /dev/null; then
eval "$1=yum"
elif which pacman > /dev/null; then
eval "$1=pacman"
else
echo_error "Was not able to auto-detect your package manager."
exit 6
fi
}
update_pkg_mgr_cache() {
local update_package_lists
echo -n "Update package lists before auto-installing anything (y/n): "
read update_package_lists
if [[ $update_package_lists =~ ^(y|yes)$ ]]; then
echo_warn "Updating package lists..."
if [[ $pkg_mgr == pacman || $pkg_mgr == dnf ]]; then
sudo $pkg_mgr --refresh > /dev/null 2>&1
elif [[ $pkg_mgr == brew ]]; then
brew update > /dev/null 2>&1
else
sudo $pkg_mgr update > /dev/null 2>&1
fi
fi
}
install_bin_called_once=0
install_bin() {
if [ -z $pkg_mgr ]; then
auto_detect_pkg_mgr pkg_mgr
declare -r pkg_mgr=$pkg_mgr
fi
if [[ $install_bin_called_once -eq 0 ]]; then
update_pkg_mgr_cache
install_bin_called_once=1
fi
echo_warn "Installing [$1]..."
if [[ $pkg_mgr == pacman ]]; then
sudo pacman -S $1
elif [[ $pkg_mgr == brew ]]; then
brew install $1
else
sudo $pkg_mgr install -y $1
fi
if [[ $? -gt 0 ]] && [[ $2 ]]; then
echo_error "Was not able to auto-install [$1]. Install it before running this script."
exit 7
fi
echo_success "[$1] installed"
}
######## (Un)install dotfile packages
backup() {
for package in ${packages[@]}; do
conflicts=($(stow -t $HOME -d $BASE_DIR -n $package 2>&1 | grep "existing target" | cut -d ":" -f2))
if [[ ${#conflicts[@]} -gt 0 ]]; then
echo_warn "Conflicts found for package [$package]. Moving to backup dir [$BACKUP_DIR/$package]..."
fi
for conflict in ${conflicts[@]}; do
mkdir -p $BACKUP_DIR/$package/$(dirname $conflict)
mv $HOME/$conflict $BACKUP_DIR/$package/$(dirname $conflict)
done
done
}
restore() {
for package in ${packages[@]}; do
backups=($(ls -A $BACKUP_DIR/$package 2> /dev/null))
for backup in ${backups[@]}; do
mv $BACKUP_DIR/$package/$backup $HOME
done
if [[ ${#backups[@]} -gt 0 ]]; then
rm -r $BACKUP_DIR/$package
fi
done
}
setup_sub_scripts() {
if [ $no_script_mode -eq 0 ]; then
for package in ${packages[@]}; do
if [ -f $BASE_DIR/$package/setup_ ] && [ -x $BASE_DIR/$package/setup_ ]; then
echo_info "Running setup sub script for the [$package] package"
. $BASE_DIR/$package/setup_
fi
done
fi
}
if [[ $uninstall_mode == 0 ]]; then
backup
stow -t $HOME -d $BASE_DIR --ignore="_" -R stow
stow -t $HOME -d $BASE_DIR --no-folding --ignore="_" -R ${packages[@]}
stowExitCode=$?
setup_sub_scripts
if [[ stowExitCode -gt 0 ]]; then
echo_error "Dotfiles failed to install for ${packages[*]}"
exit 10
fi
echo_success "Dotfiles installed for ${packages[*]}"
else
stow -t $HOME -d $BASE_DIR -n -D ${packages[@]} > /dev/null 2>&1
if [[ $? == 0 ]]; then
stow -t $HOME -d $BASE_DIR -D ${packages[@]} > /dev/null
if [[ $? -gt 0 ]]; then
echo_error "Dotfiles failed to uninstall ${packages[*]}"
exit 10
fi
restore
fi
echo_success "Dotfiles uninstalled for ${packages[*]}"
fi