#!/bin/sh -
#===============================================================================
#
#          FILE: system-generator-check-gpu
#
#   DESCRIPTION: The goal are to detect if the gpu are present/loaded or not
#  ORGANIZATION: STMicroelectronics
#     COPYRIGHT: Copyright (C) 2018, STMicroelectronics - All Rights Reserved
#===============================================================================

check_dt_status() {
    gcnano_path=$(find /proc/device-tree/soc* | grep gpu | head -n 1)
    gcnano_dir=$(basename $gcnano_path)
    if [ -z "$gcnano_dir" ] || [ ! -d $gcnano_path ];
    then
        echo "Gcnano in /proc/device-tree/soc/ is not available" > /dev/kmsg
        force_no_dtb_gpu="1"
    else
        if [ ! -f $gcnano_path/status ]; then
            gcnano_status="okay"
        else
            if $(grep -q "okay" $gcnano_path/status); then
                gcnano_status="okay"
            fi
        fi
    fi
}

check_load_module() {
    local _timeout=0
    if [ -n "$(cat /proc/modules | grep etnaviv)"  ]; then
        galcore_module=$(cat /proc/modules | grep ^etnaviv)
        return
    fi
    while [ -z "$(cat /proc/modules | grep galcore)" ]; do
        sleep 1
        _timeout=$(($_timeout+1))
        if [ "$_timeout" -eq "10" ]; then
            echo "Time out check load galcore module expired" > /dev/kmsg
            break
        fi
    done
    galcore_module=$(cat /proc/modules | grep galcore)
}

check_devices() {
    local _timeout=0
    if [ -n "$(cat /proc/modules | grep etnaviv)"  ]; then
        galcore_device=$(cat /proc/modules | grep ^etnaviv)
        return
    fi
    while [ -z "$(ls -l /dev/ | grep galcore)" ]; do
        sleep 1
        _timeout=$(($_timeout+1))
        if [ "$_timeout" -eq "10" ]; then
            echo "Time out check galcore device expired" > /dev/kmsg
            break
        fi
    done
    galcore_device=$(ls -l /dev/ | grep galcore)
}

check_default_weston_config() {
    if [ "$force_no_dtb_gpu" = "1" ];
    then
        if [ -e /etc/default/weston ] && $(grep -q "Autogenerated" /etc/default/weston);
        then
            default_weston_status="Autogenerated"
        fi
    fi
}
add_weston_force_renderer() {
    # with HDMI and GPU with 2 planes
    # sometime we need to force WESTON_FORCE_RENDERER
    if  $(cat /proc/device-tree/compatible | grep -q "stm32mp15")
    then
        hdmi_status=$(cat /sys/class/drm/card0-HDMI-A-1/status)
        if [ -e /etc/default/weston ] &&  ! $(grep -q "WESTON_FORCE_RENDERER" /etc/default/weston) ; then
            echo "#" >> /etc/default/weston
            echo "# if \"LTDC fifo underrun\" messages occur " >> /etc/default/weston
            echo "# You can force the RENDERER on weston" >> /etc/default/weston
            echo "#WESTON_FORCE_RENDERER=1" >> /etc/default/weston
        fi
        if [ "$hdmi_status" = "connected" ]; then
            if [ -e /etc/default/weston ] && $(grep -q "WESTON_FORCE_RENDERER" /etc/default/weston) ; then
                sed -i "s/^#WESTON_FORCE_RENDERER=1/WESTON_FORCE_RENDERER=1/g" /etc/default/weston
            fi
        else
            if [ -e /etc/default/weston ] && $(grep -q "Autogenerated" /etc/default/weston) ; then
                # disable WESTON_FORCE_RENDERER
                sed -i "s/^WESTON_FORCE_RENDERER=1/#WESTON_FORCE_RENDERER=1/g" /etc/default/weston
            fi
        fi
    fi
}


#
# Main
#
gcnano_status="disabled"
galcore_module=""
galcore_device=""
default_weston_status=""
force_no_dtb_gpu=0

check_dt_status
check_default_weston_config
if [ "$default_weston_status" = "Autogenerated" ];
then
    echo "Weston already configured"
    exit 0
fi

check_load_module
check_devices

if [ "$gcnano_status" = "okay" ] && [ -n "$galcore_module" ] && [ -n "$galcore_device" ];
then
    #echo "Gcnano is present and activated" > /dev/kmsg
    add_weston_force_renderer
    if [ -f /etc/default/weston ] && $(grep -q "Autogenerated" /etc/default/weston) ;
    then
        sed -i "s/^OPTARGS=--use-pixman/#OPTARGS=--use-pixman/g" /etc/default/weston
        sed -i "/#Autogenerated/d" /etc/default/weston
    fi
    if [ ! -f /etc/default/weston ];
    then
        echo "#Autogenerated" > /etc/default/weston
        echo "#OPTARGS=--use-pixman" >> /etc/default/weston
        echo "WESTON_USER=weston" >> /etc/default/weston
        add_weston_force_renderer
    fi
else
    if [ -f /etc/default/weston ] && $(grep -q "pixman" /etc/default/weston) ;
    then
        echo "Weston already configured on pixman" > /dev/kmsg
    else
        echo "Configure weston on pixman" > /dev/kmsg
        echo "#Autogenerated" > /etc/default/weston
        echo "OPTARGS=--use-pixman" >> /etc/default/weston
        echo "WESTON_USER=weston" >> /etc/default/weston
        add_weston_force_renderer
    fi
fi

dri_device=$(ls -l /dev/ | grep dri)
if [ -z "$dri_device" ];
then
    exit 1
fi
