-
Notifications
You must be signed in to change notification settings - Fork 1
/
run
executable file
·109 lines (89 loc) · 2.46 KB
/
run
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
#!/bin/sh
# Runs the 3 twitch-plays scripts concurrently. Configurable options are listed in the help command.
cd "$( dirname "$0" )"
. shell/tests.sh
# script flags
bot=1
nes=2
stream=4
# PIDs
botid=
nesid=
streamid=
# relevant paths
datadir=~/.twitch-plays-pi/proc/
scriptdir=shell/core/
outdest=/dev/null # destination of script output
scripts=0 # mask of scripts to run; if 0, all will run
dryrun=false # whether or not to actually execute the target scripts
get_flag() {
# $1: Flag to check mask for
# returns success if flag found or $scripts is 0, otherwise fails
[ "$(( $scripts & $1 ))" -eq $1 ] 2>/dev/null || [ $scripts -eq 0 ]
}
start_script() {
# calls script in $scriptdir with name $1, saving
# the PID in case something goes wrong
# $1: script name
echo "Starting $1 script..."
"$scriptdir/$1" >$outdest 2>&1 &
echo $! >"$datadir/$1.id"
eval "${1}id=$!"
wait
}
die() {
kill 0
}
while getopts hvbnsd opt; do
case $opt in
h)
# whitespace type is important here
# leading tabs are ignored but not leading spaces
cat <<-EOF
Usage: ./run.sh [-h] [-bns] [-q] [-d]
Options:
-h Show this help message and exit.
-b Start the bot script.
-n Start the NES script.
-s Start the streaming script.
-v Don't suppress script outputs (verbose).
-d Debug (don't execute core scripts, dump status).
If any of [-bns] are used, only the specified scripts will run.
Otherwise, all scripts will run.
EOF
exit 0
;;
v)
outdest=/dev/stdout
;;
b)
scripts=$((scripts | bot))
;;
n)
scripts=$((scripts | nes))
;;
s)
scripts=$((scripts | stream))
;;
d)
dryrun=true
;;
esac
done
if $dryrun; then
echo "Output destination: $outdest"
echo "Enabled scripts:"
get_flag $bot && printf "\t* Bot\n"
get_flag $nes && printf "\t* NES\n"
get_flag $stream && printf "\t* Stream\n"
exit 0
fi
trap "exit" INT TERM
trap "die" EXIT
# run each script indicated by flags in $scripts.
# PIDs are saved in $datadir/{name}.id if manual killing necessary
get_flag $bot && start_script bot &
get_flag $nes && start_script nes &
get_flag $stream && start_script stream &
printf "Press ^C to stop processes\n\n"
wait