# Define python binary path
PYTHON_DIR="/var/packages/_PYTHON_PACKAGE_/target/bin"
# Define ffmpeg binary path
FFMPEG_DIR="/var/packages/_FFMPEG_PACKAGE_/target/bin"
# Add local bin, virtualenv along with ffmpeg and python to the default PATH
PATH="${SYNOPKG_PKGDEST}/env/bin:${SYNOPKG_PKGDEST}/bin:${PYTHON_DIR}:${FFMPEG_DIR}:${PATH}"

# Service configuration. Change http and htsp ports here and in conf/tvheadend.sc for non-standard ports
HTTP=9981
HTSP=9982

# Replace generic service startup, run service in background
GRPN=$(id -gn ${EFF_USER})
UPGRADE_CFG_DIR="${SYNOPKG_PKGVAR}/dvr/config"

TVH_BIN="${SYNOPKG_PKGDEST}/bin/tvheadend"

# Group configuration to manage permissions of recording folders
GROUP=sc-media

# Base arguments
TVH_ARGS="-f -C -u ${EFF_USER} -g ${GRPN} \
    --http_port ${HTTP} --htsp_port ${HTSP} \
    -c ${SYNOPKG_PKGVAR} \
    -p ${PID_FILE} \
    -l ${LOG_FILE} \
    --nobackup"

SVC_BACKGROUND=yes
SERVICE_COMMAND="${TVH_BIN} ${TVH_ARGS}"


service_postinst ()
{
    # Create a Python virtualenv and install wheel (EPG Grabber)
    install_python_virtualenv
    install_python_wheels
}

service_postupgrade ()
{
    # Need to enforce correct permissions for recording directories on upgrades
    echo "Adding ${GROUP} group permissions on recording directories:"
    for file in ${UPGRADE_CFG_DIR}/*
    do
        DVR_DIR=$(grep -e 'storage\":' ${file} | awk -F'"' '{print $4}')
        # Exclude directories in @appstore as ACL permissions skew up package installations
        TRUNC_DIR=$(echo "$(realpath ${DVR_DIR})" | awk -F/ '{print "/"$3}')
        if [ "${TRUNC_DIR}" = "/@appstore" ]; then
            echo "Skip: ${DVR_DIR} (system directory)"
        elif [ $SYNOPKG_DSM_VERSION_MAJOR -lt 7 ]; then
            echo "Done: ${DVR_DIR}"
            set_syno_permissions "${DVR_DIR}" "${GROUP}"
        fi
    done

    # For backwards compatibility, restore ownership of package system directories
    if [ $SYNOPKG_DSM_VERSION_MAJOR -lt 7 ]; then
        echo "Restore '${EFF_USER}' unix permissions on package system directories"
        chown ${EFF_USER}:${USER} "${SYNOPKG_PKGDEST}"
        set_unix_permissions "${SYNOPKG_PKGVAR}"
    fi
}
