#!/bin/sh

[% IF !CONFIG_DVB_MODULES -%]
  [% SKIPFILE %]
[% END -%]

kernelRelease=$(uname -r)
d=/lib/modules/$kernelRelease/kernel/drivers/media/dvb
x=/lib/modules/$kernelRelease/extra

num_hw_frontend_demux=[%CONFIG_NUM_HW_FRONTEND_DEMUXES%]
num_hw_memory_demux=$(([%CONFIG_NUM_HW_DEMUXES%]-[%CONFIG_NUM_HW_FRONTEND_DEMUXES%]))

case ${num_hw_frontend_demux} in
	"0")
		hw_demux_frontend_mask=0x0
		;;
	"1")
		hw_demux_frontend_mask=0x1
		;;
	"2")
		hw_demux_frontend_mask=0x3
		;;
	*)
		echo "Invalid HW frontend demux number"
		exit 1
		;;
esac

case ${num_hw_memory_demux} in
	"0")
		hw_demux_memory_mask=0x0
		;;
	"1")
		hw_demux_memory_mask=0x4
		;;
	"2")
		hw_demux_memory_mask=0xc
		;;
	*)
		echo "Invalid HW demux number"
		exit 1
		;;
esac

hw_demux_mask=$((hw_demux_frontend_mask|hw_demux_memory_mask))

# PVR demuxes number
[% IF ! CONFIG_PVR_RECORD -%]
pvr_demux_mask=0x0
[% ELSE %]
pvr_demux_mask=${hw_demux_frontend_mask}
[% END -%]

opts_phStbDemux="demod0=[% CONFIG_TS0_demod %] tuner0=[% CONFIG_TS0_tuner %] demod1=[% CONFIG_TS1_demod %] tuner1=[% CONFIG_TS1_tuner %]"
opts_phStbDemux833x="enabled=${hw_demux_mask} pvr=${pvr_demux_mask}"
opts_phStbDemux833x="$opts_phStbDemux833x TS0_routing=[%CONFIG_TS0_ROUTING%] TS1_routing=[%CONFIG_TS1_ROUTING%] TS2_routing=[%CONFIG_TS2_ROUTING%]"


case "$1" in
    start)
# Load DVB modules if available. This will load support for DVB, tuners and HW demux.
    echo -n "Loading DVB modules..."

[% IF CONFIG_TDA10024 -%]
insmod $d/frontends/tda10024.ko
[% END -%]
[% IF CONFIG_TDA10046 -%]
insmod $d/frontends/tda1004x.ko
[% END -%]
[% IF CONFIG_TDA10048 -%]
insmod $d/frontends/tda10048.ko init_on_startup=1
[% END -%]
[% IF CONFIG_TDA10086 -%]
insmod $d/frontends/tda10086.ko
[% END -%]
[% IF CONFIG_ISDB_TC90507XBG -%]
insmod $d/frontends/tc90507xbg.ko
[% END -%]
[% IF CONFIG_ISDB_MB86A20S -%]
insmod $d/frontends/mb86a20s.ko
[% END -%]

insmod $d/dvb-core/dvb-core.ko
insmod $d/nxp/phStbDemux.ko $opts_phStbDemux

[% IF CONFIG_TDHL5_T -%]
#
# Fixme: $n directory isn't defined... therefore this module will not load. Should it be loaded from $f instead ?
#
if [ -f $n/tdhl5_sam.ko ]; then
	
	mknod /dev/tdhl5 c 210 1
	insmod $n/tdhl5_sam.ko
fi
[% END -%]

insmod $x/phStbDemux833x.ko $opts_phStbDemux833x

[% IF CONFIG_COMMON_INTERFACE_PRESENT -%]
opts_phStbCommonInterface=
opts_phStbCommonInterface="$opts_phStbCommonInterface gpio_reset_pin=[% CONFIG_COMMON_INTERFACE_RESET_GPIO_PIN %]"
opts_phStbCommonInterface="$opts_phStbCommonInterface gpio_detect_pin=[% CONFIG_COMMON_INTERFACE_DETECT_GPIO_PIN %]"
opts_phStbCommonInterface="$opts_phStbCommonInterface gpio_bypass_pin=[% CONFIG_COMMON_INTERFACE_BYPASS_GPIO_PIN %]"
insmod $x/phStbCommonInterface.ko $opts_phStbCommonInterface
[% END -%]
    echo "done"

    ;;
    stop)
    echo "Cannot unload DVB modules!"
    ;;
    *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac
