#!/bin/sh

source /etc/utils/shell-utils.sh


export XDG_RUNTIME_DIR="/run"

waitfordir=60
while [ ! -d $XDG_RUNTIME_DIR -a $waitfordir -gt 0 ];
do
    sleep 1
    echo "waiting XDG_RUNTIME_DIR mount while $waitfordir > 0 "
    waitfordir=$((waitfordir-1))
done
echo 'XDG_RUNTIME_DIR mounted'

getPreferreVideodMode() {
    list=$(modetest 2>&1 | grep flags | grep type)
    while IFS= read -r line; do                   
        lace="p"                                  
        if [[ ! -z $( echo "$line" | grep "preferred") ]]; then      
            [[ ! -z $( echo "$line" | grep "interlace") ]] && lace="i"
            IFS=' ' read -r -a params  <<< "$line"                     
            echo "${params[2]}x${params[6]}$lace${params[1]}Hz"        
            break                                                      
        fi                                                             
     done <<< "$list"                                                              
}

getPreferredWindowSize() {
    IFS='xpi' read -r  -a size <<< "$1"
    [[ ${size[0]} -gt 1920 || ${size[1]} -gt 1080 ]] && echo "1920x1080" || echo "${size[0]}x${size[1]}"
}


setupTvmode() {
    case $1 in
        3840x2160p60)
            g_tvmode="3840x2160p60" ;;
        3840x2160p50)
            g_tvmode="3840x2160p50" ;;
        1080p-60)
            g_tvmode="1920x1080p60" ;;
        1080i-60)
            g_tvmode="1920x1080i60" ;;
        1080p-50)
            g_tvmode="1920x1080p50" ;;
        1080i-50)
            g_tvmode="1920x1080i50" ;;
        720p-60)
            g_tvmode="1280x720p60"  ;;
        720p-50)
            g_tvmode="1280x720p50"  ;;
        576p-50)
            g_tvmode="720x576p50"   ;;
        pal|PAL)
            g_tvmode="720x576i50"   ;;
        ntsc|NTSC)
            g_tvmode="720x480i60"   ;;
        Auto)
            g_tvmode=$(getPreferreVideodMode) &&  g_window_size=$(getPreferredWindowSize "$g_tvmode")
        ;;
        *)
           g_tvmode="1280x720p60" ;;

    esac
    g_window_size=$(getPreferredWindowSize "$g_tvmode")
}

setupGrmode() {
    if [ "$2" = "720x480i60" ]; then
	g_window_size="720x480"
	return
    fi

    case $1 in
        1920*)
            g_window_size="1920x1080"   ;;
        1280*)
            g_window_size="1280x720"    ;;
        720|720x576*)
            g_window_size="720x576"     ;;
        720x480*)                                                                                 
            g_window_size="720x480"     ;; 
        tvsystem_res)
            setupGrmode $2              ;;
    esac
}


get_fw_env "graphicres" gres
get_fw_env "tvsystem" tvmode


#Defaults in case of empty parameters
[ -z "$gres" ] && gres="tvsystem_res"
[ -z "$tvmode" ] && tvmode="Auto"


setupTvmode  $tvmode
setupGrmode  $gres $g_tvmode


log
log "Setup WESTEROS display. STB config: [graphicres = $gres, tvsystem = $tvmode], WESTEROS config: TVMODE = $g_tvmode, WINDOW_SIZE = $g_window_size"
log

export  WESTEROS_GL_USE_BEST_MODE=0
export  WESTEROS_GL_MODE="$g_tvmode"
export  WESTEROS_GL_MAX_MODE=3840x2160
export  WESTEROS_GL_GRAPHICS_MAX_SIZE=1920x1080
export  WESTEROS_GL_USE_AMLOGIC_AVSYNC=1
export  WESTEROS_SINK_AMLOGIC_USE_DMABUF=1
export  WESTEROS_GL_REFRESH_PRIORITY=F,80
export  WESTEROS_GL_USE_REFRESH_LOCK=1
export  WESTEROS_SINK_USE_FREERUN=1
export  WESTEROS_SINK_USE_ESSRMGR=1
export  ESSOS_NO_EVENT_LOOP_THROTTLE=1
export  AVPK_SKIP_HDMI_VALIDATION=1



#For wesetros-sink
echo 1 > /sys/module/amvdec_ports/parameters/multiplanar
echo 1 > /sys/module/amvdec_ports/parameters/vp9_need_prefix
echo 1 > /sys/module/amvdec_ports/parameters/av1_need_prefix
echo 0 > /sys/module/drm/parameters/vblankoffdelay
echo 4181938 > /sys/module/amvdec_mh264/parameters/error_proc_policy


#For wesetros-soc-drm
echo 1 > /sys/module/aml_drm/parameters/video_axis_zoom


export LD_PRELOAD=/usr/lib/libwesteros_gl.so.0.0.0
exec /usr/bin/infomir-shell --width $(echo $g_window_size | cut -d'x' -f1) --height $(echo $g_window_size | cut -d'x' -f2) --framerate 60


