forked from ehough/docker-kodi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·93 lines (69 loc) · 2.38 KB
/
entrypoint.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
#!/bin/bash
# ehough/docker-kodi - Dockerized Kodi with audio and video.
#
# https://github.com/ehough/docker-kodi
# https://hub.docker.com/r/erichough/kodi/
#
# Copyright 2018-2021 - Eric Hough (eric@tubepress.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#############################################################################
# This script is the entry point for the Kodi container and it has two jobs:
#
# 1. start Kodi by calling kodi-standalone (configurable via environment
# variable KODI_COMMAND).
#
# 2. cleanly stop Kodi when the container terminates, waiting up to 10
# seconds (configurable via environment variable KODI_QUIT_TIMEOUT)
# before itself exiting.
#
#############################################################################
readonly ENV_VAR_KODI_COMMAND="KODI_COMMAND"
readonly ENV_VAR_KODI_QUIT_TIMEOUT="KODI_QUIT_TIMEOUT"
log () {
echo "---> $1"
}
die () {
log "$1"
exit "$2"
}
get_kodi_pid () {
pidof kodi.bin
}
stop_kodi () {
if [[ -z $(get_kodi_pid) ]]; then
die "Kodi does not appear to be running. Exiting." 0
fi
local timer=0
local -r timeout="${!ENV_VAR_KODI_QUIT_TIMEOUT:-10}"
local remaining
log "asking Kodi to quit"
kodi-send --action="Quit"
while [[ $timer -lt $timeout && -n $(get_kodi_pid) ]]; do
remaining=$((timeout - timer))
log "waiting for Kodi to terminate ($remaining seconds until timeout)"
timer=$((timer+1))
sleep 1
done
if [[ -z $(get_kodi_pid) ]]; then
die 'Kodi terminated successfully' 0
fi
log "WARNING: timeout of $timeout second(s) reached"
}
start_kodi () {
local -r command="${!ENV_VAR_KODI_COMMAND:-kodi-standalone}"
# gracefully stop Kodi whenever this script is terminated for any reason
trap stop_kodi EXIT
log "starting Kodi with command: $command"
$command
}
start_kodi