-
Notifications
You must be signed in to change notification settings - Fork 0
/
minipc-tmux-daemon-helper.sh
executable file
·348 lines (306 loc) · 8.97 KB
/
minipc-tmux-daemon-helper.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/bash
HELP=$(cat << EOF
Usage: $(basename "$0") [options] {[i]nstall,[c]lean,[r]einstall}
-h, --help Show this message
-t, --tmux-dir Set tmux directory
-s, --systemd-dir Set user systemd directory
-r, --systemd-req Set user systemd required service
-m, --minipc-dir Set minipc script directory
-u, --umask Set umask
-i, --interactive Run interactively
The included scripts/system-setup-helper.sh which will setup dependancies and
cause the service to be started at boot.
After installation, run \`systemctl --user start ${SRV}\` to start the
minipc tmux daemon. To attach to the session, run \`tmux -L mtd a -t mtd\`. You
may want to add \`alias minipc='tmux -L mtd attach -t mtd'\` to your .*rc. To
kill the daemon, either stop the attached session manually or run
\`systemctl --user stop ${SRV}\`.
[Ctrl+c] within the session restarts the minipc script.
Do not remove this file or ${LOG}. If this file is moved, move the log
with it. Clean before reinstalling.
Default key bindings (Meta is likely Alt):
M-f -> send-prefix (begin command)
: -> command-prompt (tmux command prompt)
z -> kill-window
d -> detach
r -> source-file /path/to/${CNF}
Examples:
To detach the connected session, press [Alt+f] then press [d].
To kill the connected session, press [Alt+f] then press [z].
For some terminals, the meta key has to be configured to act as an escape key.
E.g. set 'XTerm*metaSendsEscape: true' for \`xterm\`
EOF
)
LOG=.mtdparam.log
SRV=minipctd.service
CNF=tmux-mtd.conf
CONF_DIR=${XDG_CONFIG_HOME:-$HOME/.config}
TMUX_DIR=${CONF_DIR}/tmux
SYSTEMD_DIR=${CONF_DIR}/systemd/user
SYSTEMD_REQ=irm-jetson.service
MINIPC_DIR=$PWD
MINIPC_TARGET=src/build/main
UMASK_MODE=0022
user_tmux_dir=
user_systemd_dir=
user_systemd_req=
user_minipc_dir=
user_minipc_target=
user_umask_mode=
script_name=$(basename "$0")
script_file=$(realpath "$0")
script_dir=$(realpath "${script_file}" | xargs dirname)
script_log=${script_dir}/${LOG}
function printhelp() {
echo "$HELP"
exit ${1:-0}
}
function install() {
interactive=${1/-}
if [ -f "${script_log}" ]; then
echo "${script_log}" already exists.
echo Run \`${script_name} clean\` before reinstalling.
exit 1
fi
if [ ${interactive} ]; then
echo Choose \`tmux\` configuration directory.
echo -n [default=${TMUX_DIR}]:\
read user_tmux_dir
echo
echo Choose user systemd directory.
echo -n [default=${SYSTEMD_DIR}]:\
read user_systemd_dir
echo
echo Choose systemd service requirement.
echo -n [default=${SYSTEMD_REQ}]:\
read user_systemd_req
echo
echo Choose minipc directory.
echo -n [default=${MINIPC_DIR}]:\
read user_minipc_dir
echo
echo Choose minipc script name.
echo -n [default=${MINIPC_TARGET}]:\
read user_minipc_target
echo
echo Choose umask.
echo -n [default=${UMASK_MODE}]\
read user_umask_mode
echo
fi
echo Selected configuration:
echo "TMUX_DIR: ${user_tmux_dir:=$TMUX_DIR}"
echo "SYSTEMD_DIR: ${user_systemd_dir:=$SYSTEMD_DIR} (user)"
echo "MINIPC_DIR: ${user_minipc_dir:=$MINIPC_DIR}"
echo "MINIPC_TARGET: ${user_minipc_target:=$MINIPC_TARGET}"
echo "UMASK_MODE: ${user_umask_mode:=$UMASK_MODE}"
echo
echo "SERVICE: ${user_systemd_dir}/${SRV}"
echo "SCRIPT: ${script_file}"
echo "LOG: ${script_log}"
echo
if [ ${interactive} ]; then
echo -n Continue? [,y/n]\
read cont
[ "$cont" == n ] && exit 0
echo
fi
trap 'rm -rf "$tmpdir"' EXIT
tmpdir=$(mktemp -d /tmp/smtd.XXXXX)
# Files
f_mtdparamlog=${script_log}
f_tmuxconf=${user_tmux_dir}/${CNF}
f_systemdservice=${user_systemd_dir}/${SRV}
# Temporary files
tf_mtdparamlog=${tmpdir}/"${f_mtdparamlog}"
tf_tmuxconf=${tmpdir}/"${f_tmuxconf}"
tf_systemdservice=${tmpdir}/"${f_systemdservice}"
mkdir -p "${tf_tmuxconf%\/*}" && > "${tf_tmuxconf}"
echo +++ Write tmux configuration to "${tf_tmuxconf}"
cat << EOF | tee "${tf_tmuxconf}"
# config for minipctd in detached tmux session
unbind-key -a
set -g escape-time 0
set -g mouse off
set -g visual-activity off
set -g visual-bell off
set -g visual-silence off
set -g monitor-activity off
set -g monitor-bell off
set -g prefix M-f
bind M-f send-prefix
bind : command-prompt
bind z kill-window
bind d detach
bind r source-file ${f_tmuxconf}
EOF
echo
echo
mkdir -p "${tf_systemdservice%\/*}" && > "${tf_systemdservice}"
echo +++ Write systemd user service to "${tf_systemdservice}"
cat << EOF | tee "${tf_systemdservice}"
[Unit]
Description=minipctd
Requires=${user_systemd_req}
[Service]
Type=forking
ExecStart=/bin/tmux -f "${f_tmuxconf}" -L mtd new-ses -s mtd -n minipctd -c "${user_minipc_dir}" -d -- "${script_file}" jobcontrol "${user_minipc_target}" --dummy --skip-tests
ExecStop=/usr/bin/tmux -L mtd kill-ses
ExecReload=/usr/bin/tmux -L mtd send-keys C-c
[Install]
WantedBy=multi-user.target
EOF
echo
echo
mkdir -p "${tf_mtdparamlog%\/*}" && > "${tf_mtdparamlog}"
echo +++ Log installed files and directories to "${tf_mtdparamlog}"
# Files
for f in ${!f_@}; do
echo "${f}=${!f}" | tee -a "${tf_mtdparamlog}"
done
# Directories
for d in ${!d_@}; do
echo "${d}=${!d}" | tee -a "${tf_mtdparamlog}"
done
echo
echo
echo Created files:
sed -e '/^[^f]/d' -e "s|^f_.*=\(.*\)|${tmpdir}/\1|" "${tf_mtdparamlog}" \
| xargs ls -ld
echo
if [ ${interactive} ]; then
echo -n Copy to system? [,y/n]\
read cont
[ "$cont" ] && exit 0
echo
fi
echo Copy to system \(umask ${user_umask_mode}\)
function copy_to_system() (
tmpdir=$1
script_file=$2
script_dir=$3
script_log=$4
umask_mode=$5
umask "${umask_mode}"
source "${tmpdir}/${script_log}"
# Directories
for d in ${!d_@}; do
mkdir -d "${!d}"
done
# Files
for f in ${!f_@}; do
cat "${tmpdir}/${!f}" > "${!f}"
done
)
copy_to_system "${tmpdir}" \
"${script_file}" \
"${script_dir}" \
"${script_log}" \
"${user_umask_mode}"
cat "${script_log}" | cut -d= -f2 | xargs ls -l
echo
# Changes
echo Reload systemd configuration with \`systemctl --user daemon-reload\`
systemctl --user daemon-reload
echo Enable ${SRV}
systemctl --user enable "${SRV}"
echo
echo Done.
}
function clean() {
interactive=${1/-}
reinstall=${2/-}
if [ ! -f "${script_log}" ]; then
echo "${script_log}" does not exist.
echo Nothing to be cleaned.
[ ${reinstall} ] \
&& return 0 \
|| exit 0
fi
if [ ${interactive} ]; then
cat "${script_log}"
echo
echo -n Remove these? [,y/n]\
read cont
[ "$cont" == n ] && exit 0
echo
fi
source "${script_log}"
# Files
for f in ${!f_@}; do
[ -f "${!f}" ] && \
rm -v "${!f}"
done
# Directories
for d in ${!d_@}; do
rm -rfv "${!d}"
done
echo Disable ${SRV}
systemctl --user disable "${SRV}"
echo
echo Done.
}
function jobcontrol() {
#echo $@
echo Job started $(date)
job_target=$1
job_mode=${2:---dummy}
shift 2
returncode=$?
# Restart if killed with C-c
if [ ${returncode} -eq 130 ]; then
deactivate
clear
jobcontrol "${job_target}" --display-menu "$@"
else
deactivate
echo Not killed with C-c, restarting in 1s.
sleep 1
clear
jobcontrol "${job_target}" --dummy "$@"
fi
}
interactive=-
reinstall=-
function run() {
while [ $# -gt 0 ]; do
case $1 in
-t|--tmux-dir)
user_tmux_dir=$2;
shift; shift;;
-s|--systemd-dir)
user_systemd_dir=$2;
shift; shift;;
-r|--systemd-req)
user_systemd_req=$2;
shift; shift;;
-m|--minipc-dir)
user_minipc_dir=$2;
shift; shift;;
-u|--umask)
user_umask_mode=$2;
shift; shift;;
-i|--interactive)
interactive=1;
shift;;
i|install)
install ${interactive};
shift;;
c|clean)
clean ${interactive};
shift;;
r|reinstall)
reinstall=1
clean ${interactive} ${reinstall};
install ${interactive};
shift;;
jobcontrol)
shift;
jobcontrol "$@";;
*)
printhelp;
shift;;
esac
done
}
run "${@:-help}"