-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert-symlink.sh
More file actions
53 lines (40 loc) · 928 Bytes
/
convert-symlink.sh
File metadata and controls
53 lines (40 loc) · 928 Bytes
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
#!/usr/bin/env bash
set -euo pipefail
link_to_replace="$1"
if [[ ! -L "$link_to_replace" ]]; then
echo "error: $link_to_replace is not a symlink" >&2
exit 1
fi
link_target="$(readlink "$link_to_replace")"
rm -f "$link_to_replace"
{
cat <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
self_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
self="$self_dir/$(basename -- "${BASH_SOURCE[0]}")"
EOF
printf 'link_target=%q\n' "$link_target"
cat <<'EOF'
target="$link_target"
if [[ "$target" != /* ]]; then
target="$self_dir/$target"
fi
if ! rm -f "$self" || [[ -e "$self" ]]; then
exec -a "$0" "$target" "$@"
fi
EOF
cat <<'EOF'
tmp_link="${self}.tmp.$$"
if ln -s -- "$link_target" "$tmp_link" && mv -f -- "$tmp_link" "$self"; then
:
else
rm -f -- "$tmp_link"
exec -a "$0" "$target" "$@"
fi
EOF
cat <<'EOF'
exec -a "$0" "$self" "$@"
EOF
} > "$link_to_replace"
chmod +x "$link_to_replace"