summarylogtreecommitdiffstats
path: root/vdr-runvdr.sh
blob: 7d3e066fc1ec0698a6559a88728af4bb6904f645 (plain)
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
#!/bin/bash

# runvdr - VDR launcher
#
# runvdr [VDROPTION]...

shopt -s extglob nocasematch nullglob

VDR=/usr/sbin/vdr
PLUGINDIR=/usr/lib/vdr
PLUGINVER=VDR_PLUGIN_VERSION
PLUGINSUF=${PLUGINVER:+.$PLUGINVER}

log()
{
    type -P logger &>/dev/null && \
        logger -s -p daemon.info -t ${0##*/} "$1" 2>&1 || echo "INFO: $1"
}

plugconf()
{
    local plugin=$1 PLUGIN_OPTIONS= PLUGIN_ENABLED=
    if [[ -e /etc/vdr/vdr-plugins.d/$plugin.conf ]] ; then
        . /etc/vdr/vdr-plugins.d/$plugin.conf
        case $PLUGIN_ENABLED in no|false|0) return ;; esac
    fi
    if [[ $PLUGIN_OPTIONS ]] ; then
        VDR_OPTIONS+=( --plugin="$plugin $PLUGIN_OPTIONS" )
    else
        VDR_OPTIONS+=( --plugin=$plugin )
    fi
}

build_cmdline()
{
    local plugin= p=
    # Add "priority" plugins.
    for plugin in $VDR_PLUGIN_ORDER ; do
        [[ -e $PLUGINDIR/libvdr-${plugin}.so$PLUGINSUF ]] && plugconf $plugin
    done
    # Add the rest available.
    for plugin in $PLUGINDIR/libvdr-*.so$PLUGINSUF ; do
        plugin=${plugin##*/libvdr-}
        plugin=${plugin%.so$PLUGINSUF}
        for p in $VDR_PLUGIN_ORDER ; do
            if [[ $plugin == $p ]] ; then
                # Already added.
                continue 2
            fi
        done
        plugconf $plugin
    done
}

reload_dvb()
{
    local modules=$( /sbin/lsmod | \
        awk '/^dvb_core/ { gsub(","," ",$4) ; print $4 }' )
    if [[ $modules ]] ; then
        log "Reloading DVB modules"
        /sbin/modprobe -r $modules dvb_core
        for module in $modules ; do
            /sbin/modprobe $module
        done
    fi
}

set_rtcwake()
{
    # Check timestamp set by shutdown script.
    local nexttimer=$( cat /var/run/vdr/next-timer 2>/dev/null )
    rm -f /var/run/vdr/next-timer

    if [[ $nexttimer != +([0-9]) ]] ; then
        # Next timer timestamp not set by shutdown script or bogus,
        # try to get it via SVDRP.
        nexttimer=$( svdrpsend NEXT abs 2>/dev/null | \
            sed -rne 's/^250[[:space:]]+[0-9]+[[:space:]]+([0-9]+).*/\1/p' )
    fi

    if [[ $nexttimer && $nexttimer -gt $( date +%s ) ]] ; then
        [[ -f /etc/vdr/vdr ]] && . /etc/vdr/vdr
        local when=$(( $nexttimer - ${WAKEUP_BEFORE_RECORDING:-10} * 60 ))
        local hrwhen=$( date -d "1970-01-01 $when sec UTC" )
        log "Setting wakeup time for next recording: $hrwhen"
        /usr/sbin/rtcwake -m no -t $when >/dev/null
    fi
}

if [[ $1 == --set-wakeup ]] ; then
    # Just set RTC wakeup for next timer event.
    set_rtcwake
    exit $?
fi

rc=
while true ; do

    VDR_OPTIONS=()
    if [[ $VDR_INIT ]] ; then
        [[ -f /etc/vdr/vdr ]] && . /etc/vdr/vdr
        [[ $DAEMON_COREFILE_LIMIT ]] && \
            ulimit -S -c $DAEMON_COREFILE_LIMIT &>/dev/null && \
            VDR_OPTIONS+=( --userdump ) && cd ${TMPDIR:-/tmp}
        build_cmdline
    fi

    $VDR "$@" "${VDR_OPTIONS[@]}"
    rc=$?

    # 137: "kill -KILL" eg in killproc(), others: "man vdr"
    case $rc in
        0|2|137)
            log "VDR exited with status $rc, exiting"
            break
            ;;
        *)
            log "VDR exited with status $rc, attempting restart"
            case $RELOAD_DVB in yes|true|1) reload_dvb ;; esac
            ;;
    esac

done

exit $rc