-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·230 lines (192 loc) · 5.76 KB
/
install.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
225
226
227
228
229
230
#!/usr/bin/env bash
# Install script for dotfiles
# Define usage function
usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -a, --add Add symlinks"
echo " -r, --remove Remove symlinks"
echo " -l, --list List available configs"
echo " -n, --no-prompt Create links without confirmation"
echo " -b, --no-backup Do not create backups of existing configs"
echo " -v, --verbose Enable verbose mode"
echo " -h, --help Show this help message"
exit 1
}
# Check if no arguments were provided
if [ $# -eq 0 ]; then
usage
# Optionally, provide more detailed usage information here
exit 1
fi
BACKUP=1
NO_PROMPT=0
VERBOSE=0
# Parse command line options
while getopts ":arlbnvh" opt; do
case "$opt" in
a)
MODE=add
;;
r)
MODE=remove
;;
l)
MODE=list
;;
b)
BACKUP=0
;;
n)
NO_PROMPT=1
;;
o)
output_file="$OPTARG"
;;
v)
VERBOSE=1
;;
h)
usage
;;
:)
echo "Option -$OPTARG requires an argument."
usage
;;
\?)
echo "Invalid option: -$OPTARG"
usage
;;
*)
usaage
;;
esac
done
# Enable dot file matching
shopt -s dotglob
# Home directory, set this manually if needed
HOME_DIR="$(echo $HOME)"
# Config Directory
if [ -n "$XDG_CONFIG_HOME" ]; then
CONFIG_DIR="$XDG_CONFIG_HOME"
else
CONFIG_DIR="$HOME_DIR/.config"
fi
# Check if the home folder exists.
if [ ! -d "$HOME_DIR" ]; then
echo "Home folder does not exist: $HOME_DIR"
exit 1
fi
# Check if the source folder exists.
if [ ! -d "$CONFIG_DIR" ]; then
echo "Config folder does not exist: $CONFIG_DIR"
exit 1
fi
verbose_echo(){
if [ "$VERBOSE" == "1" ];then
echo $1
fi
}
link_all_files(){
source=$1
target=$2
# Loop through files in the source folder.
for file in "$source"/*; do
# Extract the file name without the path.
filename=$(basename "$file")
if [ "$filename" != ".config" ];then
source_file=$(realpath $file)
target_file="$target/$filename"
if [ "$NO_PROMPT" == "0" ]; then
echo -n "Add $filename? [Y/n] "
read -n 1 -r answer
answer=${answer:-y} # Set default to 'y' if no input is provided
if [ "$answer" == "n" ] || [ "$answer" == "N" ]; then
echo
continue
fi
fi
local backed_up
# Backup the original, criteria is backup enabled, target exists and is not symlink to source
if [ "$BACKUP" == "1" ] && { ([ -e "$target_file" ] && [ ! -L "$target_file" ]) || ([ -L "$target_file" ] && [ "$(readlink "$target_file")" != "$source_file" ]); }; then
backed_up="(backup created)"
mv "$target_file" "$target_file-bkp"
fi
# Create a symlink in the target folder.
ln -s "$source_file" "$target/$filename"
verbose_echo "Installed: $filename $backed_up"
fi
done
}
unlink_all_files(){
source=$1
target=$2
# Loop through files in the source folder.
for file in "$source"/*; do
# Extract the file name without the path.
filename=$(basename "$file")
if [ "$filename" != ".config" ];then
source_file=$(realpath $file)
target_file="$target/$filename"
if [ "$NO_PROMPT" == "0" ]; then
echo -n "Remove $filename? [Y/n] "
read -n 1 -r answer
answer=${answer:-y} # Set default to 'y' if no input is provided
if [ "$answer" == "n" ] || [ "$answer" == "N" ]; then
echo
continue
fi
fi
# Remove only links if symlink matches
if [ -e "$target_file" ] && [ -L "$target_file" ] && [ "$(readlink "$target_file")" == "$source_file" ]; then
rm "$target_file"
if [ -e $target_file-bkp ]; then
mv "$target_file-bkp" "$target_file"
verbose_echo "Config removed: $filename. Backup Restored."
else
verbose_echo "Config removed: $filename"
fi
fi
fi
done
}
list(){
source=$1
target=$2
echo Path: \'$target\'
# Loop through files in the source folder.
for file in "$source"/*; do
# Extract the file name without the path.
filename=$(basename "$file")
if [ "$filename" != ".config" ];then
source_file=$(realpath $file)
target_file="$target/$filename"
if [ -e "$target_file" ]; then
if [ ! -L "$target_file" ] || ([ -L "$target_file" ] && [ "$(readlink "$target_file")" != "$source_file" ]); then
echo "$filename : Installed (conflicting)."
else
if [ -e $target_file-bkp ]; then
echo "$filename : Installed (backup exists)."
else
echo "$filename : Installed."
fi
fi
else
echo "$filename : Not installed."
fi
fi
done
echo
}
if [ "$MODE" == "add" ]; then
link_all_files "./dot-files" $HOME_DIR
link_all_files "./dot-files/.config" $CONFIG_DIR
elif [ "$MODE" == "remove" ]; then
unlink_all_files "./dot-files" $HOME_DIR
unlink_all_files "./dot-files/.config" $CONFIG_DIR
elif [ "$MODE" == "list" ]; then
list "./dot-files" $HOME_DIR
list "./dot-files/.config" $CONFIG_DIR
fi
# Disable dot file matching
shopt -s dotglob