#!/bin/sh

CONF=/etc/config/qpkg.conf
QPKG_NAME="DVBLinkTVServer"
DVBLINK_INSTALL_DIR=$(/sbin/getcfg $QPKG_NAME Install_Path -d "" -f $CONF)

DVBLINK_ROOT_DIR="/mnt/dvblink"

ln -sf $DVBLINK_INSTALL_DIR $DVBLINK_ROOT_DIR


export LD_LIBRARY_PATH=${DVBLINK_ROOT_DIR}/lib

SUNDTEK_QPKG_NAME="TVH_Dev_Sundtek"
SUNDTEK_QPKG_DIR=$(/sbin/getcfg $SUNDTEK_QPKG_NAME Install_Path -d "" -f $CONF)

# if dvblink is installed, let the dvblink startscript start the driver
SUNDTEK_ENABLED=$(/sbin/getcfg $SUNDTEK_QPKG_NAME Enable -u -d FALSE -f $CONF)
if [ "$SUNDTEK_ENABLED" = "TRUE" ]; then
        SUNDTEK_QPKG_NAME=TVH_Dev_Sundtek
        SUNDTEK_INSTALL_DIR=$(/sbin/getcfg $SUNDTEK_QPKG_NAME Install_Path -d "" -f $CONF)
        SUNDTEK_PATH=${SUNDTEK_INSTALL_DIR}/sundtek/opt/bin/
        export LD_PRELOAD=${SUNDTEK_INSTALL_DIR}/sundtek/opt/lib/libmediaclient.so

        if [ ! -e  ${SUNDTEK_INSTALL_DIR}/sundtek_dvblogic.conf ]; then
            echo first_adapter=8 > ${SUNDTEK_INSTALL_DIR}/sundtek_dvblogic.conf
        fi
        ${SUNDTEK_PATH}/mediaclient --shutdown
        ${SUNDTEK_PATH}/mediaclient --start=10 --serverpath=${SUNDTEK_PATH} --config=${SUNDTEK_INSTALL_DIR}/sundtek_dvblogic.conf --pluginpath=${SUNDTEK_PATH}
fi

export DVBLINK_CONFIG_DIR=${DVBLINK_ROOT_DIR}/config
export DVBLINK_COMMON_DIR=${DVBLINK_CONFIG_DIR}

current_dir=`pwd`
cd ${DVBLINK_ROOT_DIR}/common/
sh ./start-sources.sh
cd $current_dir
${DVBLINK_ROOT_DIR}/dvblink_server
