#!/bin/sh


# 获取系统语言环境 | Get system locale
LANG=$(locale | grep LANGUAGE | cut -d '=' -f 2 | cut -d '_' -f 1)
if [ "$LANG" != "zh" ] ; then
    LANG="en"
fi

# 中文符号支持 | Support special Chinese characters 
export LC_ALL=zh_CN.UTF-8

# 翻译文本 | Translation variables
readonly TITLE_zh="警告"
readonly TITLE_en="Warning"
readonly TEXT_zh="未检测到 NVIDIA 显卡或显卡驱动，无法使用NVIDIA显卡启动"
readonly TEXT_en="No NVIDIA graphic card or NVIDIA X driver not detected. Cannot run with NVIDIA graphic card"

eval "TITLE=\$TITLE_${LANG}"
eval "TEXT=\$TEXT_${LANG}"

# 判断 NVIDIA 闭源驱动 是否安装 | Judge whether nvidia-driver is installed
lshw -c video 2>/dev/null | grep "driver=nvidia" > /dev/null
if [ $? -ne 0 ]
then
	# zenity --warning --width=500 --title="$TITLE" --text="$TEXT"
    notify-send -t 5000 -a dde-dock-graphics-plugin -i dialog-warning "$TEXT"
    exit
fi

FILE=$1

if [ "${FILE##*.}"x = "desktop"x ]
then
    __NV_PRIME_RENDER_OFFLOAD=1 __VK_LAYER_NV_optimus=NVIDIA_only __GLX_VENDOR_LIBRARY_NAME=nvidia $(cat $FILE | grep "Exec=" | sed "s/^Exec=//" | sed "s/%.//" | sed "s/^\"//g" | sed "s/\" *$//g" | head -n 1)
else
    __NV_PRIME_RENDER_OFFLOAD=1 __VK_LAYER_NV_optimus=NVIDIA_only __GLX_VENDOR_LIBRARY_NAME=nvidia "$@"
fi
