1#!/bin/sh
2
3# We're called with the real Kodi executable as
4# first argument, followed by any Kodi extra args
5KODI="${1}"
6shift
7
8# In case someone asked we terminate, just kill
9# the Kodi process
10trap_kill() {
11    LOOP=0
12    killall "${KODI##*/}"
13}
14trap trap_kill INT QUIT TERM
15
16LOOP=1
17while [ ${LOOP} -eq 1 ]; do
18    # Hack: BusyBox ash does not catch signals while a non-builtin
19    # is running, and only catches the signal when the non-builtin
20    # command ends. So, we just background the Kodi binary, and wait
21    # for it. But BusyBox' ash's wait builtin does not return the
22    # exit code even if there was only one job (which is correct
23    # for POSIX). So we explicitly wait for the Kodi job
24    "${KODI}" "${@}" &
25    wait %1
26    ret=$?
27    case "${ret}" in
28        0)  ;;
29        64) poweroff; LOOP=0;;
30        66) reboot;   LOOP=0;;
31        *)  # Crash
32            sleep 1
33            ;;
34    esac
35done
36exit ${ret}
37