-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshared.sh
executable file
·50 lines (48 loc) · 867 Bytes
/
shared.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
#!/bin/sh
ask() {
printf "%s " "$1"
read -r reply
case "$reply" in
"Y")
return 0
;;
"y")
return 0
;;
*)
return 1
;;
esac
}
# TODO rewrite this in C because POSIX sucks
checked_copy() {
mkdir -p "$(dirname "$2")"
if [ -L "$2" ]; then
if [ -e "$2" ]; then
echo "$2 already linked, skipping..." && return 0;
else
if ! ask "$2 is a broken link, remove it?"; then
return 0;
fi
fi
fi
if [ -f "$2" ]; then
cmp -s "$1" "$2" || { diff "$1" "$2" || true && ask "$1 and $2 differ, overwrite?" || return 1; }
fi
# TOCTOU, what's that?
if ! touch "$2"; then
if ask "$2 is not writable, elevate permissions?"; then
command_rm="sudo rm"
command_ln="sudo ln"
else
return 1;
fi
else
command_rm="rm"
command_ln="ln"
fi
set -x
$command_rm -r "$2"
$command_ln -s "$PWD/$1" "$2"
{ set +x; } 2>/dev/null
}