#!/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_dir=$(ls /proc/device-tree/soc/ | grep gpu)
    if [ -z "$gcnano_dir" ] || [ ! -d /proc/device-tree/soc/$gcnano_dir ];
    then
        echo "Gcnano in /proc/device-tree/soc/ is not available" > /dev/kmsg
        force_no_dtb_gpu="1"
    else
        if [ ! -f /proc/device-tree/soc/$gcnano_dir/status ]; then
            gcnano_status="okay"
        else
            if $(grep -q "okay" /proc/device-tree/soc/$gcnano_dir/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
}
#
# 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
    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
    fi
else
    if [ -f /etc/default/weston ] && $(grep -q "Autogenerated" /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
    fi
fi

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