-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_agent_wrapper.sh
executable file
·128 lines (104 loc) · 3.29 KB
/
install_agent_wrapper.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
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
122
123
124
125
126
127
128
#! /usr/bin/env bash
# Install a wrapper for an app in /Applications/<name>.app that
# 1. allows the app to be started from the macOS Dock, and
# 2. initializes environment variables so that my SSH agent will be
# used by the app's process.
#
# Thanks to
# https://stackoverflow.com/questions/829749/launch-mac-eclipse-with-environment-variables-set
# for the idea on editing the Info.plist and running lsregister.
# Thanks to
# https://stackoverflow.com/questions/36111323/in-plist-files-how-to-extract-string-text-after-unique-key-tag-via-xmlstarlet-to
# for crucial info on using xmlstarlet to trivially edit the Info.plist.
# Tested on macOS 10.12.6.
set -eu
prog=$(basename $0)
function usage {
[ "${*-}" ] && echo "$prog: $*" 1>&2
cat 1>&2 <<EOF
Usage: $prog [-f] appname
Wrap /Applications/APPNAME.app with a shell script that initializes an
SSH agent for use by APPNAME.
-f :: means force the use of an SSH agent (if available),
otherwise only start it if the env evar AGENT_WRAPPER_USE_AGENT
has a value other than null.
EOF
exit 1
}
function errordie {
[ "${*-}" ] && echo "$prog: $*" 1>&2
exit 1
}
debug=
appname=
force=
while [ $# -gt 0 ]; do
case $1 in
--debug) debug=$1 ;;
-f) force=$1 ;;
-*) usage bad argument: $1 ;;
*) [ $# -gt 1 ] && usage too many args: $*
if [[ "$1" =~ (.*)\.app$ ]]; then
appname="${BASH_REMATCH[1]}"
else
appname="$1"
fi
;;
esac
shift
done
function d {
echo "+ $*" 1>&2
if [ ! "$debug" ]; then
"$@"
fi
}
# The file to source to make the SSH agent available. This file was
# generated with
# $ ssh-agent -s > $HOME/tmp/agent.info
agentinfo="$HOME/tmp/agent.info"
appdir="/Applications/${appname}.app"
lsregister="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
[ -d "$appdir" ] || errordie $appdir does not exist
[ -x "$lsregister" ] || errordie cannot find lsregister
path="$appdir/Contents/MacOS"
appexe="$path/${appname}"
appexereal="$path/${appname}.real"
plist="$appdir/Contents/Info.plist"
wraptmp=/tmp/tempA$$
plisttmp=/tmp/tempB$$
rm -f $wraptmp $plisttmp
trap "/bin/rm -f $wraptmp $plisttmp" 0
#### Move the real app aside
if [ ! -e "$appexereal" ]; then
d sudo mv "$appexe" "$appexereal"
fi
#### Create the wrapper
cat > $wraptmp <<EOF
#! /usr/bin/env bash
if [ "$force" ] && [ -f "$agentinfo" ]; then
source "$agentinfo"
elif [ "\${AGENT_WRAPPER_USE_AGENT-}" ] && [ -f "$agentinfo" ]; then
source "$agentinfo"
fi
logger "$path/$appname"
exec "$appexereal" "\$@"
EOF
d chmod +x $wraptmp
#### Install the wrapper
d sudo mv $wraptmp "$appexe"
exit 0
###############################################################################
# No longer necessary, since now the original path is used for the wrapper.
# Code left in because it took me a good while to figure it out!
# If this code is used again, move this comment to the top of the file:
# Depends on: xmlstarlet (I use the macports version)
type -p xmlstarlet > /dev/null || errordie xmlstarlet not installed
d xmlstarlet \
ed -u \
'//key[.="CFBundleExecutable"]/following-sibling::string[1]' \
-v "$(basename $script)" \
< $plist > $plisttmp
[ -f ${plist}.orig ] || d sudo mv $plist ${plist}.orig
d sudo mv $plisttmp $plist
d $lsregister -v -f "$appdir"