### Generic variables and functions
### -------------------------------

if [ -z "${SYNOPKG_PKGNAME}" ] || [ -z "${SYNOPKG_DSM_VERSION_MAJOR}" ]; then
  echo "Error: Environment variables are not set." 1>&2;
  echo "Please run me using synopkg instead. Example: \"synopkg start [packagename]\"" 1>&2;
  exit 1
fi


### Package specific variables and functions
### ----------------------------------------

# Package specific behaviors
# Sourced script by generic installer and start-stop-status scripts

KERNEL_MIN="4.4"
KERNEL_RUNNING=$(uname -r)
STATUS=$(printf '%s\n%s' "${KERNEL_MIN}" "${KERNEL_RUNNING}" | sort -VCr && echo $?)
FFMPEG_DIR=/var/packages/ffmpeg/target
iHD=${FFMPEG_DIR}/lib/iHD_drv_video.so

###
### Disable Intel iHD driver on older kernels
### $(uname -r) <= ${KERNEL}
###
disable_iHD ()
{
    if [ "${STATUS}" = "0" ]; then
       [ -s ${iHD} ] && mv ${iHD} ${iHD}-DISABLED 2>/dev/null
    fi
}

service_postinst ()
{
    if [ $SYNOPKG_DSM_VERSION_MAJOR -lt 7 ];then
        # setuid for proper vaapi access
        chmod u+s ${FFMPEG_DIR}/bin/ffmpeg
        chmod u+s ${FFMPEG_DIR}/bin/vainfo
    fi

    disable_iHD
}

service_postupgrade ()
{
    if [ $SYNOPKG_DSM_VERSION_MAJOR -lt 7 ];then
        # setuid for proper vaapi access
        chmod u+s ${FFMPEG_DIR}/bin/ffmpeg
        chmod u+s ${FFMPEG_DIR}/bin/vainfo
    fi

    disable_iHD
}
