-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstow.sh
executable file
·224 lines (185 loc) · 4.7 KB
/
stow.sh
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
216
217
218
219
220
221
222
223
224
#!/bin/bash
# define ANSI escape codes for custom colors (everforest)
white='\033[38;5;231m' # #d3c6aa
black='\033[38;5;235m' # #272e33
gray='\033[38;5;242m' # #859289
red='\033[38;5;203m' # #e67e80
green='\033[38;5;113m' # #a7c080
yellow='\033[38;5;223m' # #dbbc7f
blue='\033[38;5;73m' # #7fbbb3
purple='\033[38;5;176m' # #d699b6
aqua='\033[38;5;71m' # #83c092
orange='\033[38;5;215m' # #e69875
reset='\033[0m'
# arguments:
# a string
# color code
# "n" meaning, print newline
# returns:
# none
# outputs:
# input string with colors
printc() {
if [ "$3" = "n" ]; then
# add newline
echo -e "$2$1$reset"
else
# don't add newline
echo -ne "$2$1$reset"
fi
}
print() {
printc "$1" "$white"
}
printn() {
printc "$1" "$white" "n"
}
usage() {
if [ "$1" = 0 ] || [ "$1" = "error" ]; then
printc " Usage:" "$orange" "n"
printc "\t$0 [option]" "$yellow" "n"
echo
printc " Options:" "$orange" "n"
printc "\tsetup\t setup symlinks using stow" "$yellow" "n"
printc "\tremove\t remove symlinks created by stow" "$yellow" "n"
exit 1
fi
}
# arguments:
# a directory name (stow module name)
# path to find the module
# returns:
# 10 if successfull
# 11 if not successfull
take_backup() {
local dir_name="$1"
local directory="$2"
if [ -L "$directory/$dir_name" ]; then
printc "\`$dir_name\`" "$red"
print " is a symlink in "
printc "$directory" "$orange" "n"
print "Removing symlink of "
printc "$directory/$dir_name" "$orange" "n"
rm -v "$directory/$dir_name"
sleep 0.5
return 10
elif [ -d "$directory/$dir_name" ]; then
print "Directory "
printc "\`$dir_name\`" "$red"
printn " exists in $directory/ . Taking backup..."
print "Moving "
printc "$directory/$dir_name" "$orange"
print " to "
printc "$directory/dotfiles_backup/" "$orange" "n"
mkdir -p "$directory/dotfiles_backup"
local timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
local new_dir_name="${dir_name}_${timestamp}"
mv -vfu "$directory/$dir_name" "$directory/dotfiles_backup/$new_dir_name"
sleep 0.5
return 10
else
print "Directory "
printc "\`$dir_name\`" "$red"
print " does not exist in "
printc "$directory/" "$orange"
printn ". Skipping backup..."
sleep 0.5
return 11
fi
}
# arguments:
# a directory name (stow module name)
# returns:
# none
stow_symlink() {
local module="$1"
stow -t ~ "$module" -v
stow_exit_status=$?
if [ "$stow_exit_status" != 0 ]; then
echo
echo "Stow setup failed."
exit 1
fi
}
# check if the number of arguments is correct
usage $#
# parse the first argument
action="$1"
# perform actions based on the parameter
case "$action" in
"setup")
for dir in */; do
if [ -d "$dir" ]; then
dir_name=$(basename "$dir")
# TODO: make the directory finding more dynamic
# TODO: need to handle files too
if [ "$dir_name" = "assets" ]; then
take_backup "wallpaper" "$HOME/.config"
echo
take_backup "fonts" "$HOME/.local/share"
echo
take_backup ".themes" "$HOME"
echo
stow_symlink "$dir_name"
elif [ "$dir_name" = "konsole" ]; then
take_backup "$dir_name" "$HOME/.local/share"
stow_symlink "$dir_name"
elif [ "$dir_name" = "home_scripts" ]; then
take_backup ".bin" "$HOME"
stow_symlink "$dir_name"
elif [ "$dir_name" = "utility" ]; then
take_backup "betterlockscreen" "$HOME/.config"
echo
take_backup "btop" "$HOME/.config"
echo
take_backup "dmenu" "$HOME/.config"
echo
take_backup "felix" "$HOME/.config"
echo
take_backup "handlr" "$HOME/.config"
echo
take_backup "helix" "$HOME/.config"
echo
take_backup "macchina" "$HOME/.config"
echo
take_backup "networkmanager-dmenu" "$HOME/.config"
echo
take_backup "qBittorrent" "$HOME/.config"
echo
take_backup "rofi" "$HOME/.config"
echo
take_backup "slock" "$HOME/.config"
echo
take_backup "zathura" "$HOME/.config"
echo
stow_symlink "$dir_name"
else
take_backup "$dir_name" "$HOME/.config"
stow_symlink "$dir_name"
fi
fi
echo
done
;;
"remove")
# remove stow symlinks
for dir in */; do
if [ -d "$dir" ]; then
module=$(basename "$dir")
stow -t ~ -D "$module" -v
stow_exit_status=$?
if [ "$stow_exit_status" != 0 ]; then
echo
printc "Failed to remove stow symlinks..." "$red" "n"
exit 1
fi
fi
done
;;
*)
echo
printc "Invalid action: $action" "$red" "n"
usage "error"
exit 1
;;
esac