-
Notifications
You must be signed in to change notification settings - Fork 0
/
brew_symlinks
executable file
·121 lines (98 loc) · 2.62 KB
/
brew_symlinks
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
#! /usr/bin/env bash
set -eu -o pipefail
prog="$(basename "$0")"
function errordie {
[ "${*-}" ] && echo "$prog: $*" 1>&2
exit 1
}
if ! brew=$(type -p brew); then
errordie brew is not installed or in PATH
fi
prefix=$($brew --prefix)
realpath=$($brew --prefix coreutils)/bin/grealpath
function usage {
[ "${*-}" ] && echo "$prog: $*" 1>&2
cat 1>&2 <<EOF
Usage: $prog [--exec] [-q]
Print what Homebrew symlinks should be created in ${prefix}/bin/.
Homebrew already creates links for programs which do not conflict
with native macOS programs. What this script does: symlink the
programs in
${prefix}/.../libexec/gnubin
into ${prefix}/bin/.
Command line args:
--exec :: create the links printed
-q :: do not print info about --exec
EOF
exit 1
}
[ "$(uname -s)" = "Darwin" ] || errordie only runs on macOS
install=
quiet=
while [ $# -gt 0 ]; do
case $1 in
--exec) install=$1 ;;
-q) quiet=$1 ;;
*) usage extra args: $* ;;
esac
shift
done
# setup admin user
if id -Gn "$USER" | grep -q -w admin; then
admin=
else
admin="admin@$(hostname -s)"
fi
# usage: x command args
# Execute COMMAND with ARGS in the appropriate environment, which
# is as user 'admin'.
function x {
local execute=$install
if [ "$1" = "--exec" ]; then
execute=$1
shift
fi
local command="$1"
shift
if [ "$execute" ]; then
echo "+ $command $*"
if [ ! "$admin" ]; then # execute directly
"$command" "$@"
else # use the admin credentials
ssh -i "$HOME/.ssh/id_rsa_nopw" "$admin" "$command" "$@"
fi
else
echo "would: $command $*"
fi
}
# so * expands to nothing if there are no matches
shopt -s nullglob
{
cd "$prefix"
x --exec "$brew" cleanup -v
cd "${prefix}"
read -a gnubins <<< "$(find . '(' -name bin -o -name gnubin ')' -print0 | xargs -0 echo)"
dst=${prefix}/bin
for gnubin in "${gnubins[@]}"; do
progs=($gnubin/*)
for f in "${progs[@]}"; do
base=$(basename "$f")
if [ -L "${dst}/$base" ] && [ ! -e "${dst}/$base" ]; then
# link is bad, so remove it
x rm -f "${dst}/$base"
fi
if [ -e "${dst}/$base" ]; then
: # ignore
elif [ ! -L "${dst}/$base" ]; then
# This is to make the symlinks relative
target=$($realpath -s -m --relative-to="${dst}/" "${prefix}/$f")
x ln -s "${target}" "bin/$base"
fi
done
done
if [ ! "$install" ] && [ ! "$quiet" ]; then
echo ""
echo "To execute the above commands do: $prog --exec"
fi
exit 0
}