#!/bin/sh

#### WARNING  WARNING  WARNING  WARNING  WARNING  WARNING ####
# Replacing CONFIG_* variables with hard coded values will most likely cause
# problems in phStbKBufMgr, since the phStbKBufMgr kernel module uses generated
# C code based on values in $BUILDROOT/prjconfig.h .
# THUS... use prjconfig and "prjmake all" to ensure that this file
# in $ROOTFS and the phStbKBufMgr kernel module are consistent.
#### WARNING  WARNING  WARNING  WARNING  WARNING  WARNING ####

# 'resolution' can be set to one of:
#
# 1280x720x50p 1280x720x60p 1280x720x59.94p
# 1920x1080x50i 1920x1080x60i 1920x1080x59.94i
# 1920x1080x29.97p
# 1920x1080x60p   1920x1080x50p
#
# 640x480x60p     800x600x75p        1024x768x75p
# 1280x768x60p    1280x800x60p       1360x768x60p
#
# 720x480x60p 720x480x59.94p 720x576x50p
#
# PAL-BG PAL-I PAL-M PAL-N PAL-NC SECAM NTSC-M NTSC-M-JPN

[% IF CONFIG_NAME == "mojito" -%]
resolution=640x480x60p
[% ELSE %]
# Load in any stored resolution setting
echo -n "Reading resolution from registry..."
resolution=`registry-direct .roku v`
if [ $? != 0 ]; then
    boot-progress --error registry-failed
fi
if [ "$resolution" == "" ]; then
	resolution=auto
	registry-direct .roku v $resolution
	echo -n "none found, using default: "
fi
echo $resolution
[% END -%]

# 'graphics_mode' can be set to one of:
#
# ARGB     - 32 bit ARGB
# ARGB1555 - 16 bit ARGB [1 Bit Alpha]
# ARGB4444 - 16 bit ARGB [4 Bit Alpha]
# RGB16    - 16 bit  RGB [No alpha]
# LUT8     -  8 bit CLUT
graphics_mode=[%CONFIG_FRAMEBUFFER_FORMAT%]
case ${resolution} in

	"pal-bg")
		resolution="PAL-BG"
		;;
	"pal-i")
		resolution="PAL-I"
		;;
	"pal-n")
		resolution="PAL-N"
		;;
	"pal-nc")
		resolution="PAL-NC"
		;;
	"secam")
		resolution="SECAM"
		;;
	"pal-m")
		resolution="PAL-M"
		;;
	"ntsc-m")
		resolution="NTSC-M"
		;;
	"ntsc-m-jpn")
		resolution="NTSC-M-JPN"
	   	;;

	*)
	   ;;
esac

rc_protocol=[%CONFIG_REMOTE_CONTROL_TYPE%]

# The size (in KiB) of the buffer space used by alternate directFB allocation pool
alt_directfb_bufsize=[%CONFIG_ALT_DIRECTFB_BUFSIZE%]

[% IF CONFIG_HDSD_DOWNSCALE_ENABLE -%]
# Enable simultaneous HD and SD outputs
hdsd_enable=1
[% ELSE %]
hdsd_enable=0
[% END -%]

[% IF CONFIG_ANALOG_VGA_ENABLE -%]
# Enable Analog VGA output
avga_enable=1
[% ELSE %]
avga_enable=0
[% END -%]

# Set width and height to zero (0) to use standard framebuffer resolutions
fb_width=0
fb_height=0

# Set the number of frame buffers to be created
[% IF CONFIG_ONE_FB -%]
num_fb=1
[% ELSE -%]
[%   IF CONFIG_OSD_SD_TO_HD_UPSCALING -%]
num_fb=1
[%   ELSE -%]
num_fb=2
[%   END -%]
[% END -%]

[% IF CONFIG_DOUBLE_BUFFERED_FB -%]
double_buffered=1
[% ELSE %]
double_buffered=0
[% END -%]

case "$1" in
    start)

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

[% IF CONFIG_PHSTBHDMI_WRAPPER -%]
	if [ "$resolution" == "auto" ]; then
	    opts_phStbHdmi=mode_option=NTSC-M
	else
	    opts_phStbHdmi=mode_option=${resolution}
	fi

	# Any HDMI driver must be loaded after phStbHdmi
	insmod $x/phStbHdmi.ko $opts_phStbHdmi
[% END -%]

[% IF CONFIG_HDMI_NEW_TDA9984 -%]
   # Startup HDMI driver very early so that EDID is read more quickly
   insmod $x/hdmi84.ko gpio_int_pin=[% CONFIG_HDMI_GPIO_PIN %]
[% END -%]

[% IF CONFIG_DIRECTFB_MULTIAPP -%]
if [ -f $x/fusion.ko ]; then
   insmod $x/fusion.ko
fi
[% END -%]

if [ -f $d/input/evdev.ko ]; then
insmod $d/input/evdev.ko
fi

[% IF CONFIG_RANDOM_NUMBER_GENERATOR_MODULE -%]
    if [ -f $c/pnx833x-rng.ko ]; then
        insmod $d/char/hw_random/pnx833x-rng.ko
    fi
[% END -%]

    echo -n "Loading pnx core modules..."
    insmod $x/phStbDbg.ko
    #### THIS IS THE PLACE TO ADD tmdbg statements like:
    #### tmdbg dbg enable phStbKBufMgr
    for m in phKernelIfce phStbOsal phStbKBufMgr; do
        eval insmod $x/$m.ko \$opts_$m
    done

    # phStbNetDev depends on phStbKBufMgr.
    # phStbBootDsp depends on phStbNetDev.
    insmod $x/phStbNetDev.ko mtu=500 subnet=[%CONFIG_DSP_IP_SUBNET%]
    hostip=[%CONFIG_DSP_IP_SUBNET%].1
    ifconfig lo 127.0.0.1
    ifconfig ipc0 $hostip

    echo "done"

    echo -n "Loading dsp..."
    opts_phStbBootDsp=cmdline="\"phStbNetDev.subnet=[%CONFIG_DSP_IP_SUBNET%][%IF CONFIG_UART1_MIPS2_CONSOLE%] console=ttyS1,115200[%END%]\""

[% IF CONFIG_DSP_TELNETD -%]
    # Enables telnet from HOST PC to dsp:
    echo 1 >/proc/sys/net/ipv4/ip_forward
[% END -%]

[% IF CONFIG_DSP_FILE_ACCESS -%]
    opts_phStbRSServer=multi
[% END -%]

[% IF CONFIG_HDMI_PRESENT -%]
	opts_phStbCore=hdmi=1
	opts_tda998x="[% IF ! CONFIG_PHSTBHDMI_WRAPPER -%]mode_option=NTSC-M [% END -%]gpio_int_pin=[% CONFIG_HDMI_GPIO_PIN %]"
[% IF CONFIG_HDMI_TDA9981_2_3 -%]
	tda998x_module=tda9983
[% END -%]
[% IF CONFIG_HDMI_TDA9984 -%]
	tda998x_module=tda9984
[% END -%]
[% IF CONFIG_HDMI_TDA998X_AUTO_SELECT -%]
	tda998x_module=$(exStbDetectHDMI)
[% END -%]
[% END -%]

[% IF CONFIG_HDMI_PRESENT -%]
[% IF CONFIG_HDMI_NEW_TDA9984 -%]

	# New driver is now initialized earlier to parallize EDID reading with loading of other modules

[% ELSE -%]

	[% IF CONFIG_HDMI_TDA998X_AUTO_SELECT -%][ -n "$tda998x_module" ] && [% END -%]insmod $x/${tda998x_module}.ko $opts_tda998x
[% END -%]

[% END -%]

	if [ "$resolution" == "auto" ]; then
		resolution=`videomode-auto 2>/dev/null`

		echo -n "auto mode selected: "
		echo $resolution
	fi

	echo $resolution > /etc/display.mode

	# Some of the video fixup scripts are interested in the resolution we've got
	export resolution


    # Set up any necessary module parameters
    # Set up the DirectFB display variables depending on the initial mode
    case ${resolution} in
    "1280x720x60p" | "1280x720x59.94p" | "1280x720x50p")
       if [ "${num_fb}" == 1 ]; then
       [% IF CONFIG_OSD_SD_TO_HD_UPSCALING -%]
          # TODO make this more generic, eg handle NTSC
          display_width=720
          display_height=576
       [% ELSE -%]
          display_width=1280
          display_height=720
       [% END -%]
          fb_width=${display_width}
          fb_height=${display_height}
       else
          display_width=720
          display_height=720
       fi
       ;;
    "1920x1080x60i" | "1920x1080x59.94i" | "1920x1080x50i" | "1920x1080x29.97p" | "1920x1080x50p" | "1920x1080x60p")
       if [ "${num_fb}" == 1 ]; then
       [% IF CONFIG_OSD_SD_TO_HD_UPSCALING -%]
          # TODO make this more generic, eg handle NTSC
          display_width=720
          display_height=576
       [% ELSE -%]
          display_width=1920
          display_height=1080
       [% END -%]
          fb_width=${display_width}
          fb_height=${display_height}
       else
          display_width=960
          display_height=540
       fi
       ;;
    "720x576x50p" | "PAL-BG" | "PAL-I" | "PAL-N" | "PAL-NC" | "SECAM")
       display_width=720
       display_height=576
       ;;
    "720x480x60p" | "720x480x59.94p" | "PAL-M" | "NTSC-M" | "NTSC-M-JPN")
       display_width=720
       display_height=480
       ;;
    "640x480x60p")
       display_width=640
       display_height=480
       ;;
    "800x600x75p")
       num_fb=1
       display_width=800
       display_height=600
       fb_width=${display_width}
       fb_height=${display_height}
       ;;
    "1024x768x75p")
       num_fb=1
       display_width=1024
       display_height=768
       fb_width=${display_width}
       fb_height=${display_height}
       ;;
    "1280x768x60p")
       num_fb=1
       display_width=1280
       display_height=768
       fb_width=${display_width}
       fb_height=${display_height}
       ;;
    "1280x800x60p")
       num_fb=1
       display_width=1280
       display_height=800
       fb_width=${display_width}
       fb_height=${display_height}
       ;;
    "1360x768x60p")
       num_fb=1
       display_width=1360
       display_height=768
       fb_width=${display_width}
       fb_height=${display_height}
       ;;
    *)
       echo "Invalid Display Mode found. Rebooting into default mode."
       registry-direct .roku v ""
       exec reboot
       ;;
    esac

echo Resolution=${resolution}
echo display_width=${display_width}
echo display_height=${display_height}
echo fb_width=${fb_width}
echo fb_height=${fb_height}

[% IF CONFIG_HW_ACCEL_DRAW -%]
    opts_phStbDraw=scratch_memory_size=[% CONFIG_STRETCH_SCRATCH_MEMORY %]
[% END -%]
    opts_phStbVo="mode_option=${resolution} hdsd_enable=$hdsd_enable videofilter_gpio=0x0908"
    opts_phStbVideoRenderer="video_mode=${resolution} hdsd_enable=$hdsd_enable background=0x000000 avga_enable=$avga_enable"
[% IF CONFIG_HDSD_DOWNSCALE_ENABLE -%]
[% IF CONFIG_HDSD_DOWNSCALE_FLICKER_FILTER_ENABLE -%]
    opts_phStbVideoRenderer="$opts_phStbVideoRenderer hdsd_flicker_filter_enable=1"
[% ELSE %]
    opts_phStbVideoRenderer="$opts_phStbVideoRenderer hdsd_flicker_filter_enable=0"
[% END -%]
[% END -%]
    opts_phStbRCProtocol=rc_protocol=${rc_protocol}
[% IF CONFIG_IR_INPUT_1 -%]
    opts_phStbRCProtocol="$opts_phStbRCProtocol input=1"
[% END -%]
    opts_phStbFB="width=${fb_width} height=${fb_height} num_fb=${num_fb} double_buffered=${double_buffered} background=0x00000000"

[% IF CONFIG_DIRECTFB_VERSION_1_2_X -%]
    opts_phStbFB="$opts_phStbFB extended_fb_mem=0"
[% END -%]
[% IF CONFIG_ALT_DIRECTFB_BUFSIZE > 0 -%]
    opts_phStbFB="$opts_phStbFB use_kernel_buffmgr=1"
[% END -%]
[% IF !CONFIG_CACHED_ACCESS_TO_FB -%]
    opts_phStbFB="$opts_phStbFB cached_memory=0"
[% END -%]
    opts_phStbFB="$opts_phStbFB gfx_memory_size=[% CONFIG_FB_SCRATCH_MEMORY %]"
    opts_phStbFB="$opts_phStbFB gfx_display_mode=${graphics_mode}"

opts_phStbCore="$opts_phStbCore Core_sharedMemory=1"
   
    # We've got HDMI up and running and we've now chosen our 'auto'mode, start the next few modules 

    for m in \
	phStbIpc \
	phStbCore \
	phStbRSServer \
	[% IF CONFIG_DEV_MMIO -%] phStbMmio [% END -%] \
	[% IF CONFIG_HW_ACCEL_DRAW -%] phStbDraw [% END -%] \
	phStbVideoRenderer \
	phStbVo \
    ; do
        eval insmod $x/$m.ko \$opts_$m
    done

	# We've now got the video renderer running, so tell the HDMI chip what mode to really run at
	hdmi-setmode ${resolution}
    
    if [  "${fb_width}" != 0 ]; then
       if [  "${fb_width}" != ${display_width} ]; then
           display_width=${fb_width}
           echo "Non-standard Framebuffer display width - update /etc/fb.modes"
       fi
    fi


    if [  "${fb_height}" != 0 ]; then
       if [  "${fb_height}" != ${display_height} ]; then
           display_height=${fb_height}
           echo "Non-standard Framebuffer display height - update /etc/fb.modes"
       fi
    fi

    for m in \
    [% IF CONFIG_FRAMEBUFFER -%] phStbFB[% END -%] \
    ; do
        eval insmod $x/$m.ko \$opts_$m
    done

    echo Build directfb
    
    # Set up the DirectFB init file
    echo "mode="${display_width}"x"${display_height} > /etc/directfbrc
    echo "pixelformat=${graphics_mode}" >> /etc/directfbrc
    echo "no-vt-switch" >> /etc/directfbrc
[% IF CONFIG_DIRECTFB_VERSION_1_2_X -%]
    echo "system=devmem" >> /etc/directfbrc
    echo "video-phys="$(cat /sys/module/phStbFB/parameters/gfx_memory_start) >> /etc/directfbrc
    echo "video-length="$(cat /sys/module/phStbFB/parameters/gfx_memory_available) >> /etc/directfbrc
    # The lines below stop the layer flipping at startup.
    echo "init-layer=0" >> /etc/directfbrc
    echo "layer-bg-none" >> /etc/directfbrc
    if [ "${num_fb}" == 2 ]; then
    echo "init-layer=1" >> /etc/directfbrc
    echo "layer-bg-none" >> /etc/directfbrc
    fi
    # Uncomment the lines below to help with debugging DirectFB issues.
    #echo "debug" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/2D" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/Encoder" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/Gfx" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/Video" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/GSurf" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/GLock" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/Mixer" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/Output" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/Screen" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/Utils" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/Video" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/VSurf" >> /etc/directfbrc
    #echo "debug = NXP/STB22X/VLock" >> /etc/directfbrc
    # Uncomment the lines below to help debug VideoProvider issues.
    #echo "debug = NXP/STB22x/VideoProviderASF" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/VideoProviderCommon" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/VideoProviderES" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/VideoProviderTS" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/VideoProviderV4L" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/VideoProviderGst" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/VideoProviderGstPlatform" >> /etc/directfbrc
    #echo "debug = NXP/STB22x/VideoProviderGstEvents" >> /etc/directfbrc
    # Uncomment the lines below to help debug ImageProvider issues.
    #echo "debug = NXP/STB22x/ImageProvider" >> /etc/directfbrc
    # The sections below help debug DirectFB.
    #echo "debug = Core/Font" >> /etc/directfbrc
    #echo "debug = Core/Font/Surf" >> /etc/directfbrc
    #echo "debug = Core/Graphics" >> /etc/directfbrc
    #echo "debug = Core/GraphicsOps" >> /etc/directfbrc
    #echo "debug = Core/GfxState" >> /etc/directfbrc
    #echo "debug = Core/Input" >> /etc/directfbrc
    #echo "debug = Core/Input/Evt" >> /etc/directfbrc
    #echo "debug = Core/Layers" >> /etc/directfbrc
    #echo "debug = Core/Surface" >> /etc/directfbrc
    #echo "debug = Core/SurfBuffer" >> /etc/directfbrc
    #echo "debug = Core/SurfaceCore" >> /etc/directfbrc
    #echo "debug = Core/System" >> /etc/directfbrc
    #echo "debug = Core/Windows" >> /etc/directfbrc
    #echo "debug = Core/WindowStack" >> /etc/directfbrc
    #echo "debug = Core/WM" >> /etc/directfbrc
    #echo "debug = DevMem/Surfaces" >> /etc/directfbrc
    #echo "debug = DevMem/SurfLock" >> /etc/directfbrc
    #echo "debug = Direct/Interface" >> /etc/directfbrc
    #echo "debug = Direct/Clock" >> /etc/directfbrc
    #echo "debug = Direct/Main" >> /etc/directfbrc
    #echo "debug = Direct/Mem" >> /etc/directfbrc
    #echo "debug = Direct/Modules" >> /etc/directfbrc
    #echo "debug = Direct/Signals" >> /etc/directfbrc
    #echo "debug = Direct/Thread" >> /etc/directfbrc
    #echo "debug = Direct/Thread/Init" >> /etc/directfbrc
    #echo "debug = DirectFB/Core" >> /etc/directfbrc
    #echo "debug = IDFBEventBuffer" >> /etc/directfbrc
    #echo "debug = IDirectFB" >> /etc/directfbrc
    #echo "debug = IDirectFBDisplayLayer" >> /etc/directfbrc
    #echo "debug = IDirectFBSurface" >> /etc/directfbrc
    #echo "debug = IDirectFBSurfaceL" >> /etc/directfbrc
    #echo "debug = IDirectFBSurfaceW" >> /etc/directfbrc
    #echo "debug = SurfaceManager" >> /etc/directfbrc
    #echo "debug = WM/Default" >> /etc/directfbrc
    # The following help to debug any GTK issues that might arise.
    #echo "debug = GDKDFB/Crossing" >> /etc/directfbrc
    #echo "debug = GDKDFB/CrossX" >> /etc/directfbrc
    #echo "debug = GDKDFB/Drawable" >> /etc/directfbrc
    #echo "debug = GDKDFB/DrawClip" >> /etc/directfbrc
    #echo "debug = GDKDFB/Events" >> /etc/directfbrc
    #echo "debug = GDKDFB/EventCheck" >> /etc/directfbrc
    #echo "debug = GDKDFB/EventTrans" >> /etc/directfbrc
    #echo "debug = GDKDFB/Grab/Kbd" >> /etc/directfbrc
    #echo "debug = GDKDFB/Grab/Ptr" >> /etc/directfbrc
    #echo "debug = GDKDFB/Scroll" >> /etc/directfbrc
    #echo "debug = GDKDFB/Visual" >> /etc/directfbrc
    #echo "debug = GDKDFB/WinIDTable" >> /etc/directfbrc
    #echo "debug = GDKDFB/WinIDLookup" >> /etc/directfbrc
[% ELSE -%]
    echo "depth=32" >> /etc/directfbrc
[% END -%]

    echo Start boot splash

    # Start boot splash now
    progress_switch=`registry-direct boot splash`

    progress initialise $init_script $progress_switch

    # And startup the rest of the modules
    
    for m in \
    phStbBootDsp \
    [% IF CONFIG_EEPROM_PRESENT -%] at24c64 [% END -%] \
    [% IF CONFIG_SCART_AK470X -%] ak470x [% END -%] \
    [% IF CONFIG_SCART_UDA3000 -%] uda3000 [% END -%] \
    phStbGpio \
    [% IF CONFIG_SAA1064 -%] saa1064 [% END -%] \
    [% IF CONFIG_RC_PROTOCOL -%] phStbRCProtocol [% END -%] \
    [% IF CONFIG_INFRA_RED -%] phStbIR [% END -%] \
    [% IF CONFIG_FRONT_PANEL_KEYBOARD -%] phStbKey [% END -%] \
    [% IF CONFIG_WMDRMPD -%] phStbWmdrmPd [% END -%] \
    [% IF CONFIG_NAME == 'afte' -%] phPnx833xAfte [% END -%] \
    [% IF CONFIG_SMART_CARD_PRESENT -%] phStbSmc [% END -%] \
    ; do
        echo insmod $x/$m.ko $opts_$m
        eval insmod $x/$m.ko \$opts_$m
    done
    echo "About to rmmod phStbBootDsp"
    rmmod phStbBootDsp
    echo "rmmod phStbBootDsp complete"

[% IF CONFIG_MULTICAST -%]
    # Ensure that Multicast streams can be accessed through either Ethernet connection
    echo "About to add multicast route"
    /sbin/route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0 > /dev/null 2>&1
    /sbin/route add -net 224.0.0.0 netmask 240.0.0.0 dev eth1 > /dev/null 2>&1
    echo "Added multicast route"
[% END -%]

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