#!/usr/bin/sh
# vdr-check-setup - ExecStartPre of vdr.service, runs as root.
#
# The packaged channels.conf contains only whitespace, and VDR is of no use
# without channels: refuse to start (exit 2 matches RestartPreventExitStatus
# in vdr.service) until a channel list exists.  A list written by root, by a
# scanner or an editor, is handed over to VDR, which rewrites it.

cfg=/etc/vdr/channels.conf

if ! grep -q '[^[:space:]]' "$cfg" 2>/dev/null ; then
    msg="$cfg has no channels; create it with a channel scanner, e.g. w_scan2 or \"dvbv5-scan -O VDR\" (dvb-tools)."
    # stderr may be a VT (StandardInput=tty in a drop-in)
    if [ -n "$INVOCATION_ID" ] ; then
        logger -t "${0##*/}" -p user.err -- "$msg"
    else
        echo "$msg" >&2
    fi
    exit 2
fi
chown vdr:video "$cfg" && chmod 644 "$cfg"
