-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·50 lines (48 loc) · 1.74 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
#!/usr/bin/env sh
ignore="docs,install.sh,macos.sh,LICENSE,README.md"
cutstring="DO NOT EDIT BELOW THIS LINE"
for name in *; do
target="$HOME/.$name"
# If the target exists...
if [ -e "$target" ]; then
# ... and it's not a symlink...
if [ ! -L "$target" ]; then
# ... find the cutstring in the target.
linesabove=$(grep -m 1 -n "$cutstring" "$target" | sed "s/:.*//")
# If we found the cutstring...
if [ -n "$linesabove" ]; then
echo "Updating $target"
# ... copy it and the lines above it to a temp file.
tmp=$(mktemp)
head -n "$linesabove" "$target" >"$tmp"
# Then find the cutstring in the (reversed) source.
linesbelow=$(sed '1!G;h;$!d' "$name" | grep -m 1 -n "$cutstring" | sed "s/:.*//")
# If we found the cutstring, append the lines below it to the temp file.
if [ -n "$linesbelow" ]; then
tail -n "$((linesbelow - 1))" "$name" >>"$tmp"
# If we didn't find the cutstring, append the whole file to the temp file.
else
cat "$name" >>"$tmp"
fi
# Finally, move the temp file to the target destination.
mv "$tmp" "$target"
# If we didn't find the cutstring in the target, show a warning and do nothing.
else
echo "WARNING: $target exists but is not a symlink"
fi
fi
# If the target doesn't exist...
else
# ... and it's not ignored...
if ! echo "$ignore" | grep -q "\b$name\b"; then
echo "Creating $target"
# ... if it contains the cutstring, copy it.
if grep -q "$cutstring" "$name"; then
cp "$PWD/$name" "$target"
# ... if it doesn't contain the cutstring, symlink it.
else
ln -s "$PWD/$name" "$target"
fi
fi
fi
done