-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·67 lines (56 loc) · 1.58 KB
/
setup.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
#!/usr/bin/env sh
set -euf
# https://stackoverflow.com/a/29835459/16330198
dir="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"
PREFIX="${PREFIX:-$HOME}"
# Loop through every file in source directory and sub directories except .git
# subdirectory and this script
files="$(find "$dir" \( -path "$dir/.git" -o -name '.*.swp' -o -path "$dir/setup.sh" \) -prune -o -type f -print | sort)"
until [ -z "$files" ]; do
f="$(echo "$files" | head -1)"
files="$(echo "$files" | tail -n +2)"
if realpath --relative-to=. . > /dev/null 2>&1; then
relative_path="$(realpath --relative-to="$dir" "$f")"
else
relative_path="${f#"$dir/"}"
fi
container="$(dirname "$relative_path")"
dest="$PREFIX/$relative_path"
dest_container="$PREFIX/$container"
mkdir -p "$dest_container"
if diff -q "$f" "$dest" > /dev/null 2>&1; then
echo "Skipping identical $relative_path"
continue
fi
if [ -s "$dest" ]; then
resp=
until [ "$resp" = 'y' ] || [ "$resp" = 'n' ]; do
printf "Replace ~/%s? (y/n/d/q) " "$relative_path"
read -r resp
resp="$(printf '%.1s' "$resp" | tr '[:upper:]' '[:lower:]')"
if [ "$resp" = 'y' ]; then
if command -v trash > /dev/null 2>&1; then
trash "$dest"
else
mv "$dest" "$dest.bak"
fi
elif [ "$resp" = 'd' ]; then
diff "$dest" "$f" || true
elif [ "$resp" = 'q' ]; then
exit 0
else
continue 2
fi
done
fi
echo "Copying $relative_path"
cp "$f" "$dest"
if [ "$container" = '.gnupg' ]; then
chmod 700 "$dest_container"
chmod 600 "$dest"
fi
if [ "$container" = '.local/bin' ]; then
chmod 755 "$dest"
fi
done
echo 'Done'